目錄: html
設計模式六大原則:單一職責原則設計模式
設計模式六大原則:接口隔離原則 ide
設計模式六大原則:迪米特法則code
接口隔離原則(Interface Segregation Principle):blog
一、客戶端不該依賴它不須要的接口接口
二、類間的依賴關係應該創建在最小的接口上ip
其實通俗來理解就是,不要在一個接口裏面放不少的方法,這樣會顯得這個類很臃腫。接口應該儘可能細化,一個接口對應一個功能模塊,同時接口裏面的方法應該儘量的少,使接口更加靈活輕便。或許有的人認爲接口隔離原則和單一職責原則很像,但兩個原則仍是存在着明顯的區別。單一職責原則是在業務邏輯上的劃分,注重的是職責。接口隔離原則是基於接口設計考慮。例如一個接口的職責包含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 }