设计模式原则之:接口隔离原则
客户端不应该依赖它不需要的接口,即一个接口对另一个类的依赖应该建立在最小的接口上
看图说话:
/** * @description: 接口隔离原则 * @author: abel.he * @date: 2023-08-01 **/ public class InterfaceSegregationPrincipleError { public static void main(String[] args) { A a = new A(); a.depend1(new B()); a.depend2(new B()); a.depend3(new B()); C c = new C(); c.depend1(new D()); c.depend4(new D()); c.depend5(new D()); } } 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"); } } /** * 通过接口Interface1 使用了方法 1 2 3 4 5 没用到 */ class A { public void depend1(Interface1 interface1) { interface1.operation1(); } public void depend2(Interface1 interface1) { interface1.operation2(); } public void depend3(Interface1 interface1) { interface1.operation3(); } } /** * 依赖接口 Interface1 使用方法 1 4 5 , 为使用方法 2 3 */ class C { public void depend1(Interface1 interface1) { interface1.operation1(); } public void depend4(Interface1 interface1) { interface1.operation4(); } public void depend5(Interface1 interface1) { interface1.operation5(); } }
应传统方法的问题和使用接口隔离原则改进
- 类A通过Interface1依赖类B,类C通过Interface1依赖类D,如果Interface1对于类A和C来说不是最小接口,那么类B和D必须去实现它们不需要的方法
- 将接口拆分为独立的几个接口,类A和C分别与它们需要的接口建立依赖关系。也就是采用接口隔离原则
- 接口Interface1中出现的方法,根据实际情况拆分为三个接口
- 代码实现
/** * @description: 接口隔离原则正确案例 * @author: abel.he * @date: 2023-08-01 **/ public class InterfaceSegregationPrincipleCorrect { public static void main(String[] args) { A1 a = new A1(); a.depend1(new B1()); a.depend2(new B1()); a.depend3(new B1()); C1 c1 = new C1(); c1.depend(new D1()); c1.depend4(new D1()); c1.depend4(new D1()); } } interface Interface { void operation(); } interface Interface2 { void operation2(); void operation3(); } interface Interface3 { void operation4(); void operation5(); } class B1 implements Interface, Interface2 { @Override public void operation() { System.out.println("B1 实现了 operation"); } @Override public void operation2() { System.out.println("B1 实现了 operation2"); } @Override public void operation3() { System.out.println("B1 实现了 operation3"); } } class D1 implements Interface, Interface3 { @Override public void operation() { System.out.println("D1 实现了方法operation"); } @Override public void operation4() { System.out.println("D1 实现了方法operation4"); } @Override public void operation5() { System.out.println("D1 实现了方法operation5"); } } class A1 { public void depend1(Interface inter) { inter.operation(); } public void depend2(Interface2 interface2) { interface2.operation2(); } public void depend3(Interface2 interface2) { interface2.operation3(); } } class C1 { public void depend (Interface inter){ inter.operation(); } public void depend4(Interface3 interface3) { interface3.operation4(); } public void depend5(Interface3 interface3) { interface3.operation4(); } }