关于相关"代理"模式的学习
2011-02-15 14:38 RayLee 阅读(238) 评论(0) 编辑 收藏 举报设计模式中有一类是跟代理相关的模式:Proxy, State, Facade, Adapter。初学者很容易混淆,不能明确它们的用途和区别。本文浅谈下它们的区别,以帮助更好的理解和运用。
Proxy
interface ProxyBase { void function(); } class Proxy implements ProxyBase { ProxyBase implementation = new Implementation(); @Override public void function() { implementation.function(); } } class Implementation implements ProxyBase { @Override public void function() { System.out.println("Implementation.function()"); } } class Test { public static void main(String[] args) { Proxy p = new Proxy(); p.function(); } }
从例子可以看出,Proxy和Implementation同时实现了接口ProxyBase,真正的功能是由Implementation实例提供的。
State
interface State { void function1(); void function2(); } class StateOne implements State { @Override public void function1() { System.out.println("StateOne.function1()"); } @Override public void function2() { System.out.println("StateOne.function2()"); } } class StateTwo implements State { @Override public void function1() { System.out.println("StateTwo.function1()"); } @Override public void function2() { System.out.println("StateTwo.function2()"); } } class User { State state = new StateOne(); public void setState(State newState) { state = newState; } public void service() { state.function1(); state.function2(); } } class Test { public static void main(String[] args) { User user = new User(); user.service(); user.setState(new StateTwo()); user.service(); } }
State模式提供统一的服务接口,而根据不同的实现,提供不同的行为。
Facade
/* Complex parts */ class CPU { public void freeze() { ... } public void jump(long position) { ... } public void execute() { ... } } class Memory { public void load(long position, byte[] data) { ... } } class HardDrive { public byte[] read(long lba, int size) { ... } } /* Facade */ class Computer { private CPU cpu; private Memory memory; private HardDrive hardDrive; public Computer() { this.cpu = new CPU(); this.memory = new Memory(); this.hardDrive = new HardDrive(); } public void startComputer() { cpu.freeze(); memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE)); cpu.jump(BOOT_ADDRESS); cpu.execute(); } } /* Client */ class You { public static void main(String[] args) { Computer facade = new Computer(); facade.startComputer(); } }
Facade模式:将一个复杂的系统转换成简单易用的服务接口,它在结构上没有要求,更多是强调一种概念。
Adapter
class Adapter { Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } void request() { adaptee.specificRequest(); } } class Adaptee { void specificRequest() { System.out.println("Adaptee.specificRequest()"); } } class Test { public static void main(String[] args) { Adaptee adaptee = new Adaptee(); Adapter adapter = new Adapter(adaptee); adapter.request(); } }
Adapter模式:当接口不匹配时,将接口转换成另一种类型的接口。
Summary
在比较上面的几种模式前,先清楚两个概念:接口和行为。此处的接口指对外所提供的服务。行为指具体的服务实现。
用这两个概念来解释区别,Adapter的本意就是接口改变,行为不变。Proxy,State是接口不变,行为改变。(Proxy可以认为是State的一种特殊情形,在结构上,State有很多服务实现,而Proxy只有一种)
You can refer to http://www.netobjectives.com/PatternRepository/index.php?title=AdapterVersusProxyVersusFacadePatternComparison for more details.