Create an interface for an object or action / behavior?

When you create an interface, create it based on behavior, follow the -able standard, for example.

interface Comparable interface Enumerable interface Listable interface Talkable interface Thinkable 

Or based on an object like

 interface Comparator interface Enumerator interface List interface Human 

And why?

UPDATE

This question is not about the naming convention (-strong suffix or I-prefix). It is about the intention of the interface design and its impact on: -

  • flexibility
  • complexity / simplicity
  • maintainability

For example, if I need to implement various functions by executing a method, 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 an interface based on an object, we can use it as

 public class Man implements Human public class Woman implements Human 

And we can also use it for polymorphism purposes.

 Human man = new Man(); 
+4
source share
3 answers

Interfaces are not about what a particular class does. They are more about what the client needs and what is being replaced instead of what the client needs. For example, instead of trying to extract an interface from the Man class, instead focus on one of the classes that use the Man class. What does it do? Perhaps this is a Conversation class that wants to make things talk to each other. If you gave him two Man classes, he could call talk() on them. But if you want it to be more flexible, you can ignore the concept of what it does - makes you talk, and use the Talkable interface (for example). When you see the Talkable interface in this Conversation class, you should not think that this is really Man , but rather: โ€œWhere I see Talkable , I can replace everything that Talkable implements, be it Man , Woman , Parrot , Robot , etc. .d. "

If I'm unclear, there are good resources on this. Check Robert Martin The principle of dependency inversion , The principle of separation of chains , The principle of Liskov replacement for beginners.

+4
source

The Comparable interface must be implemented by classes whose instances can be COMPARED; the Comparator interface (in languages โ€‹โ€‹that perform any level of general programming, possibly Comparator<T> ! -) should be implemented by classes whose instances can COMPARE OTHER. Both uses have excellent precedents that do not essentially overlap with each other; it seems to you that there is no key semantic difference.

+3
source

Interfaces have been created to determine the required behavior of objects, and you can have similar behavior for several objects. For example, if you want to create human behavior, you can have objects of man and woman that share the same behavior, but have some different functions / attributes. If you do not use interfaces in this way, in this case you will need to create 2 interfaces for both sexes.

So, interfaces are related to the behavior of objects.

+1
source

All Articles