关于相关"代理"模式的学习
设计模式中有一类是跟代理相关的模式:Proxy, State, Facade, Adapter。初学者很容易混淆,不能明确它们的用途和区别。本文浅谈下它们的区别,以帮助更好的理解和运用。
Proxyview sourceprint?01
interface
ProxyBase {
02
????
void
function();
03
}
04
?05
class
Proxy
implements
ProxyBase {
06
????
ProxyBase implementation =
new
Implementation();
07
????
?08
????
@Override
09
????
public
void
function() {
10
????????
implementation.function();
11
????
}
12
}
13
?14
class
Implementation
implements
ProxyBase {
15
????
@Override
16
????
public
void
function() {
17
????????
System.out.println(
"Implementation.function()"
);
18
????
}
19
}
20
?21
class
Test {
22
????
public
static
void
main(String[] args) {
23
????????
Proxy p =
new
Proxy();
24
????????
p.function();
25
????
}
26
}
从例子可以看出,Proxy和Implementation同时实现了接口ProxyBase,真正的功能是由Implementation实例提供的。
Stateview sourceprint?01
interface
State {
02
????
void
function1();
03
????
void
function2();
04
}
05
?06
class
StateOne
implements
State {
07
????
@Override
08
????
public
void
function1() {
09
????????
System.out.println(
"StateOne.function1()"
);
10
????
}
11
????
?12
????
@Override
13
????
public
void
function2() {
14
????????
System.out.println(
"StateOne.function2()"
);
15
????
}
16
}
17
?18
class
StateTwo
implements
State {
19
????
@Override
20
????
public
void
function1() {
21
????????
System.out.println(
"StateTwo.function1()"
);
22
????
}
23
????
?24
????
@Override
25
????
public
void
function2() {
26
????????
System.out.println(
"StateTwo.function2()"
);
27
????
}
28
}
29
?30
class
User {
31
????
State state =
new
StateOne();
32
????
?33
????
public
void
setState(State newState) {
34
????????
state = newState;
35
????
}
36
????
?37
????
public
void
service() {
38
????????
state.function1();
39
????????
state.function2();
40
????
}
41
}
42
?43
class
Test {
44
????
public
static
void
main(String[] args) {
45
????????
User user =
new
User();
46
????????
user.service();
47
????????
user.setState(
new
StateTwo());
48
????????
user.service();
49
????
}
50
}
State模式提供统一的服务接口,而根据不同的实现,提供不同的行为。
Facadeview sourceprint?01
/* Complex parts */
02
?
?03
class CPU {
04
?
?05
????
public void freeze() { ... }
06
????
public void jump(long position) { ... }
07
????
public void execute() { ... }
08
?
?09
}
10
?
?11
class Memory {
12
?
?13
????
public void load(long position, byte[] data) { ... }
14
?
?15
}
16
?
?17
class HardDrive {
18
?
?19
????
public byte[] read(long lba, int size) { ... }
20
?
?21
}
22
?
?23
/* Facade */
24
?
?25
class Computer {
26
?
?27
????
private CPU cpu;
28
????
private Memory memory;
29
????
private HardDrive hardDrive;
30
?
?31
????
public Computer() {
32
????????
this.cpu = new CPU();
33
????????
this.memory = new Memory();
34
????????
this.hardDrive = new HardDrive();
35
????
}
36
?
?37
????
public void startComputer() {
38
????????
cpu.freeze();
39
????????
memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
40
????????
cpu.jump(BOOT_ADDRESS);
41
????????
cpu.execute();
42
????
}
43
?
?44
}
45
?
?46
/* Client */
47
?
?48
class
You {
49
?
?50
????
public
static
void
main(String[] args) {
51
????????
Computer facade =
new
Computer();
52
????
facade.startComputer();
53
????
}
54
?
?55
}
Facade模式:将一个复杂的系统转换成简单易用的服务接口,它在结构上没有要求,更多是强调一种概念。
Adapterview sourceprint?01
class
Adapter {
02
????
Adaptee adaptee;
03
????
?04
????
public
Adapter(Adaptee adaptee) {
05
????????
this
.adaptee = adaptee;
06
????
}
07
????
?08
????
void
request() {
09
????????
adaptee.specificRequest();
10
????
}
11
}
12
?13
class
Adaptee {
14
????
void
specificRequest() {
15
????????
System.out.println(
"Adaptee.specificRequest()"
);
16
????
}
17
}
18
?19
class
Test {
20
????
public
static
void
main(String[] args) {
21
????????
Adaptee adaptee =
new
Adaptee();
22
????????
Adapter adapter =
new
Adapter(adaptee);
23
????????
adapter.request();
24
????
}
25
}
Adapter模式:当接口不匹配时,将接口转换成另一种类型的接口。
Summary在比较上面的几种模式前,先清楚两个概念:接口和行为。此处的接口指对外所提供的服务。行为指具体的服务实现。
用这两个概念来解释区别,Adapter的本意就是接口改变,行为不变。Proxy,State是接口不变,行为改变。(Proxy可以认为是State的一种特殊情形,在结构上,State有很多服务实现,而Proxy只有一种)