第二节:接口隔离原则

接口隔离原则(Interface Segregation Principle)

一、基本介绍

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

  2、先看一张图

    

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

   3、按隔离原则应当这样处理:将接口 Interface1 拆分为独立的几个接口,类 和类 分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则。

二、应用实例

  1、类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖 类 D。(未使用接口隔离)

    代码案例:

  1 public class Segregation1 {
  2     public static void main(String[] args) {
  3 
  4     }
  5 }
  6 
  7 //接口
  8 interface Interface1 {
  9     void operation1();
 10     void operation2();
 11     void operation3();
 12     void operation4();
 13     void operation5();
 14 }
 15 
 16 class B implements Interface1 {
 17 
 18     @Override
 19     public void operation1() {
 20         System.out.println("B 实现了 operation1");
 21     }
 22 
 23     @Override
 24     public void operation2() {
 25         System.out.println("B 实现了 operation2");
 26     }
 27 
 28     @Override
 29     public void operation3() {
 30         System.out.println("B 实现了 operation3");
 31     }
 32 
 33     @Override
 34     public void operation4() {
 35         System.out.println("B 实现了 operation4");
 36     }
 37 
 38     @Override
 39     public void operation5() {
 40         System.out.println("B 实现了 operation5");
 41     }
 42 }
 43 
 44 class D implements Interface1 {
 45 
 46     @Override
 47     public void operation1() {
 48         System.out.println("D 实现了 operation1");
 49     }
 50 
 51     @Override
 52     public void operation2() {
 53         System.out.println("D 实现了 operation2");
 54     }
 55 
 56     @Override
 57     public void operation3() {
 58         System.out.println("D 实现了 operation3");
 59     }
 60 
 61     @Override
 62     public void operation4() {
 63         System.out.println("D 实现了 operation4");
 64     }
 65 
 66     @Override
 67     public void operation5() {
 68         System.out.println("D 实现了 operation5");
 69     }
 70 }
 71 
 72 /**
 73  *  A 类通过接口 Interface1 依赖(使用)B类,但是只会用到1,2,3方法
 74  */
 75 class A {
 76 
 77     public void depend1(Interface1 i) {
 78         i.operation1();
 79     }
 80 
 81     public void depend2(Interface1 i) {
 82         i.operation2();
 83     }
 84 
 85     public void depend3(Interface1 i) {
 86         i.operation3();
 87     }
 88 }
 89 
 90 /**
 91  * C 类通过接口Interface1 依赖(使用) D类,但是只会用到1,4,5方法
 92  */
 93 class C {
 94     public void depend1(Interface1 i) {
 95         i.operation1();
 96     }
 97 
 98     public void depend4(Interface1 i) {
 99         i.operation4();
100     }
101 
102     public void depend5(Interface1 i) {
103         i.operation5();
104     }
105 }

  2、使用接口隔离原则改进

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

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

    (3)接口Interface1中出现的方法,根据实际情况拆分为三个接口。

       

       代码示例:

  1 public class Segregation2 {
  2     public static void main(String[] args) {
  3         A a = new A();
  4         a.depend1(new B()); // A类通过接口去依赖B类
  5         a.depend2(new B());
  6         a.depend3(new B());
  7 
  8         C c = new C();
  9 
 10         c.depend1(new D()); // C类通过接口去依赖(使用)D类
 11         c.depend4(new D());
 12         c.depend5(new D());
 13     }
 14 }
 15 
 16 //接口1
 17 interface Interface1 {
 18     void operation1();
 19 }
 20 
 21 //接口2
 22 interface Interface2 {
 23     void operation2();
 24     void operation3();
 25 }
 26 
 27 
 28 //接口3
 29 interface Interface3 {
 30     void operation4();
 31     void operation5();
 32 }
 33 
 34 
 35 class B implements Interface1, Interface2 {
 36 
 37     @Override
 38     public void operation1() {
 39         System.out.println("B 实现了 operation1");
 40     }
 41 
 42     @Override
 43     public void operation2() {
 44         System.out.println("B 实现了 operation2");
 45     }
 46 
 47     @Override
 48     public void operation3() {
 49         System.out.println("B 实现了 operation3");
 50     }
 51 }
 52 
 53 
 54 class D implements Interface1, Interface3 {
 55 
 56     @Override
 57     public void operation1() {
 58         System.out.println("D 实现了 operation1");
 59     }
 60 
 61     @Override
 62     public void operation4() {
 63         System.out.println("D 实现了 operation4");
 64     }
 65 
 66     @Override
 67     public void operation5() {
 68         System.out.println("D 实现了 operation5");
 69     }
 70 }
 71 
 72 /**
 73  * A 类通过接口Interface1,Interface2 依赖(使用) B类,但是只会用到1,2,3方法
 74  */
 75 class A {
 76     public void depend1(Interface1 i) {
 77         i.operation1();
 78     }
 79 
 80     public void depend2(Interface2 i) {
 81         i.operation2();
 82     }
 83 
 84     public void depend3(Interface2 i) {
 85         i.operation3();
 86     }
 87 }
 88 
 89 /**
 90  * C 类通过接口Interface1,Interface3 依赖(使用) D类,但是只会用到1,4,5方法
 91  */
 92 class C {
 93     public void depend1(Interface1 i) {
 94         i.operation1();
 95     }
 96 
 97     public void depend4(Interface3 i) {
 98         i.operation4();
 99     }
100 
101     public void depend5(Interface3 i) {
102         i.operation5();
103     }
104 }

    

三、总结

  一个类对另一个类的依赖应该建立在最小的接口上

原文地址:https://www.cnblogs.com/niujifei/p/14146734.html