• 02.接口隔离原则(Interface Segregation Principle)


    一言

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

    为什么要有接口隔离原则

    反例设计

    在这里插入图片描述

    反例代码

    public class Segregation1 {
    }
    
    interface Interface1 {
        void operation1();
        void operation2();
        void operation3();
        void operation4();
        void operation5();
    }
    
    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");
        }
    }
    
    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");
        }
    }
    
    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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    针对类A,通过接口Interface1依赖类B,类C通过Interface1依赖类D,如果接口Interface1对于类A和类C来说不是最小的接口,那么对于类B和类D来说,他们就必须去实现原本他们不需要的方法。这显然是有优化空间的。

    在上图中,A通过Interface1依赖(使用)B,但A中实际上只会用到1,2,3三个方法;而C通过Interface1依赖(使用)D,但C中只会用到接口1,4,5三个方法

    设计提升

    按接口隔离原则应当这样处理:
    将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。
    核心其实就是一个字:拆!
    实际上上例中的Interface1完全可以拆分成三个接口,这样一来底层按需取用也降低了耦合性。

    优化结构

    在这里插入图片描述

    代码详解

    public class Segregation2 {
        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());
        }
    }
    
    interface Interface1 {
        void operation1();
    }
    
    interface Interface2 {
        void operation2();
        void operation3();
    }
    
    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{//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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    头脑风暴

    讲了这么多,为啥感觉接口隔离原则和单一职责原则说的是一个事呢?

    其实描述的并不是一个事情,
    单一职责原则原注重的是职责,而接口隔离原则注重对接口依赖的隔离。单一职责原则主要是约束类,其次才是接口和方法,它针对的是程序中的实现和细节;而接口隔离原则主要约束接口,主要针对抽象,针对程序整体框架的构建。
    我们可以再参考一下ChatGPT的表述:
    在这里插入图片描述
    也就是说,单一职责原则旨在保持类的“内聚”,使得每个类的职责更加明确,更加专注。而接口隔离原则关注点在接口上,它要求接口设计遵循一定的原子性,减少架构层面不必要的耦合可能


    关注我,共同进步,每周至少一更。——Wayne

  • 相关阅读:
    【C++进阶】map和set——下篇(红黑树的学习以及封装map和set)
    nginx配置文件 location语法
    腾讯云学生用户专享优惠活动汇总
    CSS动画实现节流
    huggingface下大模型最好的方案
    SSM+服装管理系统 毕业设计-附源码080948
    【从0开始编写webserver·基础篇#02】服务器的核心---I/O处理单元和任务类
    大二Web课程设计——美食网站设计与实现(HTML+CSS+JavaScript)
    Redis常见错误
    iOS pod install 失败深度解决方案
  • 原文地址:https://blog.csdn.net/WayneSlytherin/article/details/134487076