Should my classes prevent developers from doing the wrong thing with them?

I try to understand where good contracts end and paranoia begins. Actually, I just don’t know what a good developer should take care of and what he should forget about :)

Let's say I have a class that contains values ​​(s) such as java.lang.Integer. Its instances are aggregated by other objects (MappedObjects), (one-to-many or many-to-many) and are often used inside MappedObjects methods. For performance reasons, I also track these relationships in TreeMap (besides, guava MultiMap doesn't matter) to be able to quickly iterate over MappedObjects bound to a certain range of Integer keys. Thus, in order to maintain system integrity, I have to change the MappedObject.bind (Integer integer) method to update my map as follows:

class MappedObject {
    public void bind (Integer integer) {
        MegaMap.getInstance().remove(fInteger, this);
        fInteger = integer;
        MegaMap.getInstance().add(fInteger, this);
    }

    ...

    private Integer fInteger;
}

MappedObject , , . MappedObject bind() - .

+5
5

, . , , init ( ServletConfig), , , , , .

API- -, . , , , , .

+4

. , , . , API , , .

+1

, , , .

: (int) (Integer), .

, bind. null, int Integer .

+1

, .

, . , , , .

, - , .. - . :

class MappedObject {
public final void bind (Integer integer) {
    MegaMap.getInstance().remove(fInteger);
    internalBind( integer );
    MegaMap.getInstance().add(fInteger);
}

protected void internalBind( Integer integer ) {
 fInteger = integer;
}

...

private Integer fInteger;

}

internalBind(), , bind() .

: , (), (, ), (, ).

+1

If you think your API users are stupid, you should prohibit misuse. Otherwise, you should not interfere with doing what they need.

Domumentation and good naming of classes and methods should indicate how to use your API.

0
source

All Articles