Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Interface Segregation Principle (ISP)

3.50/5 (2 votes)
23 Jul 2012CPOL1 min read 14.6K  
The smaller an interface is, the better it is.

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:

Java
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:

Java
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:

Java
...
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".

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)