DesignPattern系列__02接口隔离原则

介绍

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

Demo引入

先来看一张图:

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

class B implements MyInterface {

    @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");
    }
}

class D implements MyInterface {

    @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");
    }
}

class A {
    //类A通过接口MyInterface依赖(使用)B类, 但是只使用1,2,3方法
    public void depand1(MyInterface myInterface) {
        myInterface.operation1();
    }

    public void depand2(MyInterface myInterface) {
        myInterface.operation2();
    }

    public void depand3(MyInterface myInterface) {
        myInterface.operation3();
    }
}

class C {
    public void depand1(MyInterface myInterface) {
        myInterface.operation1();
    }

    public void depand4(MyInterface myInterface) {
        myInterface.operation4();
    }

    public void depand5(MyInterface myInterface) {
        myInterface.operation5();
    }
}

如上图所示:类A通过接口MyInterface依赖类B,类C通过接口MyInterface依赖类D;但是,类A只是想要使用B实现的接口MyInterface中的1,2,3方法,类C想要使用类D实现的接口MyInterface中的1,4,5方法;所以,现在的设计,从接口隔离原则的角度来说,不符合“最小接口”。

改进措施:

既然接口MyInterface中的方法对于实现类来说,不是全部都需要的,那么,我们根据需求,将原来的接口进行拆分:

image

如图所示: 将原来的一个接口拆分为三个,对应的code如下:

interface MyInterface1 {
    void operation1();

}

interface MyInterface2 {
    void operation2();
    void operation3();

}

interface MyInterface3 {
    void  operation4();
    void  operation5();
}

class B implements MyInterface1, MyInterface2 {

    @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 MyInterface1, MyInterface3 {

    @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 {
    //类A通过接口MyInterface依赖(使用)B类, 但是只使用1,2,3方法
    public void depand1(MyInterface1 myInterface) {
        myInterface.operation1();
    }

    public void depand2(MyInterface2 myInterface) {
        myInterface.operation2();
    }

    public void depand3(MyInterface2 myInterface) {
        myInterface.operation3();
    }
}

class C {
    public void depand1(MyInterface1 myInterface) {
        myInterface.operation1();
    }

    public void depand4(MyInterface3 myInterface) {
        myInterface.operation4();
    }

    public void depand5(MyInterface3 myInterface) {
        myInterface.operation5();
    }
}

总结

接口的设计尽量要小

这是接口隔离的核心,当然过小的接口会导致项目结构的负责度增加,在实际使用中,要合理把握尺度。

接口要高内聚

即接口尽量少的公布public方法,在项目开发中,接口通常充当规范来使用,是一种承诺,承诺越少越有利于系统开发。

你可能感兴趣的:(DesignPattern系列__02接口隔离原则)