The Artima Developer Community
Sponsored Link

Interface Design by Bill Venners
Chapter 2
Classes, Inheritance, and Composition

Advertisement

Interface Design | Contents | Previous | Next

The Guidelines:

Method names should describe that activity from the perspective of the object upon which the method is being invoked. It says what the object can do. For example, a method that allows a client to pass an AmorphousBlob to an object would be named:

void acceptABlob(AmorphousBlob blob);
Not
void giveABlob(AmorphousBlob blob);
acceptABlob() says what the object can do. giveABlob() says what the client can do to the object.
Use Java interfaces to describe abstract interfaces.

Figuring out what the parts are and what the interfaces are between the parts is important, and it is hard because interfaces are difficult to change later. Using Java interfaces to represent abstract interfaces helps separate interface from implementation because you can do whatever you want behind the interface. But the interface itself is quite rigid. (It's the explosive supertype problem.) The interface is the point of coupling.

This...:

interface Talented {

    void sing();
    void warmUpVoice();
    void dance();
    void stretchLegs();
}

...is not as good as this:

interface Singer {

    void sing();
    void warmUpVoice();
}

interface Dancer {

    void dance();
    void stretchLegs();
}

sing() and warmUpVoice(), now these are words that go together well.

If you really wanted a Talented interface, then you could combine Singer and Dancer like this:

interface Talented extends Singer, Dancer {

}

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use