设计模式七大原则之接口隔离

什么是接口隔离原则?

对于类的接口依赖,客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小接口上

为什么要设计接口隔离

(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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章