Skip to content

Commit c075ae7

Browse files
committed
add > 适配器模式
1 parent 179b986 commit c075ae7

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package structural;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* Created by 李恒名 on 2017/7/27.
7+
* <p>
8+
* 适配器模式,用于将一个类的接口转换成客户端所期待的另一种接口。
9+
*/
10+
public class AdapterPattern {
11+
12+
//////////////////////////////////////////////////////////////////////
13+
// 类适配器 //
14+
//////////////////////////////////////////////////////////////////////
15+
16+
//被适配的源对象(Adaptee)
17+
static class Power_220V {
18+
public void in() {
19+
System.out.print("220V电流输入");
20+
}
21+
}
22+
23+
//目标接口(Target)
24+
interface Power_5V {
25+
void in();
26+
}
27+
28+
//适配器(Adapter),电源适配器为例
29+
static class PowerAdapter extends Power_220V implements Power_5V {
30+
@Override
31+
public void in() {
32+
System.out.print("5V电流输入");
33+
System.out.print("(");
34+
super.in();
35+
System.out.print(")");
36+
}
37+
}
38+
39+
//客户端,手机为例
40+
static class Iphone {
41+
42+
public void charge(Power_5V power) {
43+
System.out.print("充电中...");
44+
power.in();
45+
}
46+
}
47+
48+
@Test
49+
public void testClassAdapter() {
50+
Iphone iPhone = new Iphone();
51+
PowerAdapter powerAdapter = new PowerAdapter();
52+
iPhone.charge(powerAdapter);
53+
//充电中...5V电流输入(220V电流输入)
54+
}
55+
56+
//////////////////////////////////////////////////////////////////////
57+
// 对象适配器 //
58+
//////////////////////////////////////////////////////////////////////
59+
60+
//适配器(Adapter),电源适配器为例
61+
static class PowerAdapter2 implements Power_5V {
62+
63+
private Power_220V power_220V;
64+
65+
public PowerAdapter2(Power_220V power_220V) {
66+
this.power_220V = power_220V;
67+
}
68+
69+
@Override
70+
public void in() {
71+
System.out.print("5V电流输入");
72+
System.out.print("(");
73+
power_220V.in();
74+
System.out.print(")");
75+
}
76+
}
77+
78+
@Test
79+
public void testObjectAdapter() {
80+
Iphone iPhone = new Iphone();
81+
PowerAdapter2 powerAdapter = new PowerAdapter2(new Power_220V());
82+
iPhone.charge(powerAdapter);
83+
//充电中...5V电流输入(220V电流输入)
84+
}
85+
86+
87+
//////////////////////////////////////////////////////////////////////
88+
// 接口适配器 //
89+
//////////////////////////////////////////////////////////////////////
90+
91+
/**
92+
* 有时候,我们需要实现一个接口,但只想实现其中一部分方法,又不
93+
* 想出现很多空的实现方法(这会让代码不那么优美),这时候就便可
94+
* 以使用接口适配器,把那些空的实现方法放在适配器内,去重写那些
95+
* 我们真正要实现的方法即可。
96+
* <p>
97+
* 如果你使用过 Spring MVC,你应该对下面这两个接口适配器很熟悉
98+
* <p>
99+
* - WebMvcConfigurerAdapter
100+
* - HandlerInterceptorAdapter
101+
*/
102+
103+
//源接口
104+
interface Source {
105+
void method1();
106+
107+
void method2();
108+
109+
void method3();
110+
}
111+
112+
//接口适配器
113+
static abstract class SourceAdapter implements Source {
114+
@Override
115+
public void method1() {}
116+
@Override
117+
public void method2() { }
118+
@Override
119+
public void method3() { }
120+
}
121+
122+
//继承适配器,重写 method1
123+
static class SourceImpl extends SourceAdapter {
124+
@Override
125+
public void method1() {
126+
System.out.println("method1 impl");
127+
}
128+
}
129+
130+
/**
131+
* 总结:
132+
* 在我们的应用程序中我们可能需要将两个不同接口的类来进行通信,在不修改这两个的前提
133+
* 下我们可能会需要某个中间件来完成这个衔接的过程。这个中间件就是适配器。所谓适配器
134+
* 模式就是将一个类的接口,转换成客户期望的另一个接口。它可以让原本两个不兼容的接口
135+
* 能够无缝完成对接。
136+
*/
137+
138+
}

0 commit comments

Comments
 (0)