Interface segregation principle

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
Interface Segregation Principle, by [[Bob Martin | Robert Martin]] says that clients should not be forced to depend upon interfaces that they do not use. Or to put it more simply, many client specific interfaces are better than one general purpose interface. This is to avoid [[Fat interfaces]] surfacing in the design.
+
Interface Segregation Principle, by [[Bob Martin | Robert Martin]] says that clients should not be forced to depend upon interfaces that they do not use. Put more simply, many client specific interfaces are better than one general purpose interface. This principle prevents [[Fat interfaces]] surfacing in the design.
  
A good example of this principle is a modern cellphone where it can be a phone but can also be a camera or a music player. However some cellphones, especially the older generation ones, do not have camera or music players. Given these facts, now if we have an interface that provides phone, camera and music functionality (and thus, fat), the older cellphones are forced to implement the camera and music functionality which are not used.
+
A good example of this principle is a modern cellphone which, in addition to a phone, is also a camera and an MP3 player. However some cellphones, especially older ones, do not have camera or MP3 player. Now imagine that we have an interface which provides access to the phone, camera and MP3 functionality.  This would be quite a fat interface.  If an older cellphone wishes to use this interface, the camera and MP3 functionality will appear to be present, but won't actually work.
  
A better way to solve this is we have smaller and distinct interfaces; one for defining phone functionality, another for defining camera functionality, and the last one will define the music player functionality. Now the modern cell phones can interface with all of them, while the older cell phone only needs to interface with the phone functionality interface, resulting in a cleaner implementation and design.
+
A better way to solve this is to have small, distinct interfaces.  In the case of our example we would have 3 interfaces for phone, camera and MP3 functionality. Now, modern cell phones can implement all three interfaces, while older cell phones may just implement the phone interface. This results in a cleaner design and implementation.
  
 
== See Also ==  
 
== See Also ==  

Revision as of 06:57, 29 July 2009

Interface Segregation Principle, by Robert Martin says that clients should not be forced to depend upon interfaces that they do not use. Put more simply, many client specific interfaces are better than one general purpose interface. This principle prevents Fat interfaces surfacing in the design.

A good example of this principle is a modern cellphone which, in addition to a phone, is also a camera and an MP3 player. However some cellphones, especially older ones, do not have camera or MP3 player. Now imagine that we have an interface which provides access to the phone, camera and MP3 functionality. This would be quite a fat interface. If an older cellphone wishes to use this interface, the camera and MP3 functionality will appear to be present, but won't actually work.

A better way to solve this is to have small, distinct interfaces. In the case of our example we would have 3 interfaces for phone, camera and MP3 functionality. Now, modern cell phones can implement all three interfaces, while older cell phones may just implement the phone interface. This results in a cleaner design and implementation.

See Also

References

[ISP]

Personal tools