设计模式原则之:接口隔离原则
客户端不应该依赖它不需要的接口,即一个接口对另一个类的依赖应该建立在最小的接口上
看图说话:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | /** * @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中出现的方法,根据实际情况拆分为三个接口
- 代码实现
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101/**
* @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();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?