设计模式六大原则:接口隔离原则
目录:
接口隔离原则(Interface Segregation Principle):
1、客户端不应依赖它不需要的接口
2、类间的依赖关系应该建立在最小的接口上
其实通俗来理解就是,不要在一个接口里面放很多的方法,这样会显得这个类很臃肿。接口应该尽量细化,一个接口对应一个功能模块,同时接口里面的方法应该尽可能的少,使接口更加灵活轻便。或许有的人认为接口隔离原则和单一职责原则很像,但两个原则还是存在着明显的区别。单一职责原则是在业务逻辑上的划分,注重的是职责。接口隔离原则是基于接口设计考虑。例如一个接口的职责包含10个方法,这10个方法都放在同一接口中,并且提供给多个模块调用,但不同模块需要依赖的方法是不一样的,这时模块为了实现自己的功能就不得不实现一些对其没有意义的方法,这样的设计是不符合接口隔离原则的。接口隔离原则要求"尽量使用多个专门的接口"专门提供给不同的模块。
经典案例:
类A通过Interface1依赖类B,1,2,3;类B通过Interface1依赖D,1,4,5。
1 internal class Program 2 { 3 private static void Main(string[] args) 4 { 5 A a = new A(); 6 B b = new B(); 7 a.use1(b); 8 a.use2(b); 9 a.use3(b); 10 } 11 } 12 13 internal interface interface1 14 { 15 void operation1(); 16 17 void operation2(); 18 19 void operation3(); 20 21 void operation4(); 22 23 void operation5(); 24 } 25 26 internal class B : interface1 27 { 28 public void operation1() 29 { 30 Console.WriteLine($"B->{nameof(operation1)}"); 31 } 32 33 public void operation2() 34 { 35 Console.WriteLine($"B->{nameof(operation2)}"); 36 } 37 38 public void operation3() 39 { 40 Console.WriteLine($"B->{nameof(operation3)}"); 41 } 42 43 public void operation4() 44 { 45 Console.WriteLine($"B->{nameof(operation4)}"); 46 } 47 48 public void operation5() 49 { 50 Console.WriteLine($"B->{nameof(operation5)}"); 51 } 52 } 53 54 internal class D : interface1 55 { 56 public void operation1() 57 { 58 Console.WriteLine($"D->{nameof(operation1)}"); 59 } 60 61 public void operation2() 62 { 63 Console.WriteLine($"D->{nameof(operation2)}"); 64 } 65 66 public void operation3() 67 { 68 Console.WriteLine($"D->{nameof(operation3)}"); 69 } 70 71 public void operation4() 72 { 73 Console.WriteLine($"D->{nameof(operation4)}"); 74 } 75 76 public void operation5() 77 { 78 Console.WriteLine($"D->{nameof(operation5)}"); 79 } 80 } 81 82 internal class A 83 { 84 public void use1(interface1 interface1) 85 { 86 interface1.operation1(); 87 } 88 89 public void use2(interface1 interface1) 90 { 91 interface1.operation2(); 92 } 93 94 public void use3(interface1 interface1) 95 { 96 interface1.operation3(); 97 } 98 } 99 100 internal class C 101 { 102 public void use1(interface1 interface1) 103 { 104 interface1.operation1(); 105 } 106 107 public void use4(interface1 interface1) 108 { 109 interface1.operation4(); 110 } 111 112 public void use5(interface1 interface1) 113 { 114 interface1.operation5(); 115 } 116 }
显然,上述设计不符合接口隔离原则。
1 internal class Program 2 { 3 private static void Main(string[] args) 4 { 5 A a = new A(); 6 B b = new B(); 7 a.use1(b); 8 a.use2(b); 9 a.use3(b); 10 } 11 } 12 13 internal interface interface1 14 { 15 void operation1(); 16 } 17 18 internal interface interface2 19 { 20 void operation2(); 21 22 void operation3(); 23 } 24 25 internal interface interface3 26 { 27 void operation4(); 28 29 void operation5(); 30 } 31 32 internal class B : interface1, interface2 33 { 34 public void operation1() 35 { 36 Console.WriteLine($"B->{nameof(operation1)}"); 37 } 38 39 public void operation2() 40 { 41 Console.WriteLine($"B->{nameof(operation2)}"); 42 } 43 44 public void operation3() 45 { 46 Console.WriteLine($"B->{nameof(operation3)}"); 47 } 48 } 49 50 internal class D : interface1, interface3 51 { 52 public void operation1() 53 { 54 Console.WriteLine($"D->{nameof(operation1)}"); 55 } 56 57 public void operation4() 58 { 59 Console.WriteLine($"D->{nameof(operation4)}"); 60 } 61 62 public void operation5() 63 { 64 Console.WriteLine($"D->{nameof(operation5)}"); 65 } 66 } 67 68 internal class A 69 { 70 public void use1(interface1 interface1) 71 { 72 interface1.operation1(); 73 } 74 75 public void use2(interface2 interface2) 76 { 77 interface2.operation2(); 78 } 79 80 public void use3(interface2 interface2) 81 { 82 interface2.operation3(); 83 } 84 } 85 86 internal class C 87 { 88 public void use1(interface1 interface1) 89 { 90 interface1.operation1(); 91 } 92 93 public void use4(interface3 interface3) 94 { 95 interface3.operation4(); 96 } 97 98 public void use5(interface3 interface3) 99 { 100 interface3.operation5(); 101 } 102 }