接口隔離原則(Interface Segregation Principle)

1. 概念

客戶端不應(yīng)該依賴它不需要的接口,即一個(gè)類對(duì)另一個(gè)類的依賴應(yīng)該建立在最小的接口上


2. 代碼示例

示例1

  • 在示例1中不滿足接口隔離原則
  • Interface1接口的部分方法A、B類是不需要用到的,但對(duì)A、B類是可見的
public class S1 {
    public static void main(String[] args) {

    }
}

interface Interface01 {
    void operation01();

    void operation02();

    void operation03();

    void operation04();

    void operation05();
}

class B implements Interface01 {
    @Override
    public void operation01() {
        System.out.println("B 實(shí)現(xiàn)了 operation01");
    }

    @Override
    public void operation02() {
        System.out.println("B 實(shí)現(xiàn)了 operation02");
    }

    @Override
    public void operation03() {
        System.out.println("B 實(shí)現(xiàn)了 operation03");
    }

    @Override
    public void operation04() {
        System.out.println("B 實(shí)現(xiàn)了 operation04");
    }

    @Override
    public void operation05() {
        System.out.println("B 實(shí)現(xiàn)了 operation05");
    }
}

class D implements Interface01 {
    @Override
    public void operation01() {
        System.out.println("D 實(shí)現(xiàn)了 operation01");
    }

    @Override
    public void operation02() {
        System.out.println("D 實(shí)現(xiàn)了 operation02");
    }

    @Override
    public void operation03() {
        System.out.println("D 實(shí)現(xiàn)了 operation03");
    }

    @Override
    public void operation04() {
        System.out.println("D 實(shí)現(xiàn)了 operation04");
    }

    @Override
    public void operation05() {
        System.out.println("D 實(shí)現(xiàn)了 operation05");
    }
}

/**
 * A 類通過接口 Interface01 依賴 B類,但是只會(huì)用到 1,2,3方法
 */
class A {
    public void depend01(Interface01 interface01) {
        interface01.operation01();
    }
    public void depend02(Interface01 interface01) {
        interface01.operation02();
    }
    public void depend03(Interface01 interface01) {
        interface01.operation03();
    }
}
/**
 * C 類通過接口 Interface01 依賴 D類,但是只會(huì)用到 1,4,5方法
 */
class C {
    public void depend01(Interface01 interface01) {
        interface01.operation01();
    }
    public void depend04(Interface01 interface01) {
        interface01.operation04();
    }
    public void depend05(Interface01 interface01) {
        interface01.operation05();
    }
}

示例1-UML類圖

0223001.png

示例2

  • 滿足接口隔離原則
  • 將示例1中的接口Interface01,拆分成Interface001、Interface002、Interface003
  • 在此示例2中AI、CI類中不需要的方法對(duì)其是不可見的,滿足接口隔離原則
/**
 * 接口隔離原則
 */
public class S2Improve {
    public static void main(String[] args) {
        AI ai = new AI();
        ai.depend01(new BI());
        ai.depend02(new BI());
        ai.depend03(new BI());

        CI ci = new CI();
        ci.depend01(new DI());
        ci.depend04(new DI());
        ci.depend05(new DI());
    }
}

interface Interface001 {
    void operation01();
}

interface Interface002 {
    void operation02();

    void operation03();
}

interface Interface003 {
    void operation04();

    void operation05();
}

class BI implements Interface001, Interface002 {
    @Override
    public void operation01() {
        System.out.println("B 實(shí)現(xiàn)了 operation01");
    }

    @Override
    public void operation02() {
        System.out.println("B 實(shí)現(xiàn)了 operation02");
    }

    @Override
    public void operation03() {
        System.out.println("B 實(shí)現(xiàn)了 operation03");
    }
}

class DI implements Interface001, Interface003 {
    @Override
    public void operation01() {
        System.out.println("D 實(shí)現(xiàn)了 operation01");
    }

    @Override
    public void operation04() {
        System.out.println("D 實(shí)現(xiàn)了 operation04");
    }

    @Override
    public void operation05() {
        System.out.println("D 實(shí)現(xiàn)了 operation05");
    }
}

/**
 * A 類通過接口 Interface001, Interface002 依賴 B類,但是只會(huì)用到 1,2,3方法
 */
class AI {
    public void depend01(Interface001 interface001) {
        interface001.operation01();
    }

    public void depend02(Interface002 interface002) {
        interface002.operation02();
    }

    public void depend03(Interface002 interface002) {
        interface002.operation03();
    }
}

/**
 * C 類通過接口 Interface001, Interface003 依賴 D類,但是只會(huì)用到 1,4,5方法
 */
class CI {
    public void depend01(Interface001 interface001) {
        interface001.operation01();
    }

    public void depend04(Interface003 interface003) {
        interface003.operation04();
    }

    public void depend05(Interface003 interface003) {
        interface003.operation05();
    }
}

示例2-UML類圖

0323002.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容