当前位置: 移动技术网 > IT编程>软件设计>设计模式 > DesignPattern系列__02接口隔离原则

DesignPattern系列__02接口隔离原则

2019年08月04日  | 移动技术网IT编程  | 我要评论
介绍 客户端不应该依赖它不需要的接口,即 一个类对另一个类的依赖应该建立在最小接口上 。 Demo引入 先来看一张图: 如上图所示:类A通过接口MyInterface依赖类B,类C通过接口MyInterface依赖类D;但是,类A只是想要使用B实现的接口MyInterface中的1,2,3方法,类C ...

介绍

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

demo引入

先来看一张图:

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中的方法对于实现类来说,不是全部都需要的,那么,我们根据需求,将原来的接口进行拆分:

如图所示: 将原来的一个接口拆分为三个,对应的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方法,在项目开发中,接口通常充当规范来使用,是一种承诺,承诺越少越有利于系统开发。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网