1. Interface Segregation Principle (ISP: Interface Segregation Principle)
Definition: It is better to use multiple specialized interfaces than to use a single general interface. It can also be said: Build a single interface, don't build a bloated interface.
Two definitions of ISP:
◇ “Clients should not be forced to depend upon interfaces that they don't use”
A client should not rely on interfaces it does not need.
◇ “The dependency of one class to another one should depend on the smallest possible interface”
The dependence of one class on another class should be based on the smallest interface. (from client)
Interfaces and roles: Understanding an interface as a collection of characteristics of all methods provided by a class. In this way, the division of interfaces brings about the division of types. So an interface should represent only one role, rather than serving multiple roles.
Interface Contamination: The so-called interface contamination is adding unnecessary responsibilities to the interface. Interface pollution will bring maintenance and reuse problems. The most common problem is that in order to reuse the polluted interface, we are forced to implement and maintain unnecessary methods. Therefore, we must separate the client program, and separating the client program means separating the interface.
How to implement separate interfaces:
There are generally two ways to separate interfaces:
1) Use delegation to separate interfaces. (Separation through Delegation)
Just delegate the request to the implementation class of other interfaces to complete the required responsibilities, which is the adapter pattern (Adapter).
2) Use multiple inheritance to separate interfaces. (Separation through Multiple Inheritance.)
This method implements multiple interfaces to complete the required responsibilities.
Both methods have their own advantages and disadvantages. Usually we should consider the latter option first, and choose the former option if type conversion is involved.
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/scelong/archive/2009/12/18/5031030.aspx
-