接口隔离原则介绍
客户端不要依赖不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。
案例引入
A类,通过接口1依赖B类,C类通过依赖接口1依赖D类,A类只会用到1-3方法,C类只会使用到1,4,5方法。
uml类图
代码实现
public class Segregation1 { public static void main(String[] args) { A a = new A(); a.dependency1(new B()); a.dependency2(new B()); a.dependency3(new B()); C c = new C(); c.dependency1(new D()); c.dependency4(new D()); c.dependency5(new D()); }}interface Interface1{ public void operation1(); public void operation2(); public void operation3(); public void operation4(); public 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{ public void dependency1(Interface1 i){ i.operation1(); } public void dependency2(Interface1 i){ i.operation2(); } public void dependency3(Interface1 i){ i.operation3(); }}class C{ public void dependency1(Interface1 i){ i.operation1(); } public void dependency4(Interface1 i){ i.operation4(); } public void dependency5(Interface1 i){ i.operation5(); }}
案例分析
- 类A通过接口interface1依赖B,类C通过接口依赖D,因为接口interface1对于A类和C类来说,不是最小的接口,就会导致类B,类D去重写使用不到的方法
- 将接口interface1拆分成独立的接口,类A和C分别和其依赖的接口建立依赖关系。也就是采用接口隔离原则。
- 接口interface1出现的方法,也拆分成三个接口 如下图所示
案例改进uml类图
public class Segregation2 { public static void main(String[] args) { A a = new A(); a.dependency1(new B()); a.dependency2(new B()); a.dependency3(new B()); C c = new C(); c.dependency1(new D()); c.dependency4(new D()); c.dependency5(new D()); }}interface Interface1 { public void operation1();}interface Interface2{ public void operation2(); public void operation3();}interface Interface3{ public void operation4(); public 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{ public void dependency1(Interface1 i){ i.operation1(); } public void dependency2(Interface2 i){ i.operation2(); } public void dependency3(Interface2 i){ i.operation3(); }}class C{ public void dependency1(Interface1 i){ i.operation1(); } public void dependency4(Interface3 i){ i.operation4(); } public void dependency5(Interface3 i){ i.operation5(); }}
这样设计的uml类图,满足了案例A,B,C,D四类需要,也满足了接口隔离原则。
只是为了记录自己的学习历程,且本人水平有限,不对之处,请指正。