首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

关于相干"代理"模式的学习

2012-10-28 
关于相关代理模式的学习设计模式中有一类是跟代理相关的模式:Proxy, State, Facade, Adapter。初学者很容

关于相关"代理"模式的学习

设计模式中有一类是跟代理相关的模式:Proxy, State, Facade, Adapter。初学者很容易混淆,不能明确它们的用途和区别。本文浅谈下它们的区别,以帮助更好的理解和运用。

Proxyview sourceprint?01interface ProxyBase {02????void function();03}04?05class Proxy implements ProxyBase {06????ProxyBase implementation = new Implementation();07?????08????@Override09????public void function() {10????????implementation.function();11????}12}13?14class Implementation implements ProxyBase {15????@Override16????public void function() {17????????System.out.println("Implementation.function()");18????}19}20?21class Test {22????public static void main(String[] args) {23????????Proxy p = new Proxy();24????????p.function();25????}26}

从例子可以看出,Proxy和Implementation同时实现了接口ProxyBase,真正的功能是由Implementation实例提供的。

Stateview sourceprint?01interface State {02????void function1();03????void function2();04}05?06class StateOne implements State {07????@Override08????public void function1() {09????????System.out.println("StateOne.function1()");10????}11?????12????@Override13????public void function2() {14????????System.out.println("StateOne.function2()");15????}16}17?18class StateTwo implements State {19????@Override20????public void function1() {21????????System.out.println("StateTwo.function1()");22????}23?????24????@Override25????public void function2() {26????????System.out.println("StateTwo.function2()");27????}28}29?30class 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?43class 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??03class CPU {04??05????public void freeze() { ... }06????public void jump(long position) { ... }07????public void execute() { ... }08??09}10??11class Memory {12??13????public void load(long position, byte[] data) { ... }14??15}16??17class HardDrive {18??19????public byte[] read(long lba, int size) { ... }20??21}22??23/* Facade */24??25class 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??48class You {49??50????public static void main(String[] args) {51????????Computer facade = new Computer();52????facade.startComputer();53????}54??55}

Facade模式:将一个复杂的系统转换成简单易用的服务接口,它在结构上没有要求,更多是强调一种概念。

Adapterview sourceprint?01class 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?13class Adaptee {14????void specificRequest() {15????????System.out.println("Adaptee.specificRequest()");16????}17}18?19class 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只有一种)

热点排行