面向对象设计模式之接口分离原则(ISP)

一、ISP 基本介绍

  • Interface Sepregation Principle(ISP),接口分离原则
    • 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
 */
public interface ResultSet {
    /**
     * first result in result set
     */
    public void first();

    /**
     * last result in result set
     */
    public void last();

    /**
     * next result in result set
     */
    public void next();

    /**
     * previous result in result set
     */
    public void previous();

    /**
     * all results in result set
     */
    public void getAll();
}
package isp;

/**
 * @author 32098
 */
public interface SearchingEngine {
    /**
     * re-index
     */
    public void reIndex();

    /**
     * update index
     */
    public void updateIndex();

    /**
     * search
     */
    public void search();

    /**
     * get result set
     * @param resultSet: Implement class of interface ResultSet
     */
    public void getResultSet(ResultSet resultSet);
}

package isp;

/**
 * @author 32098
 */
public class FileSearchingEngine implements SearchingEngine{
    @Override
    public void reIndex() {
        
    }

    @Override
    public void updateIndex() {

    }

    @Override
    public void search() {

    }

    @Override
    public void getResultSet(ResultSet resultSet) {

    }
}

package isp;

/**
 * @author 32098
 */
public class ClientA {
    // 需求实现只需要使用reIndex()与updateIndex()
    private FileSearchingEngine fileSearchingEngine;
}

package isp;

/**
 * @author 32098
 */
public class ClientB {
    // 需求实现只需要使用search()与getResultSet()
    private FileSearchingEngine fileSearchingEngine;
}

  • Second try
package isp;

/**
 * @author 32098
 */
public interface ResultSet {
    /**
     * first result in result set
     */
    public void first();

    /**
     * last result in result set
     */
    public void last();

    /**
     * next result in result set
     */
    public void next();

    /**
     * previous result in result set
     */
    public void previous();

    /**
     * all results in result set
     */
    public void getAll();
}
package isp;

/**
 * @author 32098
 */
public interface Indexer {
    /**
     * re-index
     */
    public void reIndex();

    /**
     * update index
     */
    public void updateIndex();
}
package isp;

/**
 * @author 32098
 */
public interface Searcher {
    /**
     * search
     */
    public void search();

    /**
     * get result set
     * @param resultSet: Implement class of interface ResultSet
     */
    public void getResultSet(ResultSet resultSet);
}
package isp;

/**
 * @author 32098
 */
public class FileIndexer implements Indexer{

    @Override
    public void reIndex() {

    }

    @Override
    public void updateIndex() {

    }
}
package isp;

/**
 * @author 32098
 */
public class FileSearcher implements Searcher{
    @Override
    public void search() {

    }

    @Override
    public void getResultSet(ResultSet resultSet) {

    }
}

package isp;

/**
 * @author 32098
 */
public class ClientA {
    private FileIndexer fileIndexer;
    // private FileSearcher fileSearcher;
}
package isp;

/**
 * @author 32098
 */
public class ClientB {
    // private FileIndexer fileIndexer;
    private FileSearcher fileSearcher;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值