设计模式之接口隔离原则

2020-01-14  本文已影响0人  沉江小鱼

相关链接:
0. 设计模式之六大原则总结
1. 设计模式之单一职责原则
2. 设计模式之里式替换原则
3. 设计模式之依赖倒置原则
4. 设计模式之接口隔离原则
5. 设计模式之迪米特法则
6. 设计模式之开闭原则

1.1 定义
1.2 问题由来

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

1.3 解决方案

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

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

这个图的意思是:

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 总结

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

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

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

上一篇 下一篇

猜你喜欢

热点阅读