欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

代理模式 & 装饰器模式 (Proxy & Decorator)

程序员文章站 2022-05-04 19:23:51
...

代理模式和装饰器模式虽然概念上区别很大,但是在实现时却又比较相似。

代理模式从概念上讲,就是我想访问一个服务,但是我却不需要知道真正给我提供服务的对象,我只要访问能提供给我服务的代理对象就可以了。

装饰器模式从概念上讲,就是要装饰一个对象,只要把这个对象通过装饰器的构造函数传入,装饰器会做一些额外的装饰。因为装饰器也实现了对象实现的接口,所以就可以像操作对象一样操作装饰器。

代理模式的类图:

代理模式 & 装饰器模式 (Proxy & Decorator)
            
    
    博客分类: 设计模式 代理模式装饰器模式ProxyDecorator 

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屏蔽了它并做了一些额外的服务。

代理模式 & 装饰器模式 (Proxy & Decorator)
            
    
    博客分类: 设计模式 代理模式装饰器模式ProxyDecorator 

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做了额外的装饰。

(******请注意装饰器和代理的初始化位置和方式*****)

装饰器模式应当为所装饰的对象提供增强功能,而代理模式对所代理对象的使用施加控制,并不提供对象本身的增强功能。

装饰器模式关注于在一个对象上动态的添加方法,然而代理模式关注于控制对对象的访问