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

1 接口隔离原则的基本介绍

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

2 实例分析

我们来看一张UML 类图
在这里插入图片描述
我们通过实际代码来实现上面类图:

package com.andy.principle.segregation;

public class Segregation1 {
    public static void main(String[] args) {

    }
}

//接口
interface Interface1 {
    void operation1();

    void operation2();

    void operation3();

    void operation4();

    void operation5();
}

// B类实现了接口
class B implements Interface1 {

    @Override
    public void operation1() {
        System.out.println("B 实现了 operation1");
    }

    @Override
    public void operation2() {
        System.out.println("B 实现了 operation2");
    }

    @Override
    public void operation3() {
        System.out.println("B 实现了 operation3");
    }

    @Override
    public void operation4() {
        System.out.println("B 实现了 operation4");
    }

    @Override
    public void operation5() {
        System.out.println("B 实现了 operation5");
    }
}

// D类实现了接口
class D implements Interface1 {

    @Override
    public void operation1() {
        System.out.println("D 实现了 operation1");
    }

    @Override
    public void operation2() {
        System.out.println("D 实现了 operation2");
    }

    @Override
    public void operation3() {
        System.out.println("D 实现了 operation3");
    }

    @Override
    public void operation4() {
        System.out.println("D 实现了 operation4");
    }

    @Override
    public void operation5() {
        System.out.println("D 实现了 operation5");
    }
}

//类A通过接口Interface1 依赖(使用)B类,但是只会用到1,2,3方法
class A {
    public void depend1(Interface1 i) {
        i.operation1();
    }
    public void depend2(Interface1 i) {
        i.operation2();
    }
    public void depend3(Interface1 i) {
        i.operation3();
    }
}

//类C通过接口Interface1 依赖(使用)B类,但是只会用到1,4,5方法
class C {
    public void depend1(Interface1 i) {
        i.operation1();
    }
    public void depend4(Interface1 i) {
        i.operation4();
    }
    public void depend5(Interface1 i) {
        i.operation5();
    }
}

分析 :
Interface1 接口有5个方法,B类和D类实现了Interface1接口,并且重写了Interface1接口的5个方法.类A通过接口Interface1 依赖(使用)B类,但是只会用到1,2,3方法.类C通过接口Interface1 依赖(使用)D类,但是只会用到1,4,5方法.我们可以看到A类是不使用到4,5方法,而C类也不用2,3方法.如果接口Interface1 对于A类和C类来说不是最小接口,那么B类和类D必须去实现他们不需要的方法.

那我们要实现最小接口隔离该怎么办呢? 方法很简单,就是将Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系.也就是采用接口隔离原则.

3 实现接口隔离原则

那下面我们将使用接口隔离原则对上面的实例进行改动.
接口Interface1中出现的方法,根据实际情况可以拆分为三个接口,请看下面的UML类图:

在这里插入图片描述
用代码实现上面的类图:

package com.andy.principle.segregation.improve;

public class Segregation1 {
    public static void main(String[] args) {
        A a = new A(); //A类通过接口去依赖B类
        a.depend1(new B());
        a.depend2(new B());
        a.depend3(new B());

        C c = new C(); //C类通过接口去依赖(使用)D类
        c.depend1(new D());
        c.depend4(new D());
        c.depend5(new D());
    }
}

//接口1
interface Interface1 {
    void operation1();
}

//接口2
interface Interface2 {
    void operation2();

    void operation3();
}

//接口3
interface Interface3 {
    void operation4();

    void operation5();
}

class B implements Interface1, Interface2 {

    @Override
    public void operation1() {
        System.out.println("B 实现了 operation1");
    }

    @Override
    public void operation2() {
        System.out.println("B 实现了 operation2");
    }

    @Override
    public void operation3() {
        System.out.println("B 实现了 operation3");
    }
}

class D implements Interface1, Interface3 {

    @Override
    public void operation1() {
        System.out.println("D 实现了 operation1");
    }

    @Override
    public void operation4() {
        System.out.println("D 实现了 operation4");
    }

    @Override
    public void operation5() {
        System.out.println("D 实现了 operation5");
    }
}

class A {
    public void depend1(Interface1 i) {
        i.operation1();
    }

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

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

class C {
    public void depend1(Interface1 i) {
        i.operation1();
    }

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

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

输出结果:

B 实现了 operation1
B 实现了 operation2
B 实现了 operation3
D 实现了 operation1
D 实现了 operation4
D 实现了 operation5

分析
上面的改进方案,把原来的Interface1 接口根据实际情况分拆成3个接口,然后让需要依赖的类实现需要用到的接口,而不用实现所有接口.这就是最小就扣实现,也是接口隔离原则.

发布了18 篇原创文章 · 获赞 1 · 访问量 163

猜你喜欢

转载自blog.csdn.net/andyonlines/article/details/104079445