Interface segregation principle
||This article is written like a personal reflection or opinion essay that states the Wikipedia editor's personal feelings about a topic, rather than the opinions of experts. (April 2010) (Learn how and when to remove this template message)|
The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them. Such shrunken interfaces are also called role interfaces. ISP is intended to keep a system decoupled and thus easier to refactor, change, and redeploy. ISP is one of the five SOLID principles of object-oriented design, similar to the High Cohesion Principle of GRASP.
Importance in object-oriented design
According to many software experts who have signed the Manifesto for Software Craftsmanship, writing well-crafted and self-explanatory software is almost as important as writing working software. Using interfaces to further describe the intent of the software is often a good idea.
A system may become so coupled at multiple levels that it is no longer possible to make a change in one place without necessitating many additional changes. Using an interface or an abstract class can prevent this side effect.
|This section does not cite any sources. (November 2013) (Learn how and when to remove this template message)|
The ISP was first used and formulated by Robert C. Martin while consulting for Xerox. Xerox had created a new printer system that could perform a variety of tasks such as stapling and faxing. The software for this system was created from the ground up. As the software grew, making modifications became more and more difficult so that even the smallest change would take a redeployment cycle of an hour, which made development nearly impossible.
The design problem was that a single Job class was used by almost all of the tasks. Whenever a print job or a stapling job needed to be performed, a call was made to the Job class. This resulted in a 'fat' class with multitudes of methods specific to a variety of different clients. Because of this design, a staple job would know about all the methods of the print job, even though there was no use for them.
The solution suggested by Martin utilized what is called the Interface Segregation Principle today. Applied to the Xerox software, an interface layer between the Job class and its clients was added using the Dependency Inversion Principle. Instead of having one large Job class, a Staple Job interface or a Print Job interface was created that would be used by the Staple or Print classes, respectively, calling methods of the Job class. Therefore, one interface was created for each job type, which were all implemented by the Job class.
The Xerox case is an example of a clear violation (and resolution) of the Interface Segregation Principle, but not all violations are so clear cut.
A more commonly known example is the ATM Transaction example given in Agile Software Development: Principles, Patterns, and Practices and in an article also written by Robert C. Martin specifically about the ISP. This example is about an interface for the User Interface for an ATM, that handles all requests such as a deposit request, or a withdrawal request, and how this interface needs to be segregated into individual and more specific interfaces.
- SOLID - The "I" in SOLID stands for Interface segregation principle
- Martin, Robert (2002). Agile Software Development: Principles, Patterns and Practices. Pearson Education.
- Role Interface
- David Hayden, Interface-Segregation Principle (ISP) - Principles of Object-Oriented Class Design
- Manifesto of Software Craftsmanship
- Robert C. Martin,The Interface Segregation Principle, C++ Report, June 1996
- Principles Of OOD – Description and links to detailed articles on SOLID.