issues
search
ThomsonTang
/
techbook.thomsontang.com
output the card notes from technical books.
0
stars
0
forks
source link
The Zen of Design Patterns
#3
Open
ThomsonTang
opened
7 years ago
ThomsonTang
commented
7 years ago
接口隔离原则
术语卡——接口
术语:接口(interface)
印象:包括如下两种:
实例接口(Object Interface):用来产生具体的对象实例的接口。在Java中就是指声明一个类,然后使用
new
关键字创建该类的一个
对象实例
。那么这个类就可以看做是这个对象实例的接口。
类接口(Class Interface):这个在Java中就是指用关键字
interface
定义的接口。
例子:忽略
出处:
《原创精品系列:设计模式之禅(第2版)》(秦小波)
术语卡——接口隔离原则
术语: 接口隔离原则(interface segregation principle)
印象:
Clients should not be forced to depend upon interfaces that they don’t use.
The dependency of one class to another one should depend on the smallest possible interfaces.
例子:
不可强迫客户端依赖他不需要的接口:也就是说
客户端需要什么接口,我们就给提供什么接口
,不需要的就直接剔除掉,通过这种对接口的细化,就保证了接口的
纯洁性
。
类之间的依赖关系应建立在最小的接口上:也就是说
尽量最小化接口
,只把依赖的关系定义在一个专用的接口中提供,该接口不需要包括额外的其他方法。
出处:
Interface segregation principle - Wikipedia
反常识卡——ISP和SRP的区别
反常识:ISP和SRP看待设计的角度是不一样的。SRP关注的是类或者接口的职责,它只强调职责单一,并不关心这个类或者接口对外提供依赖或者方法时是否是纯洁的,是否提供了一些多余的方法。而ISP关注的则是接口中定义的方法是否足够少。
印象:在实际的编码设计中,常常将两者混淆。
例子:假设一个接口有某项职责,该职责是单一的,但是实现该职责需要定义10个方法,这10个方法都放在同一个接口中,并同时提供给外部的多个模块访问,各个模块按照权限不同访问不同的方法。从SRP的角度来看,这种场景是符合其原则的,但是从ISP的关注点来看,则是违背其原则的。
出处:
《原创精品系列:设计模式之禅(第2版)》(秦小波)
金句卡
专门的接口就是指提供给每个模块的都应该是单一接口,提供给几个模块就应该有几个接口,而不是建立一个庞大臃肿的接口,容纳所有的客户端访问。
接口隔离原则
术语卡——接口
new
关键字创建该类的一个对象实例。那么这个类就可以看做是这个对象实例的接口。interface
定义的接口。术语卡——接口隔离原则
反常识卡——ISP和SRP的区别
金句卡