设计模式之接口隔离原则

1.1 定义
  • 客户端不应该依赖它不需要的接口
  • 一个类对另一个类的依赖应该建立在最小的接口上
1.2 问题由来

类 A 通过接口 Interface1 依赖 B,类 C 通过接口 Interface1 依赖 D,如果接口 Interface1对于类 A 和类 B 来说不是最小接口,则类 B 和类 D 必须去实现他们不需要的方法。

1.3 解决方案

将臃肿的接口 Interface1 拆分为独立的几个接口,类 A 和类 C 分别与他们需要的接口建立依赖关系,也就是采用接口隔离原则。

1.4 举例说明
 
未遵循接口隔离原则的设计

这个图的意思是:

  • 类 A 依赖接口 I 中的方法1、方法 2、方法 3,类 B 是对类 A 依赖方法的实现。
  • 类 C 依赖接口 I 中的方法 1、方法 4、方法 5,类 D 是对类 C 依赖方法的实现
    对于类 B 和类 D 来说,虽然他们存在着用不到的方法(也就是图中红色字体标记的方法),但由于实现了接口 I,所以也必须要实现这些用不到的方法,代码如下:
protocol Interface1 {
    func method1();
    func method2();
    func method3();
    func method4();
    func method5();
}

class A {
    func depend1(interface : Interface1) {
        interface.method1();
    }
    
    func depend2(interface : Interface1) {
        interface.method2();
    }
    
    func depend3(interface : Interface1) {
        interface.method3();
    }
}

class B : Interface1 {
    func method1() {
        print("类 B 实现接口 1的方法 1")
    }
    
    func method2() {
        print("类 B 实现接口 1的方法 2")
    }
    
    func method3() {
        print("类 B 实现接口 1的方法 3")
    }
    
    
    // 对于类 B 来说,method4和 method5 不是必须的,但是由于接口中定义了这两个方法,所以也需要实现
    func method4() {}
    func method5() {}
}

class C {
    func depend1(interface : Interface1) {
        interface.method1();
    }
    
    func depend4(interface : Interface1) {
        interface.method4();
    }
    
    func depend5(interface : Interface1) {
        interface.method5();
    }
}

class D : Interface1 {
    func method1() {
        print("类 D 实现接口 1的方法 1")
    }
    
    func method4() {
        print("类 D 实现接口 1的方法 4")
    }
    
    func method5() {
        print("类 D 实现接口 1的方法 5")
    }
    
   
    // 对于类 D 来说,method2和 method3 不是必须的,但是由于接口中定义了这两个方法,所以也需要实现
    func method2() {}
    func method3() {}
}

我们可以看到,如果接口过于臃肿,只要接口中出现的方法,不管对依赖于它的类有没有用处,实现类中都必须去实现这些方法,这显然不是好的设计。如果将这个设计修改为符合接口隔离原则,就必须对接口 Interface1 进行拆分。

1.5 举例进阶

在这里,我们将原有的接口 Interface1 拆分为 3 个接口,拆分后的设计如图所示:


 
遵循接口隔离原则的设计

代码如下:

protocol Interface1 {
    func method1();
}

protocol Interface2 {
    func method2();
    func method3();
}

protocol Interface3 {
    func method4();
    func method5();
}

class A {
    func depend1(interface : Interface1) {
        interface.method1();
    }
    
    func depend2(interface : Interface2) {
        interface.method2();
    }
    
    func depend3(interface : Interface2) {
        interface.method3();
    }
}

class B : Interface1, Interface2 {
    func method1() {
        print("类 B 实现接口 1的方法 1")
    }
    
    func method2() {
        print("类 B 实现接口 2的方法 2")
    }
    
    func method3() {
        print("类 B 实现接口 2的方法 3")
    }
}

class C {
    func depend1(interface : Interface1) {
        interface.method1();
    }
    
    func depend4(interface : Interface3) {
        interface.method4();
    }
    
    func depend5(interface : Interface3) {
        interface.method5();
    }
}

class D : Interface1, Interface3 {
    func method1() {
        print("类 D 实现接口 1的方法 1")
    }
    
    func method4() {
        print("类 D 实现接口 3的方法 4")
    }
    
    func method5() {
        print("类 D 实现接口 3的方法 5")
    }
}
1.6 举例总结

接口隔离原则的含义是:建立单一接口,不要建立庞大臃肿的接口,尽量细化接口,接口中的方法尽量少。也就是说,我们要为各个类建立专用的接口,而不要试图去建立一个很庞大的接口供所有依赖它的类去调用。

上面的例子中,将一个庞大的接口变更为 3 个专用的接口所采用的就是接口隔离原则。在程序设计中,依赖几个专用的接口要比依赖一个综合的接口更灵活。接口是设计时对外部设定的“契约”,通过分散定义多个接口,可以预防外来变更的扩散,提高系统的灵活性和可维护性。

1.7 总结

很多人会觉得接口隔离原则跟之前的单一职责原则很相似,其实不然,原因有两点:

  • 单一职责原则注重的是职责,而接口隔离原则注重对接口依赖的隔离
  • 单一职责原则主要是约束类,其次才是接口和方法,它针对的是程序中的实现和细节;而接口隔离原则主要约束接口,主要针对抽象,针对程序整体框架的构建

采用接口隔离原则对接口进行约束时,要注意以下几点:

  • 接口尽量小,但是要有限度。对接口进行细化可以提高程序设计灵活性是不争的事实,但是如果过小,则会造成接口数量过多,使设计复杂化,所以要适度
  • 为依赖接口的类定制服务,只暴露给调用的类它需要的方法,它不需要的方法则隐藏起来,只有专注的为一个模块提供定制服务,才能建立最小的依赖关系
  • 提高内聚,减少对外交互,使接口用最少的方法去完成最多的事情

建议:运用接口隔离原则,一定要适度,接口设计的过大或者过小都不好,设计接口的时候,需要尽量的多花一些时间去思考和规划。



作者:code_ce
链接:https://www.jianshu.com/p/336574140fb5
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

posted on 2022-06-27 20:58  1450811640  阅读(21)  评论(0编辑  收藏  举报