6大设计原则之接口隔离原则(Interface Segregation Principle)。
来源:互联网 发布:淘宝内部折扣网 编辑:程序博客网 时间:2024/06/05 22:44
接口分为两种。
- 实例接口(Object Interface),在Java中声明一个类,然后用new关键字产生一个实例,它是对一个类型的事物的描述,这是一种接口。比如你定义Person这个类,然后使用Person zhangSan = new Person()产生了一个实例,这个实例要遵从的标准就是Person这个类,Person类就是zhangSan的接口,Java中的类也是一种接口。
- 类接口(Class Interface),Java中经常使用的interface关键字定义的接口。
- 客户端不应该依赖它不需要的接口。
- 类间的依赖关系应该建立在最小的接口上。
两种类型的美女定义
public interface IGoodBodyGirl {
// 要有姣好的面孔
public void goodLooking();
// 要有好身材
public void niceFigure();
}
public interface IgreatTemperamentGirl {
// 要有气质
public void greatTemperament();
}
最标准的美女
public class PettyGirl implements IGoodBodyGirl, IGreatTemperamentGirl {
private String name;
// 美女都有名字
public PettyGirl(String _name) {
this.name = _name;
}
// 脸蛋漂亮
public void goodLooking() {
System.out.println(this.name + "---脸蛋很漂亮!");
}
// 气质要好
public void greatTemperament() {
System.out.println(this.name + "---气质非常好!");
}
// 身材要好
public void niceFigure() {
System.out.println(this.name + "---身材非常棒!");
}
}
接口隔离原则是对接口进行规范约束,其包含以下4层含义:
- 接口要尽量小
- 根据接口隔离原则拆分接口时,首先必须满足单一职责原则。
- 接口要高内聚
- 具体到接口隔离原则就是,要求在接口中尽量少公布public方法,接口是对外的承诺,承诺越少对系统的开发越有利,变更的风险也就越少,同时也有利于降低成本。
- 定制服务
- 定制服务就是单独为一个个体提供优良的服务。我们在做系统设计时也需要考虑对系统之间或模块之间的接口采用定制服务。采用定制服务就必然有一个要求:只提供访问者需要的方法。
- 接口设计是有限度的
最佳实践:
接口隔离原则是对接口的定义,同时也是对类的定义,接口和类尽量使用原子接口或原子类来组装。在实践中可以根据以下几个规则来衡量:
- 一个接口只服务于一个子模块或业务逻辑;
- 通过业务逻辑压缩接口中的public方法,接口时常去回顾,尽量让接口达到“满身筋骨肉”,而不是“肥嘟嘟”的一大堆方法;
- 已经被污染了的接口,尽量去修改,若变更的风险较大,则采用适配器模式进行转化处理;
- 了解环境,拒绝盲从。每个项目或产品都有特定的环境因素,别看到大师是这样做的你就照抄。环境不同,接口拆分的标准不同。深入了解业务逻辑。
- 设计模式6大原则之接口隔离原则(Interface Segregation Principle)
- 6大设计原则之接口隔离原则(Interface Segregation Principle)。
- 设计原则-接口隔离原则(Interface Segregation Principle)
- 接口隔离原则(Interface Segregation Principle)
- 接口隔离原则(Interface Segregation Principle)
- 接口隔离原则(Interface segregation principle)
- 设计模式六大设计原则之接口隔离原则(Interface Segregation Principle)
- 【六大设计原则】4. 接口隔离原则 -Interface Segregation Principle
- 六大设计原则--接口隔离原则【 Interface Segregation Principle】
- 软件设计原则(五)接口隔离原则 -Interface Segregation Principle
- IOS设计模式的六大设计原则之接口隔离原则(ISP,Interface Segregation Principle)
- 接口隔离原则(Interface Segregation Principle)
- 接口隔离原则(Interface Segregation Principle)
- 接口隔离原则【Interface Segregation Principle】
- ISP(Interface Segregation Principle),接口隔离原则
- 接口隔离原则【Interface Segregation Principle】
- Interface Segregation Principle 接口隔离原则
- 设计模式六大原则(4):接口隔离原则ISP(Interface Segregation Principle)
- [IOS APP]魔兽正史-魔兽世界有声小说
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 【第一周algorithm1】-minimum-depth-of-binary-tree
- 线程和进程的区别和联系
- 排序算法之基数排序
- 6大设计原则之接口隔离原则(Interface Segregation Principle)。
- 637. Average of Levels in Binary Tree
- maven 和 ant 比较
- 算法4 自我练习
- BroadCastReceiver详解
- 1017. A除以B (20)
- IPC机制
- OSI七层与TCP/IP五层网络架构详解
- Gold Balanced Lineup POJ