SOLID principles:
The smaller an interface is, the better it is.
The idea of interfaces is to say "implement me to tell others that your class can do what I define". When you see something like:
class Button implements Clickable, Focusable, HasPosition, HasText, Widget {
...
}
In pure English, it reads as "You can click button, it can be focused or not, it has position and text". On the other hand, when you see something like this:
class PushButton implements Button {...}
It doesn't give you any idea about what the PushButton
is. There are mechanisms in Java and C# to describe the type constraints when you need to pass something that is a widget+has text+focusable.
Another point here is that classes often depend on interfaces (they require objects implementing interfaces to be passed as their constructor arguments). So, when you see something like:
...
void processItems(Iterable<Integer> items) {
...
}
...
You can easily tell that this method doesn't really care about order of items, it doesn't care about the number of these items, and for sure it is not going to add any new items to whatever you pass there. You can easily pass an ArrayList<T>
there, but still this method says: "I don't really care if you give me a Set or an ArrayList
, I just need items".