标签:out 调用 使用 sys 处理 框架 static dep rgs
接口隔离原则
客户端不应该被迫依赖于他不使用的方法。即一个类对另一个类的依赖应该建立在最小的接口上。要为各个类建立它们需要的专用接口,而不要试图去建立一个庞大的接口供所有依赖它的类去调用。
类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类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"); } } class A{ public void depend1(Interface1 i){ i.operation1(); } public void depend2(Interface1 i){ i.operation2(); } public void depend3(Interface1 i){ i.operation3(); } } class C{ public void depend1(Interface1 i){ i.operation1(); } public void depend4(Interface1 i){ i.operation4(); } public void depend5(Interface1 i){ i.operation5(); } }
按接口隔离原则应当这样处理:
将接口Interface1拆分为独立的几个接口(此处拆分为三个接口),类A和类C分别与他们需要的接口建立依赖关系,也就是采用接口隔离原则
public class InterfaceSegregation1 { 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(); } 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 { public void depend1(Interface1 i) { i.operation1(); } public void depend2(Interface2 i) { i.operation2(); } public void depend3(Interface2 i) { i.operation3(); } } class C { public void depend1(Interface1 i) { i.operation1(); } public void depend4(Interface3 i) { i.operation4(); } public void depend5(Interface3 i) { i.operation5(); } }
接口隔离原则和单一职责原则都是为了提高类的内聚性、降低它们之间的耦合性,体现了封装的思想,但两者是不同的:
1、单一职责原则注重的是职责,而接口隔离原则注重的是对接口依赖的隔离;
2、单一职责原则主要约束的是类,他针对的是程序的实现和细节;接口隔离原则主要约束接口,主要针对抽象和程序整体框架的构建。
标签:out 调用 使用 sys 处理 框架 static dep rgs
原文地址:https://www.cnblogs.com/clownAdam/p/13221608.html