ifacethoughts

Interface Segregation Principle

All the basic design principles converge on the use of abstraction for incorporating flexibility using loose coupling behaviour. Interface Segregation Principle (ISP) is a guideline for designing different interfaces.

Interfaces are useful in designing real world systems, where every object can have multiple aspects. This aspect can be a facet, a behaviour, a role – any entity that qualifies the object with that aspect. Lets take a real world example – a modern cell phone (mobile) acts more than just a phone, it is a phone, a MP3 player and a video player. When I am using it as a MP3 player, I will like to specify the song I want to play and listen to it, nothing more nothing else. When I am using it as a mobile phone, I will like to provide the number and speak, nothing more nothing else. I will get extremely irritated if I am asked for a phone number when I want to listen to a song. It is better if only the relevant questions are asked.

This is the essence of Interface Segregation Principle.

Clients should not be forced to depend upon interfaces that they do not use.

The cell phone had interfaces of a phone and a MP3 player. ISP advises to keep these two interfaces completely independent of each other. This way the phone can be treated purely as a phone or as a MP3 player. Programmatically, I can use only the interface of a MP3 player in my method called playSong. In future even if the interface for the phone is modified, playSong would remain the same.

Logically, all ISP says is that if there are two non-cohesive functionalities, keep them separate. So that they can be used independent of other. This avoids design of fat interfaces, and provides a clear design to the user (client). Break the functionalities into atomic interfaces that can be then individually accessed by the user. A good usage of Law Of Demeter can help in designing these atomic interfaces.

Related Links:

Back to Design Principles.

Technorati tags: , , ,

Discussion [Participate or Link]

  1. Apurva said:

    This is the best explanation of the ISP that i have ever gon through

  2. Abhijit Nadgouda said:

    Thanks Apurva!

  3. Raheem MA said:

    U have explained with best example! pl can u give me other example?

  4. Sanjib said:

    The example is quite explainatory

  5. The Inheritance And Composition Duel | iface thoughts said:

    […] a guideline, I first scan the interfaces thoroughly, maybe using Interface Segregation Principle, and look at composition if I am looking mainly for code reuse. Alarms also start ringing if I get […]

  6. tajoli said:

    clearly explained ISP I have ever come across.

Say your thought!

If you want to use HTML you can use these tags: <a>, <em>, <strong>, <abbr>, <code>, <blockquote>. Closing the tags will be appreciated as this site uses valid XHTML.

freshthoughts

contactme

Abhijit Nadgouda
iface Consulting
India
+91 9819820312
My bookmarks

badgesand...

This is the weblog of Abhijit Nadgouda where he writes down his thoughts on software development and related topics. You are invited to subscribe to the feed to stay updated or check out more subscription options. Or you can choose to browse by one of the topics.