設計模式七大原則之接口隔離

什麼是接口隔離原則?

對於類的接口依賴,客戶端不應該依賴它不需要的接口,即一個類對另一個類的依賴應該建立在最小接口上

爲什麼要設計接口隔離

(1) 降低了耦合度
(2) 可以達到代碼"瘦身效果"

案例演示 - 非接口隔離原則

可以先看下方的實現圖

在這裏插入圖片描述
A類通過接口Interface01依賴於B類。C類通過接口Interface01依賴於D類。如上圖所示Interface01接口對於A類和C類都不是最小接口,那麼B類和D類也必須要去實現它們不需要的方法, 這樣就造成了代碼耦合度很高而且很冗餘。

public class Normal {
    public static void main(String[] args) {
        A a = new A();
        a.operation01(new B());
        a.operation02(new B());
        a.operation03(new B());
        
        C c = new C();
        c.operation01(new D());
        c.operation04(new D());
        c.operation05(new D());
    }
}

interface Interface01 {
    void operation01();
    void operation02();
    void operation03();
    void operation04();
    void operation05();
}

/***
 * A類會使用到B的依賴,但是隻會調用operation01()、operation02()、operation03()
 */
class B implements Interface01{

    @Override
    public void operation01() {
        System.out.println("B 實現了 operation01()");
    }

    @Override
    public void operation02() {
        System.out.println("B 實現了 operation02()");
    }

    @Override
    public void operation03() {
        System.out.println("B 實現了 operation03()");
    }

    @Override
    public void operation04() {
        System.out.println("B 實現了 operation04()");
    }

    @Override
    public void operation05() {
        System.out.println("B 實現了 operation05()");
    }
}

/***
 * C類會使用到D類的依賴,但是隻會調用到operation01()、operation04()、operation05()
 */
class D implements Interface01{
    @Override
    public void operation01() {
        System.out.println("D 實現了 operation01()");
    }

    @Override
    public void operation02() {
        System.out.println("D 實現了 operation02()");
    }

    @Override
    public void operation03() {
        System.out.println("D 實現了 operation03()");
    }

    @Override
    public void operation04() {
        System.out.println("D 實現了 operation04()");
    }

    @Override
    public void operation05() {
        System.out.println("D 實現了 operation05()");
    }
}

class A {
    public void operation01(Interface01 interface01) {
        interface01.operation01();
    }

    public void operation02(Interface01 interface01) {
        interface01.operation02();
    }

    public void operation03(Interface01 interface01) {
        interface01.operation03();
    }
}

class C {
    public void operation01(Interface01 interface01) {
        interface01.operation01();
    }

    public void operation04(Interface01 interface01) {
        interface01.operation04();
    }

    public void operation05(Interface01 interface01) {
        interface01.operation05();
    }
}

案例演示 - 接口隔離原則

可以先看下方實現圖

在這裏插入圖片描述
可以將接口Interface01拆分爲獨立的幾個接口,這裏可以拆分爲三個獨立的接口。A類和C類分別於它們需要的接口建立依賴關係。也就是採用接口隔離。

public class InterfaceQuarantine {
    public static void main(String[] args) {
        A a = new A();
        a.operation01(new B());
        a.operation02(new B());
        a.operation03(new B());

        C c = new C();
        c.operation01(new D());
        c.operation04(new D());
        c.operation05(new D());
    }
}

interface Interface01 {
    void operation01();
}

interface Interface02 {
    void operation02();
    void operation03();
}

interface Interface03 {
    void operation04();
    void operation05();
}

class B implements Interface01, Interface02 {

    @Override
    public void operation01() {
        System.out.println("B 實現了operation01()");
    }

    @Override
    public void operation02() {
        System.out.println("B 實現了operation02()");
    }

    @Override
    public void operation03() {
        System.out.println("B 實現了operation03()");
    }
}

class D implements Interface01, Interface03 {

    @Override
    public void operation01() {
        System.out.println("D 實現了operation01()");
    }

    @Override
    public void operation04() {
        System.out.println("D 實現了operation04()");
    }

    @Override
    public void operation05() {
        System.out.println("D 實現了operation05()");
    }
}

class A {
    public void operation01(Interface01 interface01) {
        interface01.operation01();
    }

    public void operation02(Interface02 interface02) {
        interface02.operation02();
    }

    public void operation03(Interface02 interface02) {
        interface02.operation03();
    }
}

class C {
    public void operation01(Interface01 interface01) {
        interface01.operation01();
    }

    public void operation04(Interface03 interface03) {
        interface03.operation04();
    }

    public void operation05(Interface03 interface03) {
        interface03.operation05();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章