设计模式六大原则——接口隔离原则(ISP,Interface Segregation Principle)
来源:互联网 发布:浴缸尺寸 知乎 编辑:程序博客网 时间:2024/05/21 06:27
转自百度百科 http://baike.baidu.com/view/1638378.htm
ISP(Interface Segregation Principle)简介
- 使用多个专门的接口比使用单一的总接口要好。
- 一个类对另一个类的依赖性应当是建立在最小的接口上的。
- 一个接口代表一个角色,不应当将不同的角色都交给一个接口。没有关系的接口合并在一起,会形成一个臃肿的大接口,这是对角色和接口的污染。
- “不应该强迫客户依赖于它们不用的方法。接口属于客户,不属于它所在的类层次结构。”这个说的很明白了,再通俗点就是——不要强迫客户使用它们不用的方法。如果强迫用户使用它们不用的方法,那么这些客户就会面临由于这些不使用的方法的改变所带来的改变。
举例说明
使用场合,提供调用者需要的方法,屏蔽不需要的方法。比如说电子商务的系统,有订单这个类,就有三个地方会使用到:
- 一个是门户,只能有查询方法
- 一个是外部系统,有添加订单的方法
- 一个是后台管理系统,增删改查都要用到
根据接口隔离原则(ISP),一个类对另外一个类的依赖性应当是建立在最小的接口上。也就是说,对于门户,它只能依赖有一个查询方法的接口。
这儿不用接口继承,因为可能出现修改了父接口影响了子接口。
下面是实现代码
interface IOrderForPortal{ String getOrder();}interface IOrderForOtherSys{ String insertOrder(); String getOrder();}interface IOrderForAdmin{ //extendsIOrderForPortal,IOrderForOtherSys String deleteOrder(); String updateOrder(); String insertOrder(); String getOrder();}/*interface IOrderForPortal{String getOrder();}interface IOrderForOtherSys{String insertOrder();}interface IOrderForAdmin extendsIOrderForPortal,IOrderForOtherSys{String updateOrder();String deleteOrder();}*/class Order implements IOrderForPortal,IOrderForOtherSys,IOrderForAdmin{ private Order(){ //--什么都不干,就是为了不让直接 new,防止客户端直接New,然后访问它不需要的方法. } //返回给Portal public static IOrderForPortal getOrderForPortal(){ return (IOrderForPortal)new Order(); } //返回给OtherSys public static IOrderForOtherSys getOrderForOtherSys(){ return (IOrderForOtherSys)newOrder(); } //返回给Admin public static IOrderForAdmin getOrderForAdmin(){ return (IOrderForAdmin)new Order(); } //--下面是接口方法的实现.只是返回了一个String用于演示 public String getOrder(){ return "implemented getOrder"; } public String insertOrder(){ return "implementedinsertOrder"; } public String updateOrder(){ return "implementedupdateOrder"; } public String deleteOrder(){ return "implementeddeleteOrder"; }}public class TestCreateLimit{ public static void main(String[] args){ IOrderForPortal orderForPortal =Order.getOrderForPortal(); IOrderForOtherSys orderForOtherSys =Order.getOrderForOtherSys(); IOrderForAdmin orderForAdmin = Order.getOrderForAdmin(); System.out.println("Portal门户调用方法:"+orderForPortal.getOrder()); System.out.println("OtherSys外部系统调用方法:"+orderForOtherSys.insertOrder()); System.out.println("Admin管理后台调用方法:"+orderForAdmin.getOrder()+";"+orderForAdmin.insertOrder()+";"+orderForAdmin.updateOrder()+";"+orderForAdmin.deleteOrder()); }}
总结
客户端不应该依赖它不需要的接口;一个类对另一个类的依赖应该建立在最小的接口上。
1 0
- 设计模式六大原则——接口隔离原则(ISP,Interface Segregation Principle)
- 设计模式六大原则(4):接口隔离原则ISP(Interface Segregation Principle)
- IOS设计模式的六大设计原则之接口隔离原则(ISP,Interface Segregation Principle)
- 设计模式六大设计原则之接口隔离原则(Interface Segregation Principle)
- ISP简介(ISP--Interface Segregation Principle)接口隔离原则
- ISP(Interface Segregation Principle),接口隔离原则
- 设计模式六大原则(4):接口隔离原则(Interface Segregation Principle)
- 设计模式六大原则(4)接口隔离原则(Interface Segregation Principle)
- 【六大设计原则】4. 接口隔离原则 -Interface Segregation Principle
- 六大设计原则--接口隔离原则【 Interface Segregation Principle】
- 设计模式原则(4)--Interface Segregation Principle(ISP)--接口隔离原则
- ISP( Interface Segregation Principle)接口隔离原则
- 接口隔离原则(Interface Segregation Principle,ISP)
- 接口隔离原则(ISP--Interface Segregation Principle)
- 接口隔离原则(ISP--Interface Segregation Principle)
- 接口隔离原则(ISP - Interface Segregation Principle)
- "围观"设计模式(4)--接口隔离原则(ISP,Interface Segregation Principle)
- "围观"设计模式(4)--接口隔离原则(ISP,Interface Segregation Principle)
- 据说年薪30万的Android程序员必须知道的帖子
- 命名空间
- [**80C51单片机实现交通灯**]
- yii2实现弹框
- jquery $.post请求捕获异常
- 设计模式六大原则——接口隔离原则(ISP,Interface Segregation Principle)
- C# LIST和STRING互相转换
- WIN组合键操作大全
- MyEclipse2014用maven搭建web项目
- 如何提高商标注册成功率?
- 读you don't know js 有感之作用域
- jquery设置css样式、style属性 示例
- 各种命令
- ZipControl对文件进行压缩与解压的方法