1. 程式人生 > 其它 >【設計模式】七大設計原則(四)—— 介面隔離原則(Interface Segregation Principle)

【設計模式】七大設計原則(四)—— 介面隔離原則(Interface Segregation Principle)

介面隔離原則介紹

1.客戶端不應該依賴它不需要的介面,即一個類對另一個類的依賴應該建立再最小的介面上

2.使用多個隔離的介面,比使用單個介面要好。

3.他還有另一個意思:降低類之間的耦合度。


示例

interface Interface1{
    void operation1();
    void operation2();
    void operation3();
    void operation4();
    void operation5();
}

class B implements Interface1{
    public void operation1(){
        System.out.println("B 實現了 operation1");
    }
    public void operation2(){
        System.out.println("B 實現了 operation2");
    }
    public void operation3(){
        System.out.println("B 實現了 operation3");
    }
    public void operation4(){
        System.out.println("B 實現了 operation4");
    }
    public void operation5(){
        System.out.println("B 實現了 operation5");
    }
}

class D implements Interface1{
    public void operation1(){
        System.out.println("D 實現了 operation1");
    }
    public void operation2(){
        System.out.println("D 實現了 operation2");
    }
    public void operation3(){
        System.out.println("D 實現了 operation3");
    }
    public void operation4(){
        System.out.println("D 實現了 operation4");
    }
    public void operation5(){
        System.out.println("D 實現了 operation5");
    }
}

class A{ // A類通過介面Interface1 依賴(使用)B類,但只會使用1,2,3方法
    public void depend1(Interface1 i){
        i.operation1();
    }

    public void depend2(Interface1 i){
        i.operation2();
    }

    public void depend3(Interface1 i){
        i.operation3();
    }
}

class C{ // C類通過介面Interface1 依賴(使用)D類,但只會使用1,4,5方法
    public void depend1(Interface1 i){
        i.operation1();
    }

    public void depend4(Interface1 i){
        i.operation4();
    }

    public void depend5(Interface1 i){
        i.operation5();
    }
}

可以看到其實A類依賴B類,C類依賴D類都只使用了部分的方法,但是B類和D類都實現了介面Interface1的五個方法,有部分方法是沒用到的。按照介面隔離原則應當將介面Interface1拆成幾個介面,類A和類C分別與他們需要的介面建立依賴關係。

//介面1
interface Interface1{
    void operation1();
}

//介面2
interface  Interface2{
    void operation2();
    void operation3();
}

//介面3
interface Interface3{
    void operation4();
    void operation5();
}

class B implements Interface1,Interface2{
    public void operation1(){
        System.out.println("B 實現了 operation1");
    }
    public void operation2(){
        System.out.println("B 實現了 operation4");
    }
    public void operation3(){
        System.out.println("B 實現了 operation5");
    }
}

class D implements Interface1,Interface3{
    public void operation1(){
        System.out.println("D 實現了 operation1");
    }
    public void operation4(){
        System.out.println("D 實現了 operation2");
    }
    public void operation5(){
        System.out.println("D 實現了 operation3");
    }
}

class A{ // A類通過介面Interface1,Interface2 依賴(使用)B類,但只會使用1,2,3方法
    public void depend1(Interface1 i){
        i.operation1();
    }

    public void depend2(Interface2 i){
        i.operation2();
    }

    public void depend3(Interface2 i){
        i.operation3();
    }
}

class C{ // C類通過介面Interface1,Interface3 依賴(使用)D類,但只會使用1,4,5方法
    public void depend1(Interface1 i){
        i.operation1();
    }

    public void depend4(Interface3 i){
        i.operation4();
    }

    public void depend5(Interface3 i){
        i.operation5();
    }
}

使用一下

public class Segregation1 {
    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());
    }
}