views:

90

answers:

3

When come to create an interface, do u create it based on behavior, follow the -able standard, e.g.

interface Comparable
interface Enumerable
interface Listable
interface Talkable
interface Thinkable

Or based on object, e.g.

interface Comparator
interface Enumerator
interface List
interface Human

And why?

UPDATE

This question is not about the naming convention (-able suffix or I- prefix). This is about the design intention of the interface, and the impact of it, in terms of:-

  1. flexibility
  2. complexity / simplicity
  3. maintainability

For example, if I need to implement different features, by doing -able way, I can declare my class as

public class Man implements Talkable, Thinkable, Laughable
public class Woman implements Talkable, Thinkable, Laughable

On the other hand, if we create interface based on object, we can use it as

public class Man implements Human
public class Woman implements Human

And we can also use it for polymorphism purpose.

Human man = new Man();
+1  A: 

Hi,

Interfaces were made to define the necessary behaviour of objects, and you can have a similar behaviour for multiple objects .. Like if you want to create the behaviour of humans, you can have the objects man and women, who share the same behaviours, but have some distinct functions / attributes. If you don't use interfaces this way, in this case you would have to create 2 interfaces for both human genders.

So, interfaces are related to the behaviour of objects.

yoda
+3  A: 

Interfaces are not about what a concrete class does. They are more about what the client needs, and what is substitutable in place of what the client needs. For example, instead of trying to extract an interface from a Man class, instead focus on one of the classes that use the Man class. What does it do? Perhaps it is a Conversation class that wants to make things talk to each other. If you gave it two Man classes, it could call talk() on them. But if you wanted it to be more flexible, you can abstract out the concept of what it does - makes things talk, and have it use a Talkable interface (for example). When you see a Talkable interface on this Conversation class, you shouldn't think "that is really a Man" but rather, "Where I see a Talkable, I can substitute anything that implements Talkable, whether it is a Man, Woman, Parrot, Robot, etc."

If I am unclear, there are good resources available on the subject. Check out Robert Martin's Dependency Inversion Principle, Interface Segregation Principle, and Liskov Substitution Principle articles for starters.

SingleShot
+3  A: 

An interface Comparable should be implemented by classes whose instances can be COMPARED; an interface Comparator (in languages that do any level of generic programming, probably Comparator<T>!-) should be implemented by classes whose instances can COMPARE OTHERS. Both usages have excellent use cases, essentially disjoint from each other; you seem to be missing this key semantic distinction.

Alex Martelli
the example I am using is not tied to any implementation of particular language (Java). The Comparable/Comparator interface are just one of the example, which might be used in C#, C++, Actionscript etc.
janetsmith
Nor is my answer Java-y, I'm just suggesting the `<T>` notation for genericity because it's widely used. My point's about *English*: an Enumerable *can be* enumerated, an Enumerator *performs* the enumerating; and so forth, for most transitive verbs. Very different semantics. Even intransitive verbs work similarly (typically with some preposition implied): a Laughable object is one that *can be* laughed AT (that's what the word MEANS!), a Laugher is a subject who laughs. It's subject vs (grammatical) direct object.
Alex Martelli