The dependency of one class to another one should depend on the smallest possible interface.(一个类对另一个类的依赖应该取决于最小的可能接口)
Interface should be atomic, cohesive (内聚的), it presents an independent role, or provides independent services
Many client-specific interfaces(fine-grained interfaces,细粒度的接口) are better than one general purpose interface.
Clients should not be forced to implement unnecessary methods which they will not use.
Create an interface per client type not per client, avoid needless coupling to clients.
Interface Pollution,接口污染
Fat interface is interface pollution
Interface should be thin, it is also reasonable even there is no method defined in a interface.
Single-method-interface:Runnable
Fag interface (Indicate interface):Serializable
Saving the number of interfaces can not reduce the code-amount, but pollute the interfaces.(减少接口的数量并不能减少代码量,但会污染接口(减少接口数量意味着Fat Interface))
The intention of ISP is avoid the coupling among clients
ISP 是通过设计细粒度接口满足的:
Interface is the abstraction of the service contracts.
Service contracts is based on service requirements of clients.
Service requirements of different type of clients should be separated.
Different kind of requirements of one client should also be separated.
ISP 是 SRP 的 接口版本
二、一个不咋样的栗子
First try
package isp;/**
* @author 32098
*/publicinterfaceResultSet{/**
* first result in result set
*/publicvoidfirst();/**
* last result in result set
*/publicvoidlast();/**
* next result in result set
*/publicvoidnext();/**
* previous result in result set
*/publicvoidprevious();/**
* all results in result set
*/publicvoidgetAll();}
package isp;/**
* @author 32098
*/publicinterfaceSearchingEngine{/**
* re-index
*/publicvoidreIndex();/**
* update index
*/publicvoidupdateIndex();/**
* search
*/publicvoidsearch();/**
* get result set
* @param resultSet: Implement class of interface ResultSet
*/publicvoidgetResultSet(ResultSet resultSet);}
package isp;/**
* @author 32098
*/publicinterfaceResultSet{/**
* first result in result set
*/publicvoidfirst();/**
* last result in result set
*/publicvoidlast();/**
* next result in result set
*/publicvoidnext();/**
* previous result in result set
*/publicvoidprevious();/**
* all results in result set
*/publicvoidgetAll();}
package isp;/**
* @author 32098
*/publicinterfaceSearcher{/**
* search
*/publicvoidsearch();/**
* get result set
* @param resultSet: Implement class of interface ResultSet
*/publicvoidgetResultSet(ResultSet resultSet);}