接口隔离原则(Interface Segregation Principle)
基本介绍
- 客户端不应该依赖他不需要的接口,即一个类对另一个类的依赖应建立在最小的接口上
- 如下图
- A类通过接口Interface1依赖B类,C类通过接口Interface1依赖D类。如果Interface1对于A类和C类来说不是最小接口,那么B类和D类必须去实现他们不需要的方法
- 遵循接口隔离原则应这样处理:将接口Interface1拆分为独立的几个接口,A类和C类分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
应用实例
/*** Created with IntelliJ IDEA.* User: Mingda* Time: 2024/2/23 17:05* File: Segregation1* Description: 接口隔离原则*/
public class Segregation1 {public static void main(String[] args) {A a = new A();// A类通过接口去依赖B类a.method1(new B());a.method2(new B());a.method3(new B());C c = new C();// C类通过接口去依赖D类c.method1(new D());c.method4(new D());c.method5(new D());}
}// 接口
interface Interface1 {void operation1();void operation2();void operation3();void operation4();void operation5();
}// B类
class B implements Interface1 {public void operation1() {System.out.println("B operation1");}public void operation2() {System.out.println("B operation2");}public void operation3() {System.out.println("B operation3");}public void operation4() {System.out.println("B operation4");}public void operation5() {System.out.println("B operation5");}
}// D类
class D implements Interface1 {public void operation1() {System.out.println("D operation1");}public void operation2() {System.out.println("D operation2");}public void operation3() {System.out.println("D operation3");}public void operation4() {System.out.println("D operation4");}public void operation5() {System.out.println("D operation5");}
}/*** A类通过接口Interface1依赖(使用)B类,但是只会使用operation1()、operation2()、operation3()方法*/
class A {public void method1(Interface1 i) {i.operation1();}public void method2(Interface1 i) {i.operation2();}public void method3(Interface1 i) {i.operation3();}
}/*** C类通过接口Interface1依赖(使用)D类,但是只会使用operation1()、operation4()、operation5()方法*/
class C {public void method1(Interface1 i) {i.operation4();}public void method4(Interface1 i) {i.operation4();}public void method5(Interface1 i) {i.operation5();}
}
/*** Created with IntelliJ IDEA.* User: Mingda* Time: 2024/2/23 17:05* File: Segregation1* Description: 接口隔离原则*/
public class Segregation2 {public static void main(String[] args) {A a = new A();// A类通过接口去依赖B类a.method1(new B());a.method2(new B());a.method3(new B());C c = new C();// C类通过接口去依赖D类c.method1(new D());c.method4(new D());c.method5(new D());}
}// 接口1
interface Interface1 {void operation1();
}// 接口2
interface Interface2 {void operation2();void operation3();
}// 接口3
interface Interface3 {void operation4();void operation5();
}// B类
class B implements Interface1, Interface2 {public void operation1() {System.out.println("B operation1");}@Overridepublic void operation2() {System.out.println("B operation2");}@Overridepublic void operation3() {System.out.println("B operation3");}
}// D类
class D implements Interface1, Interface3 {public void operation1() {System.out.println("D operation1");}@Overridepublic void operation4() {System.out.println("D operation4");}@Overridepublic void operation5() {System.out.println("D operation5");}
}/*** A类通过接口Interface1、Interface2依赖(使用)B类,使用operation1()、operation2()、operation3()方法*/
class A {public void method1(Interface1 i) {i.operation1();}public void method2(Interface2 i) {i.operation2();}public void method3(Interface2 i) {i.operation3();}
}/*** C类通过接口Interface1、Interface3依赖(使用)D类,使用operation1()、operation4()、operation5()方法*/
class C {public void method1(Interface1 i) {i.operation1();}public void method4(Interface3 i) {i.operation4();}public void method5(Interface3 i) {i.operation5();}
}
github笔记