接口隔离原则(Interface Segregation Principle)
来源:互联网 发布:淘宝客服三天总结 编辑:程序博客网 时间:2024/04/29 10:35
接口隔离原则指出:
CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE.
首先介绍接口污染。
一个没有经验的设计师往往想节省接口的数目, 将一些功能相近或功能相关的接口合并, 并将这看成是代码优化的一部分。定义:从一个客户类的角度来讲:一个类对另外一个类的依赖性应当是建立在最小的接口上的。 使用多个专门的接口比使用单一的总接口要好。
比如,一个Door接口具有了lock、unlock的功能,另外一个门的对象需要监听超时时间,如果一个门开的时间太长,要自动关闭,错误的做法就是在Door接口中加入超时部分的代码。但是这样做以后,就会使得所有实现Door接口的对象都有了超时部分的功能,如果不需要就要提供null实现。一个对象从接口中获取了自己不需要的方法或者属性,并使得原来的Door接口变得越来越fat,这就是接口污染。
Since the clients are separate, the interfaces should remain separate too.
解决的办法有两个:
1. Separation through Delegation
就是适配器模式(Adapter)
2. Separation through Multiple Inheritance
使用多个专门的接口比使用单一的总接口要好。
参考:
1.《Java与模式》,阎宏编著
2.《The Interface Segregation Principle》,by Robert C. Martin
- 接口隔离原则(Interface Segregation Principle)
- 接口隔离原则(Interface Segregation Principle)
- 接口隔离原则(Interface Segregation Principle)
- 接口隔离原则【Interface Segregation Principle】
- 接口隔离原则(Interface Segregation Principle)
- ISP(Interface Segregation Principle),接口隔离原则
- 接口隔离原则【Interface Segregation Principle】
- Interface Segregation Principle 接口隔离原则
- 接口隔离原则(Interface segregation principle)
- 【六大设计原则】4. 接口隔离原则 -Interface Segregation Principle
- 六大设计原则--接口隔离原则【 Interface Segregation Principle】
- 设计原则-接口隔离原则(Interface Segregation Principle)
- 软件设计原则(五)接口隔离原则 -Interface Segregation Principle
- ISP( Interface Segregation Principle)接口隔离原则
- 接口隔离原则(Interface Segregation Principle,ISP)
- 接口隔离原则(ISP--Interface Segregation Principle)
- ISP简介(ISP--Interface Segregation Principle)接口隔离原则
- 接口隔离原则(ISP--Interface Segregation Principle)
- Map.Entry 和 Map.entrySet() ,用类遍历hashMap,hashTable
- 编写完成端口网络服务器的一些说明 (1)
- [转载] MFC程序的来龙去脉
- Web 应用程序的访问权限
- 为什么要用嵌套类,内部类?
- 接口隔离原则(Interface Segregation Principle)
- 利用Oracle执行计划机制提高查询性能
- Delphi出现“Unsatisfied forward or external declaration”错误提示
- debian etch 简要安装指南
- GDI+介绍
- 让WebWork遍历Map
- KGDB-Serial setup steps : kgdb-2.4patches,Linux- 2.6.16 kernel
- JNI详解
- 串口伪驱动实例