Java设计模式之接口隔离原则精解

作者:张起灵-小哥 时间:2022-05-05 16:42:05 

1.什么是接口隔离原则?

客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口范围上。

2.对应代码

Java设计模式之接口隔离原则精解

上面这张图呢,就违反了接口隔离原则。它对应的代码如下:👇👇👇


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.改进代码

Java设计模式之接口隔离原则精解

经过上面的一番叙述,我们可以将代码改写成下面的形式。

即将顶级接口拆分成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());
   }
}

Java设计模式之接口隔离原则精解

4.接口隔离原则总结

  1. 类A通过接口Interface1依赖类B,类C通过接口Interfacel依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法。

  2. 将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则。

来源:https://blog.csdn.net/weixin_43823808/article/details/122790319

标签:Java,接口隔离原则,设计模式
0
投稿

猜你喜欢

  • Java并发的CAS原理与ABA问题的讲解

    2023-11-25 12:17:21
  • Flutter 状态管理的实现

    2023-08-21 02:38:33
  • OpenCV实现人脸识别简单程序

    2023-07-07 00:31:12
  • SpringMVC结合天气api实现天气查询

    2021-06-01 16:56:41
  • java控制台实现学生信息管理系统(集合版)

    2023-11-11 14:16:52
  • C# WPF数据绑定模板化操作的完整步骤

    2023-05-20 15:44:50
  • Java判断字符串是否是整数或者浮点数的方法

    2022-04-30 10:06:20
  • 微信开发之使用java获取签名signature

    2022-08-01 10:47:01
  • SpringBoot集成Jpa对数据进行排序、分页、条件查询和过滤操作

    2022-03-06 19:17:50
  • springboot bootstrap.yml nacos配置中心问题

    2022-07-07 08:25:01
  • Java开发人员需知的十大戒律

    2023-09-17 07:33:50
  • Java实例讲解Comparator的使用

    2021-07-15 09:16:58
  • 详解Kotlin和anko融合进行Android开发

    2021-08-15 19:34:16
  • 简单了解redis常见客户端及Sharding机制原理

    2022-03-28 16:49:09
  • SpringBoot @PropertySource与@ImportResource有什么区别

    2023-08-22 02:02:47
  • Spring Boot Admin实践详解

    2023-08-25 06:57:53
  • 浅谈Java(SpringBoot)基于zookeeper的分布式锁实现

    2023-11-16 08:14:56
  • java获取ip地址示例

    2021-12-25 07:04:22
  • Java枚举学习之定义和基本特性详解

    2022-07-23 20:29:44
  • SpringBoot快速配置数据源的方法

    2023-07-28 13:22:42
  • asp之家 软件编程 m.aspxhome.com