Click here to Skip to main content
15,569,547 members
Articles / Programming Languages / Java
Technical Blog
Posted 23 Jul 2012

Tagged as

Stats

13.7K views
3 bookmarked

Interface Segregation Principle (ISP)

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
23 Jul 2012CPOL1 min read
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)


Written By
Software Developer (Senior)
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot everywhere Pin
Jasper4C#31-Jul-12 5:47
Jasper4C#31-Jul-12 5:47 
GeneralMy vote of 3 Pin
Jasper4C#31-Jul-12 5:45
Jasper4C#31-Jul-12 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.