接口隔离原则 Interface Segregation Principle
接口隔离原则概述
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
先看一张图:
类A通过接口Interface1依赖类B,类c通过接口Interface1依赖类D,如果接口Interface1对于类A和类c来说不是最小接口,那么类B和类D必须去实现他们不需要的方法按隔离原则.应当这样处理:将接口Interface1拆分为独立的几个接口,类A和类C分别它们需要的接口建立依赖关系,也就是采用接口隔离原则
接口隔离原则应用示例
类A通过接口Interface1依赖类B,类c通过接口Interface1依赖类D,接口Interface1中有五个方法:1,2,3,4,5方法。编写代码完成此应用实例:
1)A通过Interface1会依赖(使用)B
2)但是A中只会使用到接口Interface1中的1,2,3三个方法
3)C通过Interface1会依赖(使用)D
4)但是C中只会使用到接口Interface1中的1,4,5三个方法
代码实现(传统方法)
package com.tan.principle.interface_segregation;
public class InterfaceSegregationDemo1 {
public static void main(String[] args) {
A a = new A();
B b = new B();
a.depend1(b);
D d = new D();
C c = new C();
c.depend1(d);
}
}
interface Interface1 {
void method1();
void method2();
void method3();
void method4();
void method5();
}
class B implements Interface1 {
public void method1() {
System.out.println("B实现了method1");
}
public void method2() {
System.out.println("B实现了method2");
}
public void method3() {
System.out.println("B实现了method3");
}
public void method4() {
System.out.println("B实现了method4");
}
public void method5() {
System.out.println("B实现了method5");
}
}
class D implements Interface1 {
public void method1() {
System.out.println("D实现了method1");
}
public void method2() {
System.out.println("D实现了method2");
}
public void method3() {
System.out.println("D实现了method3");
}
public void method4() {
System.out.println("D实现了method4");
}
public void method5() {
System.out.println("D实现了method5");
}
}
//A类通过接口Interface1依赖(使用)B类,但是只会用到1,2,3方法
class A{
public void depend1(Interface1 i){
i.method1();
}
public void depend2(Interface1 i){
i.method2();
}
public void depend3(Interface1 i){
i.method3();
}
}
//C类通过接口Interface1依赖(使用)D类,但是只会用到1,4,5方法
class C{
public void depend1(Interface1 i){
i.method1();
}
public void depend4(Interface1 i){
i.method4();
}
public void depend5(Interface1 i){
i.method5();
}
}
使用接口隔离原则改进
传统代码的问题:
类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现它们不需要的方法
使用接口隔离原则改进的思路:
1.将接口Interface1拆分为独立的几个接口,类A和类C分别与它们需要的接口建立依赖关系,也就是采用接口隔离原则
2.接口Interface1中出现的方法,根据实际情况拆分为三个接口
-->接口Interface1中的五个方法继续拆分:
接口Interface1:方法1
接口Interface2:方法2,3
接口Interface3:方法4,5
由于类A通过类B去使用接口Interface1中的1,2,3三个方法,因此类B需要去实现接口Interface1和Interface2
由于类C通过类D去使用接口Interface1中的1,4,5三个方法,因此类D需要去实现接口Interface1和Interface3
代码实现(接口隔离原则改进)
package com.tan.principle.interface_segregation.improve;
public class InterfaceSegregationDemo2 {
public static void main(String[] args) {
A a = new A();
B b = new B();
a.depend1(b);
D d = new D();
C c = new C();
c.depend1(d);
}
}
//接口1
interface Interface1 {
void method1();
}
//接口2
interface Interface2 {
void method2();
void method3();
}
//接口3
interface Interface3 {
void method4();
void method5();
}
class B implements Interface1,Interface2 {
public void method1() {
System.out.println("B实现了method1");
}
public void method2() {
System.out.println("B实现了method2");
}
public void method3() {
System.out.println("B实现了method3");
}
}
class D implements Interface1,Interface3 {
public void method1() {
System.out.println("D实现了method1");
}
public void method4() {
System.out.println("D实现了method4");
}
public void method5() {
System.out.println("D实现了method5");
}
}
//A类通过接口Interface1,接口Interface2依赖(使用)B类
class A{
public void depend1(Interface1 i){
i.method1();
}
public void depend2(Interface2 i){
i.method2();
}
public void depend3(Interface2 i){
i.method3();
}
}
//C类通过接口Interface1,接口Interface3依赖(使用)D类
class C{
public void depend1(Interface1 i){
i.method1();
}
public void depend4(Interface3 i){
i.method4();
}
public void depend5(Interface3 i){
i.method5();
}
}