0x03、設計模式原則 —— 介面隔離原則
概念
客戶端不應該依賴它不需要的介面,即:一個類對另一個類的依賴應該建立在最小的介面上;(比較難理解這句話,但不要緊,請繼續看下面)
演示
看下圖,Inerface1介面中有5個抽象方法,其中,B和D為 抽象介面Interface1 的 實現類,A和C依賴於抽象介面 Interface1:
上圖中,假設:類A 只用抽象類中的1、2、3三個方法,而4、5這兩個方法類A是不需要的,而B類只用抽象介面中的1、4、5三個方法,同樣2、3這兩個方法類B是不需要的,我們就說,客戶端A和C都有依賴了它們不需要的介面;違反了 介面隔離原則!
上圖中的程式碼的實現:
interface Interface1 {void operation1(); void operation2(); void operation3(); void operation4(); void operation5(); } public class B : Interface1 { public void operation1() { Console.WriteLine("實現類B中的func1方法"); } public void operation2() { Console.WriteLine("實現類B中的func2方法"); } public void operation3() { Console.WriteLine("實現類B中的func3方法"); } public void operation4() { Console.WriteLine("實現類B中的func4方法"); } public void operation5() { Console.WriteLine("實現類B中的func5方法"); } } public class D : Interface1 {public void operation1() { Console.WriteLine("實現類D中的func1方法"); } public void operation2() { Console.WriteLine("實現類D中的func2方法"); } public void operation3() { Console.WriteLine("實現類D中的func3方法"); } public void operation4() { Console.WriteLine("實現類D中的func4方法"); } public void operation5() { Console.WriteLine("實現類D中的func5方法"); } } class A { // 類 A 中,通過介面 Interface1 依賴(使用)類B,但是隻會用到 1、2、3 這兩個方法 public void depend1(Interface1 interf) { interf.operation1(); } public void depend2(Interface1 interf) { interf.operation2(); } public void depend3(Interface1 interf) { interf.operation3(); } } class C { // 類 C 中,通過介面 Interface1 依賴(使用)類D,但是隻會用到 1、4、5 這三個方法 public void depend1(Interface1 interf) { interf.operation1(); } public void depend4(Interface1 interf) { interf.operation4(); } public void depend5(Interface1 interf) { interf.operation5(); } }
上面程式碼中,類B 實現了介面中5個方法,而 類D 也實現了介面中的5個方法,但是 類A 中,我們並沒有用到 operation4 和 operation5 這兩個方法,同樣的,類C 中頁沒有用到 operation2 和 operation3 這兩個方法,既然沒用到,那實現類不是白寫了?
總結:上面程式碼中,類A通過介面Interface1依賴類B,類C通過介面Interface1依賴類D,如果介面Interface1對於類A和類C來說不是最小介面,那麼類B和類D必須去實現他們不需要的方法
按隔離原則應當這樣處理:將介面Interface1拆分為獨立的幾個介面,類A和類C分別與他們需要的介面建立依賴關係。也就是採用介面隔離原則!
改進:介面Interface1中出現的方法,根據實際情況拆分為三個介面:
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()); } interface Interface1 { void operation1(); } interface Interface2 { void operation2(); void operation3(); } interface Interface3 { void operation4(); void operation5(); } public class B : Interface1,Interface2 { public void operation1() { Console.WriteLine("實現類B中的 operation1 方法"); } public void operation2() { Console.WriteLine("實現類B中的 operation2 方法"); } public void operation3() { Console.WriteLine("實現類B中的 operation3 方法"); } } public class D : Interface1, Interface3 { public void operation1() { Console.WriteLine("實現類D中的 operation1 方法"); } public void operation4() { Console.WriteLine("實現類D中的 operation4 方法"); } public void operation5() { Console.WriteLine("實現類D中的 operation5 方法"); } } class A { // 類A 通過介面 Interfacel,Interface2 依賴(使用)類B,但是隻會用到1,2,3方法 public void depend1(Interface1 interf) { interf.operation1(); } public void depend2(Interface2 interf) { interf.operation2(); } public void depend3(Interface2 interf) { interf.operation3(); } } class C { // 類C 通過介面 Interfacel,Interface3 依賴(使用)類B,但是隻會用到1、4、5方法 public void depend1(Interface1 interf) { interf.operation1(); } public void depend4(Interface3 interf) { interf.operation4(); } public void depend5(Interface3 interf) { interf.operation5(); } }
說白了就是:一個類依賴的介面中有我不需要的方法,那麼我們需要將這個介面拆分成兩個或多個介面,這樣,拆分成多個介面之間就相當於隔離了,這就叫 介面隔離 ,我們依賴的才分後的介面就是最小了,這就叫:一個類對另一個類的依賴應該建立在最小的介面上;
即:我們依賴的介面中如果有用不到的成員,我們就將這個介面隔離,方式就是:拆,拆成我需要的介面,這種介面就是最小的;
這章其實不算難,如果有不懂的,歡迎評論區評論,我將會適度修改上面文章讓大家都能大白話的理解它;
下一章,我們將繼續深入:依賴倒置(倒轉)原則