代理模式和装饰器模式虽然概念上区别很大,但是在实现时却又比较相似。
代理模式从概念上讲,就是我想访问一个服务,但是我却不需要知道真正给我提供服务的对象,我只要访问能提供给我服务的代理对象就可以了。
装饰器模式从概念上讲,就是要装饰一个对象,只要把这个对象通过装饰器的构造函数传入,装饰器会做一些额外的装饰。因为装饰器也实现了对象实现的接口,所以就可以像操作对象一样操作装饰器。
代理模式的类图:
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 ) 模板方法模式( ...
设计模式Golang实现《研磨设计模式》读书笔记Go语言设计模式Go语言设计...代理模式(Proxy)组合模式(Composite)享元模式(Flyweight)装饰模式(Decorator)桥模式(Bridge)行为模式中介者模式(Mediator)观察者...
装饰器模式( 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 ) 模板方法模式( ...
代理模式(Proxy) 行为型: 责任链模式(Chain of Responsibility) 命令模式(Command) 解释器模式(Interpreter) 迭代器模式(Iterator) 中介者模式(Mediator) 备忘录模式(Memento) 观察者模式(Observer) 状态模式...
Proxy ( 代理模式 ) Chain of Responsibility ( 责任链模式 ) Command ( 命令模式 ) Interpreter ( 解释器模式 ) Iterator ( 迭代器模式 ) Mediator ( 中介者模式 ) Memento ( 备忘录模式 ) Observer ( 观察...
其中包括代理模式(Proxy)、装饰器模式(Decorator)、适配器模式(Adapter)、桥接模式(Bridge)、组合模式(Composite)、外观模式(Facade)和享元模式(Flyweight)。这些模式帮助我们在不修改原有代码的情况...
装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3. 行为型模式 责任链模式(Chain of Responsibility Pattern) 命令模式(Command ...
装饰器模式(Decorator Pattern)是一种结构模式,它动态地给对象添加新的职责。通过包装原对象并保持相同的接口,装饰器可以在不修改原对象的情况下扩展其功能。这使得设计更加灵活,可以避免使用继承带来的类爆炸...
- 装饰器模式(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....
2. 结构型模式:这些模式处理对象组合和结构,如适配器模式(Adapter)、桥接模式(Bridge)、组合模式(Composite)、装饰器模式(Decorator)、外观模式(Facade)、享元模式(Flyweight)和代理模式(Proxy)。...
2. 结构型模式:这些模式处理类和对象的组合,如适配器模式(Adapter)、装饰器模式(Decorator)和代理模式(Proxy)。适配器模式可以将不兼容的接口转换为可用的接口,而装饰器模式允许动态地给对象添加新的行为或...
结构模式是指在对象之间的关系和结构上使用的模式,包括 Flyweight(共享模式)、Bridge(桥模式)、Decorator(装饰模式)、Composite(组合模式)、Adapter(适配器模式)、Proxy(代理模式)、Facade(外观模式)...
C#面向对象设计模式纵横谈(13):Proxy 代理模式(结构型模式) C#面向对象设计模式纵横谈(14):Chain of Responsibility 职责链模式(行为型模式) C#面向对象设计模式纵横谈(15):(行为型模式) Command 命令模式 ...
五、装饰器模式(Decorator) 装饰器模式允许向一个对象添加新的行为或责任,而不必修改其原始代码。C#中,通过继承和组合可以实现装饰器,尤其在需要动态改变对象行为时非常有用。 六、观察者模式(Observer) ...