Summary
Clientsshoshould not be forced to depend upon interfaces that they do not use.
Users cannot be forced to rely on interfaces they do not use. In other words, it is better to use multiple dedicated interfaces than to use a single total interface.
It contains two meanings:
-Interface design principle: the interface design should follow the minimum interface principle. Do not insert unused methods into the same interface.
If an interface method is not used, it indicates that the interface is too fat and should be divided into several specific functions.
-Interface dependency (inheritance) Principle: if an interface a depends on (inherits) Another interface B, interface A is equivalent to the method inherited from interface B, interface A inherited from interface B should also follow the above principle: it should not contain methods not used by users.
Otherwise, it indicates that interface A is contaminated by B and its relationship should be re-designed.
If users are forced to rely on interfaces they do not use, they also have to change when the interfaces change. In other words, a user is dependent on interfaces that are not used but used by other users. When other users modify this interface, all users dependent on this interface will be affected. This clearly violates the open and closed principle and is not what we expect.
The following is an example of how to design the relationship between interfaces or classes so that they do not violate the ISP principle.
Assume that there is a door with the lock and unlock functions. In addition, you can install an alarm on the door to enable the alarm function. Users can select either a common door or a door with the alarm function.
There are several design methods:
ISP violation examples:
Method 1:
Define all methods in the door interface. Figure:
But in this way, the commondoor that relies on the door interface has to implement the unused alarm () method. Violation of ISP principles.
Method 2:
The alarm interface defines the alarm method, the door interface defines the lock and Unlock methods, and the door interface inherits the alarm interface.
Like method 1, commondoor that relies on the door interface has to implement the unused alarm () method. Violation of ISP principles.
Example of ISP principles:
Method 3: implement multiple inheritance
The implementation of the adapter design pattern.
2nd) more practical solutions.
This design follows the ISP design principles.
Method 4: implemented through commissioning
Define the Alarm Method on the alarm interface and the lock and Unlock methods on the door interface. There is no inheritance relationship between interfaces. Commondoor implements the door interface,
Alarmdoor has two implementation solutions:
(1) implements both the door and alarm interfaces.
2) inherits commondoor and implements the alarm interface. This solution is inherited.
Summary
Interface segregation principle (ISP) establishes a benchmark for the granularity of abstraction for the interface from the use of the interface: when designing an interface for the system, multiple special interfaces are used to replace a single fat interface.