代理模式和装饰器模式虽然概念上区别很大,但是在实现时却又比较相似。
代理模式从概念上讲,就是我想访问一个服务,但是我却不需要知道真正给我提供服务的对象,我只要访问能提供给我服务的代理对象就可以了。
装饰器模式从概念上讲,就是要装饰一个对象,只要把这个对象通过装饰器的构造函数传入,装饰器会做一些额外的装饰。因为装饰器也实现了对象实现的接口,所以就可以像操作对象一样操作装饰器。
代理模式的类图:
Interface Subject:
package com.mode.interfaces { public interface Subject { function doAction():void; } }
RealSubject
package com.mode.concrete { import com.mode.interfaces.Subject; public class RealSubject implements Subject { public function RealSubject() { } public function doAction():void { trace("Real Subject do Action"); } } }
Proxy:
package com.mode.proxy { import com.mode.concrete.RealSubject; import com.mode.interfaces.Subject; public class Proxy implements Subject { private var subject:Subject; public function doAction():void { if(subject == null) { subject = new RealSubject(); } doSomeActionBefore(); subject.doAction(); doSomeActionAfter(); } private function doSomeActionBefore():void { trace("Proxy do some action before"); } private function doSomeActionAfter():void { trace("Proxy do some action after"); } } }
Client test:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import com.mode.interfaces.Subject; import com.mode.proxy.Proxy; protected function button1_clickHandler(event:MouseEvent):void { var subject:Subject = new com.mode.proxy.Proxy(); subject.doAction(); /* output: Proxy do some action before Real Subject do Action Proxy do some action after */ } ]]> </fx:Script> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <s:Button label="Proxy Demo" click="button1_clickHandler(event)"/> </s:Application>
你会发现我真正访问的是RealSubject的doAction,但是Proxy屏蔽了它并做了一些额外的服务。
Interface of Component:
package com.mode.interfaces { public interface Component { function operation():void; } }
ConcreteComponent:
package com.mode.concrete { import com.mode.interfaces.Component; public class ConcreteComponent implements Component { public function operation():void { trace("concreteComponent operation"); } } }
Decorator:
package com.mode.concrete { import com.mode.interfaces.Component; public class Decorator implements Component { protected var component:Component; public function Decorator(comp:Component) { component = comp; } public function operation():void { component.operation(); } } }
ConcreteDecorator:
package com.mode.concrete { import com.mode.interfaces.Component; public class ConcreteDecorator extends Decorator { private var additionalAttribute:String = "additional Attribute"; public function ConcreteDecorator(comp:Component) { super(comp); } override public function operation():void { super.operation(); addtionalBehavior(); } private function addtionalBehavior():void { trace("Concrete decorator addtioanl behavior = " + additionalAttribute); } } }
APP test:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import com.mode.concrete.ConcreteComponent; import com.mode.concrete.ConcreteDecorator; import com.mode.concrete.Decorator; import com.mode.interfaces.Component; import com.mode.interfaces.Subject; import com.mode.proxy.Proxy; protected function button1_clickHandler(event:MouseEvent):void { var comp:Component = new ConcreteComponent(); var decorator:Decorator = new ConcreteDecorator(comp); decorator.operation(); /* output: concreteComponent operation Concrete decorator addtioanl behavior = additional Attribute */ } ]]> </fx:Script> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <s:Button label="Decorator Demo" click="button1_clickHandler(event)"/> </s:Application>
你会发现是我把component传入了装饰器中,装饰器对component做了额外的装饰。
(******请注意装饰器和代理的初始化位置和方式*****)
装饰器模式应当为所装饰的对象提供增强功能,而代理模式对所代理对象的使用施加控制,并不提供对象本身的增强功能。
装饰器模式关注于在一个对象上动态的添加方法,然而代理模式关注于控制对对象的访问
相关推荐
2、代理模式PROXY PATTERN 3、单例模式SINGLETON PATTERN 4、多例模式MULTITION PATTERN 5、工厂方法模式FACTORY METHOD PATTERN 6、抽象工厂模式ABSTRACT FACTORY PATTERN 7、门面模式FACADE PATTERN 8、适配器...
装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( ...
装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( ...
装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( ...
装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( ...
装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( ...
装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3. 行为型模式 责任链模式(Chain of Responsibility Pattern) 命令模式(Command ...
代理模式(Proxy) 行为型: 责任链模式(Chain of Responsibility) 命令模式(Command) 解释器模式(Interpreter) 迭代器模式(Iterator) 中介者模式(Mediator) 备忘录模式(Memento) 观察者模式(Observer) 状态模式...
Proxy ( 代理模式 ) Chain of Responsibility ( 责任链模式 ) Command ( 命令模式 ) Interpreter ( 解释器模式 ) Iterator ( 迭代器模式 ) Mediator ( 中介者模式 ) Memento ( 备忘录模式 ) Observer ( 观察...
备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) 单例模式(Singleton Pattern) 迭代器模式(Iterator Pattern) 访问者模式(Visitor ...
- 装饰器模式(Decorator) - 桥接模式(Bridge) - 组合模式(Composite) - 外观模式(Facade) - 享元模式(Flyweight) - 观察者模式(Observer) - 模板方法模式(Template Method) - 策略模式(Strategy) - 责任链...
装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( ...
代理模式(Proxy Pattern) 行为型: 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 16. 观察者模式(Observer Pattern) 17. 解释器模式(Interpreter Pattern) 18....
装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3 行为型模式 这些设计模式特别关注对象之间的通信。 责任链模式(Chain of ...
代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern) 17. 解释器模式(Interpreter ...
2、结构模式:Flyweight(共享模式)、Bridge(桥模式)、Decorator(装饰模式)、Composite(组合模式)、Adapter(适配器模式)、Proxy(代理模式)、Facade (外观模式)。3、行为模式:Iterator(迭代模式)、Template(模板...
装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( ...
装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( ...
装饰器模式( Decorator ) 代理模式( Proxy ) 外观模式( Facade ) 桥接模式( Bridge ) 组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( ...