Java设计模式之接口隔离原则精解
作者:张起灵-小哥 发布时间:2022-05-05 16:42:05
标签:Java,接口隔离原则,设计模式
1.什么是接口隔离原则?
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口范围上。
2.对应代码
上面这张图呢,就违反了接口隔离原则。它对应的代码如下:👇👇👇
package com.szh.principle.segregation;
/**
*
*/
interface Interface1 {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class B implements Interface1 {
public void operation1() {
System.out.println("B 实现了 operation1");
}
public void operation2() {
System.out.println("B 实现了 operation2");
}
public void operation3() {
System.out.println("B 实现了 operation3");
}
public void operation4() {
System.out.println("B 实现了 operation4");
}
public void operation5() {
System.out.println("B 实现了 operation5");
}
}
class D implements Interface1 {
public void operation1() {
System.out.println("D 实现了 operation1");
}
public void operation2() {
System.out.println("D 实现了 operation2");
}
public void operation3() {
System.out.println("D 实现了 operation3");
}
public void operation4() {
System.out.println("D 实现了 operation4");
}
public void operation5() {
System.out.println("D 实现了 operation5");
}
}
class A { //A 类通过接口Interface1 依赖(使用) B类,但是只会用到1,2,3方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface1 i) {
i.operation2();
}
public void depend3(Interface1 i) {
i.operation3();
}
}
class C { //C 类通过接口Interface1 依赖(使用) D类,但是只会用到1,4,5方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface1 i) {
i.operation4();
}
public void depend5(Interface1 i) {
i.operation5();
}
}
public class Segregation1 {
public static void main(String[] args) {
A a = new A();
a.depend1(new B()); // A类通过接口去依赖B类
a.depend2(new B());
a.depend3(new B());
C c = new C();
c.depend1(new D()); // C类通过接口去依赖(使用)D类
c.depend4(new D());
c.depend5(new D());
}
}
代码虽然很长,但是不难理解。A类依赖了B类,但是只会用到顶级接口中的1、2、3这三个方法;而C类依赖了D类,但是只会用到顶级接口中的1、4、5这三个方法,也就是说在A和B这两个类的层面上而言,和顶级接口中的4、5两个方法是没什么关联的,那么B类在实现顶级接口的时候就没必要重写4、5这两个方法了。但是这里有一个问题就是顶级接口中包括了1到5这五个方法,你如果实现这个接口就必须重写这五个方法,那么我们就可以考虑将顶级接口拆分成多个接口,需要用到哪个就实现哪个,这也就是所谓的接口隔离了。
3.改进代码
经过上面的一番叙述,我们可以将代码改写成下面的形式。
即将顶级接口拆分成3个小接口,B、D两个类根据实际情况该实现哪个接口就实现哪个接口(因为这五个方法已经被分开了)。
package com.szh.principle.segregation.improve;
/**
*
*/
interface Interface1 {
void operation1();
}
interface Interface2 {
void operation2();
void operation3();
}
interface Interface3 {
void operation4();
void operation5();
}
class B implements Interface1, Interface2 {
public void operation1() {
System.out.println("B 实现了 operation1");
}
public void operation2() {
System.out.println("B 实现了 operation2");
}
public void operation3() {
System.out.println("B 实现了 operation3");
}
}
class D implements Interface1, Interface3 {
public void operation1() {
System.out.println("D 实现了 operation1");
}
public void operation4() {
System.out.println("D 实现了 operation4");
}
public void operation5() {
System.out.println("D 实现了 operation5");
}
}
class A { // A 类通过接口Interface1,Interface2 依赖(使用) B类,但是只会用到1,2,3方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface2 i) {
i.operation2();
}
public void depend3(Interface2 i) {
i.operation3();
}
}
class C { // C 类通过接口Interface1,Interface3 依赖(使用) D类,但是只会用到1,4,5方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface3 i) {
i.operation4();
}
public void depend5(Interface3 i) {
i.operation5();
}
}
public class Segregation2 {
public static void main(String[] args) {
A a = new A();
a.depend1(new B()); // A类通过接口去依赖B类
a.depend2(new B());
a.depend3(new B());
C c = new C();
c.depend1(new D()); // C类通过接口去依赖(使用)D类
c.depend4(new D());
c.depend5(new D());
}
}
4.接口隔离原则总结
类A通过接口Interface1依赖类B,类C通过接口Interfacel依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法。
将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则。
来源:https://blog.csdn.net/weixin_43823808/article/details/122790319
0
投稿
猜你喜欢
- 前言Java 8 提供了一组称为 stream 的 API,用于处理可遍历的流式数据。stream API 的设计,充分融合了函数式编程的理
- spring boot ${}占位符不起作用问题:在 pom.xml 文件里定义好属性标签,然后在 properties或者xml 中使用$
- 将此实例的子字符串中所有指定字符的匹配项替换为其他指定字符。 命名空间:System.Text 程序集:mscorl
- 前言HTML5 WebSocket实现了服务器与浏览器的双向通讯,双向通讯使服务器消息推送开发更加简单,最常见的就是即时通讯和对信息实时性要
- 1、使用 ctrl+F12打开类中所有方法的界面2、然后直接键盘中输入方法名称或者方法包含的字母,会自动模糊匹配相关方法名补充:idea快速
- 前面讲述了使用POI导出Word文件和读取Excel文件,这两个例子都相对简单,接下来要讲述的使用POI导出Excel文件要复杂得多,内容也
- 1.map遍历快速实现边距,文字自适应改变大小Container( // padding: EdgeI
- 在后台工程师开发完新代码交给QA进行测试时,软件测试人员一般都会要求后台开发对单元测试的覆盖率达到一定的标准;例如我们的标准是分支覆盖率达到
- 1、通过查找API文档:2、Map.Entry是一个接口,所以不能直接实例化。3、Map.entrySet( )返回的是一个collecti
- RFC6749OAuth2的官方文档在RFC6749:https://datatracker.ietf.org/doc/html/rfc67
- 前言数独是一种有趣的智力游戏,但是部分高难度数独在求解过程中经常出现大量单元格有多个候选数字可以填入,不得不尝试填写某个数字然后继续推导的方
- 前言用过Spring的人多多少少也都用过@Async注解,至于作用嘛,看注解名,大概能猜出来,就是在方法执行的时候进行异步执行。一、如何使用
- 消息都是存放在一个消息队列中去,而消息循环线程就是围绕这个消息队列进入一个无限循环的,直到线程退出。如果队列中有消息,消息循环线程就会把它取
- 该配置基于IDEA2020.1版本,如后续有版本更新或者配置变更,再更新idea64.exe.vmoptions配置为提供IDEA启动速度和
- 前言前面说到在我们应对高并发的场景,请求量过于大的情况下给我们服务器的压力很多,造成缓存穿透、击穿、雪崩,那么我们采用布隆过滤器,有兴趣的小
- SpringBoot默认的页面映射路径(即模板文件存放的位置)为“classpath:/templates/*.html”。静态文件路径为“
- 本篇主要描述“发送邮箱验证码、session校验”相关前(html\js)后(java)台代码,业务逻辑示例,闲话少诉,直接上代码。1、引入
- 一、一级缓存二级缓存的概念解释(1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓
- ThymeleafThymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里。我们为什么要用Thy
- 第一步:下载需要添加的jar包可以在maven库中查找下载,也可以在对应官网下载maven库网址:https://mvnrepository