<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
	>
<channel>
	<title>Comments on: ActionScript 3.0 Easy and Practical Decorator Design Pattern</title>
	<atom:link href="http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Mon, 08 Mar 2010 23:14:21 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Curro</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-3828</link>
		<dc:creator>Curro</dc:creator>
		<pubDate>Wed, 13 Jan 2010 23:03:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-3828</guid>
		<description>Hi again,

I&#039;ve seen I posted the DragForm class twice instead of the client class:
Here it goes:



package 
{
	import flash.display.*;
	import flash.net.URLRequest;
	import flash.media.Sound;
	import flash.events.MouseEvent;
	import flash.events.IOErrorEvent;
	public class Client extends Sprite
	{
		private var aTargets:Array = [];
		private var aDrags:Array = [];


		public function Client()
		{
			var tgt1 = new tg();// a movie clip associated with the Target class
			tgt1.x = 100;
			tgt1.y = 150;
			addChild(tgt1)
			aTargets.push(tgt1);
			
			var drag1 = new DragTextQuestion();// DragTextQuestion is a concrete component
			drag1.setUp(&quot;nice&quot;, tgt1, new LooseCheck);// We need a word, a target and a check strategy

			
			/******** This is what makes it function properly ****
			if I place this code after the decorations things go wrong *****/
			drag1.x = drag1.origX = 300;
			drag1.y = drag1.origY =  150;
			/*******/
			
			// preparing for the sound decorator
			
			var sound = new Sound(new URLRequest(&quot;audio/nice.mp3&quot;));
			drag1 = new DrgQSoundDecorator(drag1);
			drag1.setSound(sound);
			
			// second decoration (filters)
			drag1 = new DrgQFilterDecorator(drag1);
			
			// launch question
			drag1.addEventListener(MouseEvent.MOUSE_DOWN, drag1.dragMovie);
			drag1.addEventListener(MouseEvent.MOUSE_UP, drag1.dropMovie);
			
			//get the drag into an array for later adding to stage and checking
			aDrags.push(drag1);



			// Second drag object will have a different checking strategy
			
			var tgt2 = new tg();// a movie clip associated with the Target class
			tgt2.x = 100;
			tgt2.y = 250;
			
			aTargets.push(tgt2);
			
			var drag2 = new DragTextQuestion();// DragTextQuestion is a concrete component
			drag2.setUp(&quot;horrible&quot;, tgt2, new TightCheck);// We need a word, a target and a check strategy
		
			
			drag2.x = drag2.origX = 300;
			drag2.y = drag2.origY = 250;
			
			
			// preparing for the sound decorator
			var sound2 = new Sound(new URLRequest(&quot;audio/horrible.mp3&quot;));
			drag2 = new DrgQSoundDecorator(drag2);
			drag2.setSound(sound2);
			
			// second decoration (filters)
			drag2 = new DrgQFilterDecorator(drag2);
			
			
					
			// launch question
			drag2.addEventListener(MouseEvent.MOUSE_DOWN, drag2.dragMovie);
			drag2.addEventListener(MouseEvent.MOUSE_UP, drag2.dropMovie);
			
			//get the drag into an array for later checking
			aDrags.push(drag2);
			
			// Add targets to stage
			
			
			
			// set up for checking
			checkButton.addEventListener(MouseEvent.CLICK, checkH);
			
			placeTargets();
			placeDrags();
		}
		

		function checkH(e:MouseEvent)
		{
			for (var i = 0; i &lt; aDrags.length; i++)
			{
				if (aDrags[i].correct == false)
				{
					aDrags[i] = new DrgQWrongDecorator(aDrags[i]);
					addChild(aDrags[i]);
				}
				else
				{
					aDrags[i] = new DrgQCorrectDecorator(aDrags[i]);
					addChild(aDrags[i]);
				}

			}
		}
		
		private function placeTargets()
		{
			for (var i = 0; i &lt; aTargets.length; i++)
			{
				addChild(aTargets[i]);
			}
		}
		private function placeDrags()
		{
			for (var i = 0; i &lt; aDrags.length; i++)
			{
				addChild(aDrags[i]);
			}
		}

	}
}
</description>
		<content:encoded><![CDATA[<p>Hi again,</p>
<p>I&#8217;ve seen I posted the DragForm class twice instead of the client class:<br />
Here it goes:</p>
<p>package<br />
{<br />
	import flash.display.*;<br />
	import flash.net.URLRequest;<br />
	import flash.media.Sound;<br />
	import flash.events.MouseEvent;<br />
	import flash.events.IOErrorEvent;<br />
	public class Client extends Sprite<br />
	{<br />
		private var aTargets:Array = [];<br />
		private var aDrags:Array = [];</p>
<p>		public function Client()<br />
		{<br />
			var tgt1 = new tg();// a movie clip associated with the Target class<br />
			tgt1.x = 100;<br />
			tgt1.y = 150;<br />
			addChild(tgt1)<br />
			aTargets.push(tgt1);</p>
<p>			var drag1 = new DragTextQuestion();// DragTextQuestion is a concrete component<br />
			drag1.setUp(&#8221;nice&#8221;, tgt1, new LooseCheck);// We need a word, a target and a check strategy</p>
<p>			/******** This is what makes it function properly ****<br />
			if I place this code after the decorations things go wrong *****/<br />
			drag1.x = drag1.origX = 300;<br />
			drag1.y = drag1.origY =  150;<br />
			/*******/</p>
<p>			// preparing for the sound decorator</p>
<p>			var sound = new Sound(new URLRequest(&#8221;audio/nice.mp3&#8243;));<br />
			drag1 = new DrgQSoundDecorator(drag1);<br />
			drag1.setSound(sound);</p>
<p>			// second decoration (filters)<br />
			drag1 = new DrgQFilterDecorator(drag1);</p>
<p>			// launch question<br />
			drag1.addEventListener(MouseEvent.MOUSE_DOWN, drag1.dragMovie);<br />
			drag1.addEventListener(MouseEvent.MOUSE_UP, drag1.dropMovie);</p>
<p>			//get the drag into an array for later adding to stage and checking<br />
			aDrags.push(drag1);</p>
<p>			// Second drag object will have a different checking strategy</p>
<p>			var tgt2 = new tg();// a movie clip associated with the Target class<br />
			tgt2.x = 100;<br />
			tgt2.y = 250;</p>
<p>			aTargets.push(tgt2);</p>
<p>			var drag2 = new DragTextQuestion();// DragTextQuestion is a concrete component<br />
			drag2.setUp(&#8221;horrible&#8221;, tgt2, new TightCheck);// We need a word, a target and a check strategy</p>
<p>			drag2.x = drag2.origX = 300;<br />
			drag2.y = drag2.origY = 250;</p>
<p>			// preparing for the sound decorator<br />
			var sound2 = new Sound(new URLRequest(&#8221;audio/horrible.mp3&#8243;));<br />
			drag2 = new DrgQSoundDecorator(drag2);<br />
			drag2.setSound(sound2);</p>
<p>			// second decoration (filters)<br />
			drag2 = new DrgQFilterDecorator(drag2);</p>
<p>			// launch question<br />
			drag2.addEventListener(MouseEvent.MOUSE_DOWN, drag2.dragMovie);<br />
			drag2.addEventListener(MouseEvent.MOUSE_UP, drag2.dropMovie);</p>
<p>			//get the drag into an array for later checking<br />
			aDrags.push(drag2);</p>
<p>			// Add targets to stage</p>
<p>			// set up for checking<br />
			checkButton.addEventListener(MouseEvent.CLICK, checkH);</p>
<p>			placeTargets();<br />
			placeDrags();<br />
		}</p>
<p>		function checkH(e:MouseEvent)<br />
		{<br />
			for (var i = 0; i &lt; aDrags.length; i++)<br />
			{<br />
				if (aDrags[i].correct == false)<br />
				{<br />
					aDrags[i] = new DrgQWrongDecorator(aDrags[i]);<br />
					addChild(aDrags[i]);<br />
				}<br />
				else<br />
				{<br />
					aDrags[i] = new DrgQCorrectDecorator(aDrags[i]);<br />
					addChild(aDrags[i]);<br />
				}</p>
<p>			}<br />
		}</p>
<p>		private function placeTargets()<br />
		{<br />
			for (var i = 0; i &lt; aTargets.length; i++)<br />
			{<br />
				addChild(aTargets[i]);<br />
			}<br />
		}<br />
		private function placeDrags()<br />
		{<br />
			for (var i = 0; i &lt; aDrags.length; i++)<br />
			{<br />
				addChild(aDrags[i]);<br />
			}<br />
		}</p>
<p>	}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Curro</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-3827</link>
		<dc:creator>Curro</dc:creator>
		<pubDate>Wed, 13 Jan 2010 22:20:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-3827</guid>
		<description>I forgot; you will also need a button named &quot;checkButton&quot; on the fla.</description>
		<content:encoded><![CDATA[<p>I forgot; you will also need a button named &#8220;checkButton&#8221; on the fla.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Curro</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-3826</link>
		<dc:creator>Curro</dc:creator>
		<pubDate>Wed, 13 Jan 2010 22:17:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-3826</guid>
		<description>Hi again Bill,

here it goes! You will also need two mp3 files named nice.mp3 and horrible.mp3 in a folder named audio. In my app these contain the pronunciation of the words.

You will also need a fla file with a rectanguar movieclip (extending sprite) named &quot;tg&quot; associated with the class Target.

In the real application I have an array of words and one of targets(normally photographs or drawings representing the words).

Thank you for your help and patience

Curro


/*

The component class:

package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.*;
	import flash.events.Event;
	public class DragQuestion extends Sprite
	{
		protected  var initX;
		protected var initY;
		private var userAnswer:*;
		private var answer:*;
		private var isRight:Boolean;
		private var checkStrategy:CheckStrategy;
		private var base:Sprite = new Sprite();
				
		public function setSprite(sprite:Sprite)
		{
			base = sprite;
			addChild(base);
		}
		public function dragMovie(event:MouseEvent):void
		{
			this.startDrag();
		}
		
		public function dropMovie(event:MouseEvent):void
		{
			this.stopDrag();
			checkTarget();
			
		}
		public function setStrategy(checkStrategy:CheckStrategy)
		{
			this.checkStrategy = checkStrategy;
			this.checkStrategy.setTarget(this);

		}
		public function setTarget(t:*)
		{
			this.answer = t;
		}

		public function getAnswer():*
		{
			return this.answer;
		}
		public function get origX():Number
		{
			return initX;
		}
		public function set origX(n:Number)
		{
			this.initX = n;
		}
		public function get origY():Number
		{
			return initY;
		}
		public function set origY(n:Number)
		{
			this.initY = n;
		}
		
		public function set correct(b:Boolean)
		{
			this.isRight = b;
		}
		public function get correct():Boolean
		{
			return this.isRight;
		}
		protected function checkTarget()
		{
			checkStrategy.checkTarget();
			this.isRight = this.isRight;
		}
		
		
	}
}
		

******************

The  Abstract Decorator class:

package
{
	import flash.events.MouseEvent;
	import flash.media.Sound;
	
	public class DrgQDecorator extends DragQuestion
	{
		public function setSound(s:Sound):void{}
		override public function dragMovie(e:MouseEvent):void{}
		override public function dropMovie(e:MouseEvent):void{}
	}
}


************

The concrete component class:

package
{
	public class DragTextQuestion extends DragQuestion
	{
		private var word:String;
		
		public function setUp(word:String, target:*, checkStrategy:CheckStrategy)
		{
			this.word = word;
			this.buttonMode = true;
			setSprite( new DragForm(word));
			setStrategy(checkStrategy);
			setTarget(target);
		}
		
	}
}



***************

Check strategy interface:

package
{
	public interface CheckStrategy 
	{
		function checkTarget():void;
		function setTarget(target:Object):void;
		
	}
}

**************
Concrete Strategy 1:

package
{
	import flash.geom.*;
	public class LooseCheck implements CheckStrategy
	{
		private var question:DragQuestion;
		
		public function setTarget(target:Object):void
		{
			this.question = target as DragQuestion;
		}
		public function checkTarget():void
		{
			if (question.dropTarget != null)
			{
				if (question.dropTarget.parent.hasOwnProperty(&quot;isTarget&quot;))
				{
					if(question.dropTarget.parent == question.getAnswer())
					{
						question.correct =true;
					}
					else
					{
						question.correct =false;
					}
					question.x = question.dropTarget.parent.x;
					question.y = question.dropTarget.parent.y;
}
				else
				{
					question.x = question.origX;
					question.y = question.origY;
				}
			}
			else
			{
				question.x = question.origX;
				question.y = question.origY;
				question.correct = false;
			}
			trace(&quot;Is Right?: &quot; + question.correct);
		}
		
	}
}


********************

Concrete Check strategy 2:

package
{
	public class TightCheck implements CheckStrategy
	{
		private var question:DragQuestion;
		
		public function setTarget(target:Object):void
		{
			this.question = target as DragQuestion;
		}
		public function checkTarget():void
		{
			
				if (question.hitTestObject(question.getAnswer()))
				{
					question.correct = true;
					question.x = question.dropTarget.parent.x;
					question.y = question.dropTarget.parent.y;
					
				}
				else
				{
					question.correct = false;
					question.x = question.origX;
					question.y = question.origY;
				}
			
			trace(question.correct);
		}
		
	}
}


*********************

Concrete Decorator 1:

package
{
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	
	public class DrgQSoundDecorator extends DrgQDecorator
	{
		private var dragQ:DragQuestion;
		private var sound:Sound;
		
		public function DrgQSoundDecorator(o:DragQuestion)
		{
			this.dragQ = o;
			addChild(dragQ);

		}
		override public function dragMovie(e:MouseEvent):void
		{
			
			this.dragQ.dragMovie(e);
		}
		override public function dropMovie(e:MouseEvent):void
		{
			sound.play();
			this.dragQ.dropMovie(e);
		}
		override public function get correct():Boolean
		{
			return this.dragQ.correct;
		}
		override public function setSound(s:Sound):void
		{
			this.sound = s;
		}
	}
}


****************

Concrete Decorator 2:

package
{
	import flash.events.MouseEvent;
	import flash.filters.*;
	
	public class DrgQFilterDecorator extends DrgQDecorator
	{
		private var dragQ:DragQuestion;
		var bevelFilter:BevelFilter = new BevelFilter();
		var shadowFilter:DropShadowFilter = new DropShadowFilter();
		var glowFilter:GlowFilter = new GlowFilter();
			
		
		public function DrgQFilterDecorator(o:DragQuestion)
		{
			this.dragQ = o;
			bevelFilter.shadowAlpha =0.5;
			bevelFilter.distance = 2;
			dragQ.filters = [bevelFilter, shadowFilter];
			addChild(dragQ);
		}
		override public function dragMovie(e:MouseEvent):void
		{
			
			glowFilter.color = 0xAA0000;
			glowFilter.blurX = 32;
			glowFilter.blurY = 32;
			glowFilter.strength = .6;
			
			dragQ.filters = [glowFilter];
			this.dragQ.dragMovie(e);
		}
		override public function dropMovie(e:MouseEvent):void
		{
			
			this.dragQ.filters =[bevelFilter, shadowFilter];
			this.dragQ.dropMovie(e);
		}
		override public function get correct():Boolean
		{
			return this.dragQ.correct;
		}
		
	}
}


*****************

Concrete Decorator 3:

package
{
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.display.*;
	import flash.geom.Point;
	import flash.events.MouseEvent;
	
	//import flash.filters.DropShadowFilter
	
	public class DrgQCorrectDecorator extends DrgQDecorator
	{
		private var dragQ:DragQuestion;
		private var sound:Sound;
		var figure:Shape = new Shape();
		
		public function DrgQCorrectDecorator(o:DragQuestion)
		{
			this.dragQ = o;
			addChild(dragQ);
			var rect = getBounds(dragQ);
			var pt:Point = new Point(rect.x +rect.width - 10, rect.y  + 10)
			//figure.graphics.beginFill();
			figure.graphics.lineStyle(4,0x33CC33);
			figure.graphics.moveTo(pt.x,pt.y);
			var pt2:Point = new Point(pt.x + 8, pt.y -8);
			figure.graphics.lineTo(pt2.x,pt2.y);
			var pt3 = new Point(pt.x - 5, pt.y -5);
			figure.graphics.moveTo(pt.x, pt.y);
			figure.graphics.lineTo(pt3.x, pt3.y);

			dragQ.addChild(figure);
			trace(&quot;getBounds(dragQ)&quot; + getBounds(dragQ));
			trace(&quot;getBounds(this)&quot; + getBounds(this));
			this.addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);
								
		}
		override public function dragMovie(e:MouseEvent):void
		{
			try{
				dragQ.removeChild(figure);
			}
			catch(e:Error)
			{
			}
			this.dragQ.dragMovie(e);
		}
		override public function dropMovie(e:MouseEvent):void
		{
			this.dragQ.dropMovie(e);

		}
		override public function get correct():Boolean
		{
			if(this.dragQ.correct)
			{
				this.dragQ.removeEventListener(MouseEvent.MOUSE_DOWN, dragMovie);
				this.dragQ.buttonMode = false;
			}
			return this.dragQ.correct;
		}
		
	}
}


************************+

Concrete Decorator 4:

package
{
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.display.*;
	import flash.geom.Point;
	import flash.events.MouseEvent;
	
	
	public class DrgQWrongDecorator extends DrgQDecorator
	{
		private var dragQ:DragQuestion;
		private var sound:Sound;
		var figure:Shape = new Shape();
		
		public function DrgQWrongDecorator(o:DragQuestion)
		{
			this.dragQ = o;
			addChild(dragQ);
			
			var rect = getBounds(dragQ);
			var pt:Point = new Point(rect.x +rect.width - 10, rect.y  + 10)
			figure.graphics.lineStyle(4,0xFFBB00);
			figure.graphics.moveTo(pt.x,pt.y);
			var pt2:Point = new Point(pt.x + 8, pt.y -8);
			figure.graphics.lineTo(pt2.x,pt2.y);
			var pt3 = new Point(pt.x , pt.y-8 );
			figure.graphics.moveTo(pt.x+8, pt.y);
			figure.graphics.lineTo(pt3.x, pt3.y);
			
			dragQ.addChild(figure);
			this.addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);
								
		}
		override public function dragMovie(e:MouseEvent):void
		{
			try{
				dragQ.removeChild(figure);
			}
			catch(e:Error)
			{
			}
			this.dragQ.dragMovie(e);
		}
		override public function dropMovie(e:MouseEvent):void
		{
			this.dragQ.dropMovie(e);

		}
		override public function get correct():Boolean
		{
			return this.dragQ.correct;
		}
		
	}
}


*******************

Target Class:

package
{
	import flash.display.Sprite;
	
	public class Target extends Sprite
	{
		public var isTarget:Boolean = true;
	}
}

***************************

Helper class to give form to dragable words:

package
{
	import flash.text.*;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	public class DragForm extends Sprite
	{
		private var word:String;
		private var field:TextField;
		
		public function DragForm(word:String)
		{
			this.word = word;
			display();
		}
		public function setUp()
		{
			
			
		}

		private function display()
		{
			field = new TextField();
			field.multiline = false;
			field.selectable = false;
			
			field.autoSize = TextFieldAutoSize.CENTER;
			field.type = TextFieldType.DYNAMIC;
			field.text = word;
			//field.background = true;
			//field.border = true;


			var format:TextFormat = new TextFormat();
			format.font = &quot;Arial&quot;;
			format.size = 18;
			format.color = 0xFFFFFF;

			format.bold = true;
			field.setTextFormat(format);


			var shape:Shape = new Shape();
			var bgColour:uint = 0xCC3300;
			shape.graphics.beginFill(bgColour);
			shape.graphics.drawRoundRect(- field.width/2 -2, (-field.height/2)-2 ,field.width +4  ,field.height+4 ,30);
			shape.graphics.endFill();
			addChild(shape);
			addChild(field);

			field.x =  -  field.width / 2;
			field.y =  -  field.height / 2;
			var theMask = new Sprite();
			theMask.graphics.beginFill(0x000000, 0.0);
			theMask.graphics.drawRect(-field.width/2,-field.height/2,field.width,field.height);
			theMask.graphics.endFill();
			addChild(theMask);
		}

	}
}


********************

finally the client class. It has only two drag questions with a different check strategy each. One(TightCheck) doesn&#039;t let the dragWord on the target unless it is the right one. The other lets  it stay on and you see if it is correct with visual clues.


package
{
	import flash.text.*;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	public class DragForm extends Sprite
	{
		private var word:String;
		private var field:TextField;
		
		public function DragForm(word:String)
		{
			this.word = word;
			display();
		}
		public function setUp()
		{
			
			
		}

		private function display()
		{
			field = new TextField();
			field.multiline = false;
			field.selectable = false;
			
			field.autoSize = TextFieldAutoSize.CENTER;
			field.type = TextFieldType.DYNAMIC;
			field.text = word;
			//field.background = true;
			//field.border = true;


			var format:TextFormat = new TextFormat();
			format.font = &quot;Arial&quot;;
			format.size = 18;
			format.color = 0xFFFFFF;

			format.bold = true;
			field.setTextFormat(format);


			var shape:Shape = new Shape();
			var bgColour:uint = 0xCC3300;
			shape.graphics.beginFill(bgColour);
			shape.graphics.drawRoundRect(- field.width/2 -2, (-field.height/2)-2 ,field.width +4  ,field.height+4 ,30);
			shape.graphics.endFill();
			addChild(shape);
			addChild(field);

			field.x =  -  field.width / 2;
			field.y =  -  field.height / 2;
			var theMask = new Sprite();
			theMask.graphics.beginFill(0x000000, 0.0);
			theMask.graphics.drawRect(-field.width/2,-field.height/2,field.width,field.height);
			theMask.graphics.endFill();
			addChild(theMask);
		}

	}
}


*/

</description>
		<content:encoded><![CDATA[<p>Hi again Bill,</p>
<p>here it goes! You will also need two mp3 files named nice.mp3 and horrible.mp3 in a folder named audio. In my app these contain the pronunciation of the words.</p>
<p>You will also need a fla file with a rectanguar movieclip (extending sprite) named &#8220;tg&#8221; associated with the class Target.</p>
<p>In the real application I have an array of words and one of targets(normally photographs or drawings representing the words).</p>
<p>Thank you for your help and patience</p>
<p>Curro</p>
<p>/*</p>
<p>The component class:</p>
<p>package<br />
{<br />
	import flash.display.Sprite;<br />
	import flash.events.MouseEvent;<br />
	import flash.geom.*;<br />
	import flash.events.Event;<br />
	public class DragQuestion extends Sprite<br />
	{<br />
		protected  var initX;<br />
		protected var initY;<br />
		private var userAnswer:*;<br />
		private var answer:*;<br />
		private var isRight:Boolean;<br />
		private var checkStrategy:CheckStrategy;<br />
		private var base:Sprite = new Sprite();</p>
<p>		public function setSprite(sprite:Sprite)<br />
		{<br />
			base = sprite;<br />
			addChild(base);<br />
		}<br />
		public function dragMovie(event:MouseEvent):void<br />
		{<br />
			this.startDrag();<br />
		}</p>
<p>		public function dropMovie(event:MouseEvent):void<br />
		{<br />
			this.stopDrag();<br />
			checkTarget();</p>
<p>		}<br />
		public function setStrategy(checkStrategy:CheckStrategy)<br />
		{<br />
			this.checkStrategy = checkStrategy;<br />
			this.checkStrategy.setTarget(this);</p>
<p>		}<br />
		public function setTarget(t:*)<br />
		{<br />
			this.answer = t;<br />
		}</p>
<p>		public function getAnswer():*<br />
		{<br />
			return this.answer;<br />
		}<br />
		public function get origX():Number<br />
		{<br />
			return initX;<br />
		}<br />
		public function set origX(n:Number)<br />
		{<br />
			this.initX = n;<br />
		}<br />
		public function get origY():Number<br />
		{<br />
			return initY;<br />
		}<br />
		public function set origY(n:Number)<br />
		{<br />
			this.initY = n;<br />
		}</p>
<p>		public function set correct(b:Boolean)<br />
		{<br />
			this.isRight = b;<br />
		}<br />
		public function get correct():Boolean<br />
		{<br />
			return this.isRight;<br />
		}<br />
		protected function checkTarget()<br />
		{<br />
			checkStrategy.checkTarget();<br />
			this.isRight = this.isRight;<br />
		}</p>
<p>	}<br />
}</p>
<p>******************</p>
<p>The  Abstract Decorator class:</p>
<p>package<br />
{<br />
	import flash.events.MouseEvent;<br />
	import flash.media.Sound;</p>
<p>	public class DrgQDecorator extends DragQuestion<br />
	{<br />
		public function setSound(s:Sound):void{}<br />
		override public function dragMovie(e:MouseEvent):void{}<br />
		override public function dropMovie(e:MouseEvent):void{}<br />
	}<br />
}</p>
<p>************</p>
<p>The concrete component class:</p>
<p>package<br />
{<br />
	public class DragTextQuestion extends DragQuestion<br />
	{<br />
		private var word:String;</p>
<p>		public function setUp(word:String, target:*, checkStrategy:CheckStrategy)<br />
		{<br />
			this.word = word;<br />
			this.buttonMode = true;<br />
			setSprite( new DragForm(word));<br />
			setStrategy(checkStrategy);<br />
			setTarget(target);<br />
		}</p>
<p>	}<br />
}</p>
<p>***************</p>
<p>Check strategy interface:</p>
<p>package<br />
{<br />
	public interface CheckStrategy<br />
	{<br />
		function checkTarget():void;<br />
		function setTarget(target:Object):void;</p>
<p>	}<br />
}</p>
<p>**************<br />
Concrete Strategy 1:</p>
<p>package<br />
{<br />
	import flash.geom.*;<br />
	public class LooseCheck implements CheckStrategy<br />
	{<br />
		private var question:DragQuestion;</p>
<p>		public function setTarget(target:Object):void<br />
		{<br />
			this.question = target as DragQuestion;<br />
		}<br />
		public function checkTarget():void<br />
		{<br />
			if (question.dropTarget != null)<br />
			{<br />
				if (question.dropTarget.parent.hasOwnProperty(&#8221;isTarget&#8221;))<br />
				{<br />
					if(question.dropTarget.parent == question.getAnswer())<br />
					{<br />
						question.correct =true;<br />
					}<br />
					else<br />
					{<br />
						question.correct =false;<br />
					}<br />
					question.x = question.dropTarget.parent.x;<br />
					question.y = question.dropTarget.parent.y;<br />
}<br />
				else<br />
				{<br />
					question.x = question.origX;<br />
					question.y = question.origY;<br />
				}<br />
			}<br />
			else<br />
			{<br />
				question.x = question.origX;<br />
				question.y = question.origY;<br />
				question.correct = false;<br />
			}<br />
			trace(&#8221;Is Right?: &#8221; + question.correct);<br />
		}</p>
<p>	}<br />
}</p>
<p>********************</p>
<p>Concrete Check strategy 2:</p>
<p>package<br />
{<br />
	public class TightCheck implements CheckStrategy<br />
	{<br />
		private var question:DragQuestion;</p>
<p>		public function setTarget(target:Object):void<br />
		{<br />
			this.question = target as DragQuestion;<br />
		}<br />
		public function checkTarget():void<br />
		{</p>
<p>				if (question.hitTestObject(question.getAnswer()))<br />
				{<br />
					question.correct = true;<br />
					question.x = question.dropTarget.parent.x;<br />
					question.y = question.dropTarget.parent.y;</p>
<p>				}<br />
				else<br />
				{<br />
					question.correct = false;<br />
					question.x = question.origX;<br />
					question.y = question.origY;<br />
				}</p>
<p>			trace(question.correct);<br />
		}</p>
<p>	}<br />
}</p>
<p>*********************</p>
<p>Concrete Decorator 1:</p>
<p>package<br />
{<br />
	import flash.events.MouseEvent;<br />
	import flash.media.Sound;<br />
	import flash.net.URLRequest;</p>
<p>	public class DrgQSoundDecorator extends DrgQDecorator<br />
	{<br />
		private var dragQ:DragQuestion;<br />
		private var sound:Sound;</p>
<p>		public function DrgQSoundDecorator(o:DragQuestion)<br />
		{<br />
			this.dragQ = o;<br />
			addChild(dragQ);</p>
<p>		}<br />
		override public function dragMovie(e:MouseEvent):void<br />
		{</p>
<p>			this.dragQ.dragMovie(e);<br />
		}<br />
		override public function dropMovie(e:MouseEvent):void<br />
		{<br />
			sound.play();<br />
			this.dragQ.dropMovie(e);<br />
		}<br />
		override public function get correct():Boolean<br />
		{<br />
			return this.dragQ.correct;<br />
		}<br />
		override public function setSound(s:Sound):void<br />
		{<br />
			this.sound = s;<br />
		}<br />
	}<br />
}</p>
<p>****************</p>
<p>Concrete Decorator 2:</p>
<p>package<br />
{<br />
	import flash.events.MouseEvent;<br />
	import flash.filters.*;</p>
<p>	public class DrgQFilterDecorator extends DrgQDecorator<br />
	{<br />
		private var dragQ:DragQuestion;<br />
		var bevelFilter:BevelFilter = new BevelFilter();<br />
		var shadowFilter:DropShadowFilter = new DropShadowFilter();<br />
		var glowFilter:GlowFilter = new GlowFilter();</p>
<p>		public function DrgQFilterDecorator(o:DragQuestion)<br />
		{<br />
			this.dragQ = o;<br />
			bevelFilter.shadowAlpha =0.5;<br />
			bevelFilter.distance = 2;<br />
			dragQ.filters = [bevelFilter, shadowFilter];<br />
			addChild(dragQ);<br />
		}<br />
		override public function dragMovie(e:MouseEvent):void<br />
		{</p>
<p>			glowFilter.color = 0xAA0000;<br />
			glowFilter.blurX = 32;<br />
			glowFilter.blurY = 32;<br />
			glowFilter.strength = .6;</p>
<p>			dragQ.filters = [glowFilter];<br />
			this.dragQ.dragMovie(e);<br />
		}<br />
		override public function dropMovie(e:MouseEvent):void<br />
		{</p>
<p>			this.dragQ.filters =[bevelFilter, shadowFilter];<br />
			this.dragQ.dropMovie(e);<br />
		}<br />
		override public function get correct():Boolean<br />
		{<br />
			return this.dragQ.correct;<br />
		}</p>
<p>	}<br />
}</p>
<p>*****************</p>
<p>Concrete Decorator 3:</p>
<p>package<br />
{<br />
	import flash.events.MouseEvent;<br />
	import flash.media.Sound;<br />
	import flash.net.URLRequest;<br />
	import flash.display.*;<br />
	import flash.geom.Point;<br />
	import flash.events.MouseEvent;</p>
<p>	//import flash.filters.DropShadowFilter</p>
<p>	public class DrgQCorrectDecorator extends DrgQDecorator<br />
	{<br />
		private var dragQ:DragQuestion;<br />
		private var sound:Sound;<br />
		var figure:Shape = new Shape();</p>
<p>		public function DrgQCorrectDecorator(o:DragQuestion)<br />
		{<br />
			this.dragQ = o;<br />
			addChild(dragQ);<br />
			var rect = getBounds(dragQ);<br />
			var pt:Point = new Point(rect.x +rect.width &#8211; 10, rect.y  + 10)<br />
			//figure.graphics.beginFill();<br />
			figure.graphics.lineStyle(4,0&#215;33CC33);<br />
			figure.graphics.moveTo(pt.x,pt.y);<br />
			var pt2:Point = new Point(pt.x + 8, pt.y -8);<br />
			figure.graphics.lineTo(pt2.x,pt2.y);<br />
			var pt3 = new Point(pt.x &#8211; 5, pt.y -5);<br />
			figure.graphics.moveTo(pt.x, pt.y);<br />
			figure.graphics.lineTo(pt3.x, pt3.y);</p>
<p>			dragQ.addChild(figure);<br />
			trace(&#8221;getBounds(dragQ)&#8221; + getBounds(dragQ));<br />
			trace(&#8221;getBounds(this)&#8221; + getBounds(this));<br />
			this.addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);</p>
<p>		}<br />
		override public function dragMovie(e:MouseEvent):void<br />
		{<br />
			try{<br />
				dragQ.removeChild(figure);<br />
			}<br />
			catch(e:Error)<br />
			{<br />
			}<br />
			this.dragQ.dragMovie(e);<br />
		}<br />
		override public function dropMovie(e:MouseEvent):void<br />
		{<br />
			this.dragQ.dropMovie(e);</p>
<p>		}<br />
		override public function get correct():Boolean<br />
		{<br />
			if(this.dragQ.correct)<br />
			{<br />
				this.dragQ.removeEventListener(MouseEvent.MOUSE_DOWN, dragMovie);<br />
				this.dragQ.buttonMode = false;<br />
			}<br />
			return this.dragQ.correct;<br />
		}</p>
<p>	}<br />
}</p>
<p>************************+</p>
<p>Concrete Decorator 4:</p>
<p>package<br />
{<br />
	import flash.events.MouseEvent;<br />
	import flash.media.Sound;<br />
	import flash.net.URLRequest;<br />
	import flash.display.*;<br />
	import flash.geom.Point;<br />
	import flash.events.MouseEvent;</p>
<p>	public class DrgQWrongDecorator extends DrgQDecorator<br />
	{<br />
		private var dragQ:DragQuestion;<br />
		private var sound:Sound;<br />
		var figure:Shape = new Shape();</p>
<p>		public function DrgQWrongDecorator(o:DragQuestion)<br />
		{<br />
			this.dragQ = o;<br />
			addChild(dragQ);</p>
<p>			var rect = getBounds(dragQ);<br />
			var pt:Point = new Point(rect.x +rect.width &#8211; 10, rect.y  + 10)<br />
			figure.graphics.lineStyle(4,0xFFBB00);<br />
			figure.graphics.moveTo(pt.x,pt.y);<br />
			var pt2:Point = new Point(pt.x + 8, pt.y -8);<br />
			figure.graphics.lineTo(pt2.x,pt2.y);<br />
			var pt3 = new Point(pt.x , pt.y-8 );<br />
			figure.graphics.moveTo(pt.x+8, pt.y);<br />
			figure.graphics.lineTo(pt3.x, pt3.y);</p>
<p>			dragQ.addChild(figure);<br />
			this.addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);</p>
<p>		}<br />
		override public function dragMovie(e:MouseEvent):void<br />
		{<br />
			try{<br />
				dragQ.removeChild(figure);<br />
			}<br />
			catch(e:Error)<br />
			{<br />
			}<br />
			this.dragQ.dragMovie(e);<br />
		}<br />
		override public function dropMovie(e:MouseEvent):void<br />
		{<br />
			this.dragQ.dropMovie(e);</p>
<p>		}<br />
		override public function get correct():Boolean<br />
		{<br />
			return this.dragQ.correct;<br />
		}</p>
<p>	}<br />
}</p>
<p>*******************</p>
<p>Target Class:</p>
<p>package<br />
{<br />
	import flash.display.Sprite;</p>
<p>	public class Target extends Sprite<br />
	{<br />
		public var isTarget:Boolean = true;<br />
	}<br />
}</p>
<p>***************************</p>
<p>Helper class to give form to dragable words:</p>
<p>package<br />
{<br />
	import flash.text.*;<br />
	import flash.display.Shape;<br />
	import flash.display.Sprite;</p>
<p>	public class DragForm extends Sprite<br />
	{<br />
		private var word:String;<br />
		private var field:TextField;</p>
<p>		public function DragForm(word:String)<br />
		{<br />
			this.word = word;<br />
			display();<br />
		}<br />
		public function setUp()<br />
		{</p>
<p>		}</p>
<p>		private function display()<br />
		{<br />
			field = new TextField();<br />
			field.multiline = false;<br />
			field.selectable = false;</p>
<p>			field.autoSize = TextFieldAutoSize.CENTER;<br />
			field.type = TextFieldType.DYNAMIC;<br />
			field.text = word;<br />
			//field.background = true;<br />
			//field.border = true;</p>
<p>			var format:TextFormat = new TextFormat();<br />
			format.font = &#8220;Arial&#8221;;<br />
			format.size = 18;<br />
			format.color = 0xFFFFFF;</p>
<p>			format.bold = true;<br />
			field.setTextFormat(format);</p>
<p>			var shape:Shape = new Shape();<br />
			var bgColour:uint = 0xCC3300;<br />
			shape.graphics.beginFill(bgColour);<br />
			shape.graphics.drawRoundRect(- field.width/2 -2, (-field.height/2)-2 ,field.width +4  ,field.height+4 ,30);<br />
			shape.graphics.endFill();<br />
			addChild(shape);<br />
			addChild(field);</p>
<p>			field.x =  &#8211;  field.width / 2;<br />
			field.y =  &#8211;  field.height / 2;<br />
			var theMask = new Sprite();<br />
			theMask.graphics.beginFill(0&#215;000000, 0.0);<br />
			theMask.graphics.drawRect(-field.width/2,-field.height/2,field.width,field.height);<br />
			theMask.graphics.endFill();<br />
			addChild(theMask);<br />
		}</p>
<p>	}<br />
}</p>
<p>********************</p>
<p>finally the client class. It has only two drag questions with a different check strategy each. One(TightCheck) doesn&#8217;t let the dragWord on the target unless it is the right one. The other lets  it stay on and you see if it is correct with visual clues.</p>
<p>package<br />
{<br />
	import flash.text.*;<br />
	import flash.display.Shape;<br />
	import flash.display.Sprite;</p>
<p>	public class DragForm extends Sprite<br />
	{<br />
		private var word:String;<br />
		private var field:TextField;</p>
<p>		public function DragForm(word:String)<br />
		{<br />
			this.word = word;<br />
			display();<br />
		}<br />
		public function setUp()<br />
		{</p>
<p>		}</p>
<p>		private function display()<br />
		{<br />
			field = new TextField();<br />
			field.multiline = false;<br />
			field.selectable = false;</p>
<p>			field.autoSize = TextFieldAutoSize.CENTER;<br />
			field.type = TextFieldType.DYNAMIC;<br />
			field.text = word;<br />
			//field.background = true;<br />
			//field.border = true;</p>
<p>			var format:TextFormat = new TextFormat();<br />
			format.font = &#8220;Arial&#8221;;<br />
			format.size = 18;<br />
			format.color = 0xFFFFFF;</p>
<p>			format.bold = true;<br />
			field.setTextFormat(format);</p>
<p>			var shape:Shape = new Shape();<br />
			var bgColour:uint = 0xCC3300;<br />
			shape.graphics.beginFill(bgColour);<br />
			shape.graphics.drawRoundRect(- field.width/2 -2, (-field.height/2)-2 ,field.width +4  ,field.height+4 ,30);<br />
			shape.graphics.endFill();<br />
			addChild(shape);<br />
			addChild(field);</p>
<p>			field.x =  &#8211;  field.width / 2;<br />
			field.y =  &#8211;  field.height / 2;<br />
			var theMask = new Sprite();<br />
			theMask.graphics.beginFill(0&#215;000000, 0.0);<br />
			theMask.graphics.drawRect(-field.width/2,-field.height/2,field.width,field.height);<br />
			theMask.graphics.endFill();<br />
			addChild(theMask);<br />
		}</p>
<p>	}<br />
}</p>
<p>*/</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William B. Sanders</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-3825</link>
		<dc:creator>William B. Sanders</dc:creator>
		<pubDate>Wed, 13 Jan 2010 19:34:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-3825</guid>
		<description>Hi Curro,

Okay, now that makes it a lot clearer. So the problem is that the x and y properties are acting strangely. Why not send them and we&#039;ll see what&#039;s going on. Just put them in one big comment, and I&#039;ll break them down so that we can look at the code and figure out what&#039;s going on.

Take care,
Bill</description>
		<content:encoded><![CDATA[<p>Hi Curro,</p>
<p>Okay, now that makes it a lot clearer. So the problem is that the x and y properties are acting strangely. Why not send them and we&#8217;ll see what&#8217;s going on. Just put them in one big comment, and I&#8217;ll break them down so that we can look at the code and figure out what&#8217;s going on.</p>
<p>Take care,<br />
Bill</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Curro</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-3821</link>
		<dc:creator>Curro</dc:creator>
		<pubDate>Wed, 13 Jan 2010 18:28:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-3821</guid>
		<description>Hi Bill,
I do have a common ancestor class for both the concrete component and the abstract decorator. So I don&#039;t think that&#039;s the problem.
Anyway, I didn&#039;t express myself clearly: the strange behaviours happen or not depending on when I set the X and 
Y coordinates of the object(before or after the decoration) not on when I add them to the stage.

I also forgot to mention that I am using a strategy pattern to implement different types of checking.

Shall I send you or post the files? They are 15 although short!

Thank you again for your time and interest.

Curro</description>
		<content:encoded><![CDATA[<p>Hi Bill,<br />
I do have a common ancestor class for both the concrete component and the abstract decorator. So I don&#8217;t think that&#8217;s the problem.<br />
Anyway, I didn&#8217;t express myself clearly: the strange behaviours happen or not depending on when I set the X and<br />
Y coordinates of the object(before or after the decoration) not on when I add them to the stage.</p>
<p>I also forgot to mention that I am using a strategy pattern to implement different types of checking.</p>
<p>Shall I send you or post the files? They are 15 although short!</p>
<p>Thank you again for your time and interest.</p>
<p>Curro</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William B. Sanders</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-3819</link>
		<dc:creator>William B. Sanders</dc:creator>
		<pubDate>Wed, 13 Jan 2010 15:15:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-3819</guid>
		<description>Hi Curro,

That&#039;s a great question, and &lt;em&gt;no posts are history&lt;/em&gt; on this blog. (That&#039;s because, they&#039;re based on topic; not timeframe.) For me, one of the trickiest problems in using ActionScript 3.0 is using the DisplayObjectContainer effectively. Further, this is not just an issue for the Decorator but for all design patterns for both Flash and Flash Builder (Flex).

In looking at the Client class in the above example, I believe that you can find the answer. What is being added to the DisplayObjectContainer (stage) is the &quot;decorated object&quot; not just the &quot;object&quot; and the &quot;decoration&quot; as separate elements.

The Decorator design pattern is the only one I know &lt;em&gt;that has an abstract class that is a subclass&lt;/em&gt;. So what I believe you&#039;ve attempted to do and why you&#039;re running into trouble is that you&#039;ve separated the decorator from the component and because the decorator is subclassed from the component, it&#039;s causing a problem with the DisplayObjectContainer. Of course, I&#039;d have to look at the code, but that&#039;s what it sounds like from what you say.

Take a look at the documentation for the DisplayObject and DisplayObjectContainer.

Kindest regards,
Bill</description>
		<content:encoded><![CDATA[<p>Hi Curro,</p>
<p>That&#8217;s a great question, and <em>no posts are history</em> on this blog. (That&#8217;s because, they&#8217;re based on topic; not timeframe.) For me, one of the trickiest problems in using ActionScript 3.0 is using the DisplayObjectContainer effectively. Further, this is not just an issue for the Decorator but for all design patterns for both Flash and Flash Builder (Flex).</p>
<p>In looking at the Client class in the above example, I believe that you can find the answer. What is being added to the DisplayObjectContainer (stage) is the &#8220;decorated object&#8221; not just the &#8220;object&#8221; and the &#8220;decoration&#8221; as separate elements.</p>
<p>The Decorator design pattern is the only one I know <em>that has an abstract class that is a subclass</em>. So what I believe you&#8217;ve attempted to do and why you&#8217;re running into trouble is that you&#8217;ve separated the decorator from the component and because the decorator is subclassed from the component, it&#8217;s causing a problem with the DisplayObjectContainer. Of course, I&#8217;d have to look at the code, but that&#8217;s what it sounds like from what you say.</p>
<p>Take a look at the documentation for the DisplayObject and DisplayObjectContainer.</p>
<p>Kindest regards,<br />
Bill</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Curro</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-3818</link>
		<dc:creator>Curro</dc:creator>
		<pubDate>Wed, 13 Jan 2010 14:45:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-3818</guid>
		<description>Hi Bill,
I know this post is history but I am a slow student and I can only study design patterns during my holidays.

I am using a decorator pattern for a small drag and drop language exercise. I have a component class with basic dragdrop funcionality and then I decorate it with sound ,some embellisments(filters) and correct/incorrect feedback(all in diferent concrete decorator classes). 

The app works fine but I have found something weird: if I place the objects on the stage before I decorate them, everything goes fine, but if I place them after decorating them I get strange behaviours.

Could we have a post on how to place decorated items on the stage?

Thank you

Curro</description>
		<content:encoded><![CDATA[<p>Hi Bill,<br />
I know this post is history but I am a slow student and I can only study design patterns during my holidays.</p>
<p>I am using a decorator pattern for a small drag and drop language exercise. I have a component class with basic dragdrop funcionality and then I decorate it with sound ,some embellisments(filters) and correct/incorrect feedback(all in diferent concrete decorator classes). </p>
<p>The app works fine but I have found something weird: if I place the objects on the stage before I decorate them, everything goes fine, but if I place them after decorating them I get strange behaviours.</p>
<p>Could we have a post on how to place decorated items on the stage?</p>
<p>Thank you</p>
<p>Curro</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phillip Molaro</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-2399</link>
		<dc:creator>Phillip Molaro</dc:creator>
		<pubDate>Mon, 25 May 2009 19:29:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-2399</guid>
		<description>So how does this work with Beans? I am trying to set up a Decorator pattern where my Concrete Component is an Actionscript class that is a Bean (has true getters and setters). I am doing this because I have a Coldfusion component returning a custom object (cfcdirectory.myBean) that I am casting to my Concrete class of type cfcdirectory.myBean.  That part works and is helpful so my CF developer and I can reuse beans in other projects. 

Now I want to use a Decorator pattern to add new functionality that is relevant to my Flex app only. So I have an &quot;AbstractBean&quot; class that looks like this:
&lt;pre lang=&quot;actionscript&quot;&gt;
package decorators
{
	public class AbstractBean
	{
		protected var displayName:String;		
		
		public function AbstractBean()
		{
			super();
		}
		
		// Getters / Setters
		public function get DisplayName():String
		{
			return displayName;
		}
		
		public function set DisplayName(aValue:String):void
		{
			displayName = aValue;
		}
	}
}
&lt;/pre&gt;
------------------------------------------
An abstract decorator that looks like:
&lt;pre lang=&quot;actionscript&quot;&gt;
package decorators
{
	public class DecorateBean extends AbstractItem
	{
		public function DecorateBean()
		{
			super();
		}
		
		// Getters / Setters for distinction
		public function get DisplayName():String
		{
			return displayName;
		}
		
		public function set DisplayName(aValue:String):void
		{
			displayName = aValue;
		}
	}
}
&lt;/pre&gt;
------------------------------------------
This is what my Concrete Component looks like (this is the Bean that maps one to one to my CFC return object):
&lt;pre lang=&quot;actionscript&quot;&gt;
package model
{
	import decorators.AbstractItem;
	
	[RemoteClass(alias=&quot;decorator.cfcdirectory.myBean&quot;)]  //tells Flex what CF Object type this is
	
	[Bindable]
	public class Bean extends AbstractBean
	{
		private var _displayName:String;
		
		public function Item()
		{
			_displayName = &quot;&quot;;
			
			super();
		}
		
		
		// Getters / Setters
		public function get DisplayName():String
		{
			return _displayName;
		}
		
		public function set DisplayName(aValue:String):void
		{
			_displayName = aValue;
		}
	}
}
&lt;/pre&gt;
------------------------------------------
And finally the Concrete Decorator where I want to add a new Property based on the Display Value:
&lt;pre lang=&quot;actionscript&quot;&gt;
package decorators
{
	public class ConcreteDecoratorA extends DecorateBean
	{
		private	var _localBean:AbstractBean;
		private	var _aNewValue:String;
		
		public function Decorate_AddDisplayStatus(bn:AbstractBean)
		{
			this._localBean = bn;
			
			super();
		}
		
		override public function set Display(aValue:String):void
		{
			this.NewValue = aValue;
			_localBean.DisplayName = aValue + &#039; some extra text&#039;;
		}


		public function get NewValue():String
		{
			return _aNewValue;
		}
		
		public function set NewValue(aValue:String):void
		{
			_aNewValue = aValue;
		}
	
	}
}
&lt;/pre&gt;

I am getting nasty errors saying I am overridding classes that can&#039;t be overridden (the actual getter / setter functions). I can get my bean data to populate if I monkey with this but I can never seem to get the &quot;NewValue&quot; to actually populate.  Can you point me to an example where somebody does the Decorator with a Bean style concrete component please?  I would really like to get this to work.  Thanks!!</description>
		<content:encoded><![CDATA[<p>So how does this work with Beans? I am trying to set up a Decorator pattern where my Concrete Component is an Actionscript class that is a Bean (has true getters and setters). I am doing this because I have a Coldfusion component returning a custom object (cfcdirectory.myBean) that I am casting to my Concrete class of type cfcdirectory.myBean.  That part works and is helpful so my CF developer and I can reuse beans in other projects. </p>
<p>Now I want to use a Decorator pattern to add new functionality that is relevant to my Flex app only. So I have an &#8220;AbstractBean&#8221; class that looks like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p886code1'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8861"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p886code1"><pre class="actionscript" style="font-family:monospace;">package decorators
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AbstractBean
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> displayName:<span style="color: #0066CC;">String</span>;		
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> AbstractBean<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Getters / Setters</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> displayName;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> DisplayName<span style="color: #66cc66;">&#40;</span>aValue:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			displayName = aValue;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
An abstract decorator that looks like:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p886code2'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8862"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p886code2"><pre class="actionscript" style="font-family:monospace;">package decorators
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DecorateBean <span style="color: #0066CC;">extends</span> AbstractItem
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DecorateBean<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Getters / Setters for distinction</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> displayName;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> DisplayName<span style="color: #66cc66;">&#40;</span>aValue:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			displayName = aValue;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
This is what my Concrete Component looks like (this is the Bean that maps one to one to my CFC return object):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p886code3'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8863"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p886code3"><pre class="actionscript" style="font-family:monospace;">package model
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> decorators.<span style="color: #006600;">AbstractItem</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>RemoteClass<span style="color: #66cc66;">&#40;</span>alias=<span style="color: #ff0000;">&quot;decorator.cfcdirectory.myBean&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>  <span style="color: #808080; font-style: italic;">//tells Flex what CF Object type this is</span>
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Bean <span style="color: #0066CC;">extends</span> AbstractBean
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _displayName:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Item<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			_displayName = <span style="color: #ff0000;">&quot;&quot;</span>;
&nbsp;
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Getters / Setters</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> DisplayName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> _displayName;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> DisplayName<span style="color: #66cc66;">&#40;</span>aValue:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			_displayName = aValue;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
And finally the Concrete Decorator where I want to add a new Property based on the Display Value:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p886code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8864"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code" id="p886code4"><pre class="actionscript" style="font-family:monospace;">package decorators
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteDecoratorA <span style="color: #0066CC;">extends</span> DecorateBean
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span>	<span style="color: #000000; font-weight: bold;">var</span> _localBean:AbstractBean;
		<span style="color: #0066CC;">private</span>	<span style="color: #000000; font-weight: bold;">var</span> _aNewValue:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Decorate_AddDisplayStatus<span style="color: #66cc66;">&#40;</span>bn:AbstractBean<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._localBean = bn;
&nbsp;
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> Display<span style="color: #66cc66;">&#40;</span>aValue:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">NewValue</span> = aValue;
			_localBean.<span style="color: #006600;">DisplayName</span> = aValue + <span style="color: #ff0000;">' some extra text'</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> NewValue<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> _aNewValue;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> NewValue<span style="color: #66cc66;">&#40;</span>aValue:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			_aNewValue = aValue;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I am getting nasty errors saying I am overridding classes that can&#8217;t be overridden (the actual getter / setter functions). I can get my bean data to populate if I monkey with this but I can never seem to get the &#8220;NewValue&#8221; to actually populate.  Can you point me to an example where somebody does the Decorator with a Bean style concrete component please?  I would really like to get this to work.  Thanks!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William B. Sanders</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-2261</link>
		<dc:creator>William B. Sanders</dc:creator>
		<pubDate>Wed, 29 Apr 2009 01:17:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-2261</guid>
		<description>Hi Josh,

You will find that some patterns can be combined and others not so much—depending very much on the application. Keep in mind that the pattern is designed to meet development goals and maintain good OOP principles. The important criteria is whether the  combinations (of design patterns) work to enhance re-use and change. Sometimes mixing design patterns works fine as you will see in the Model View Controller (MVC), while in other cases the components conflict with one another.  My advice is to get the basics of each design pattern first, and then you can experiment with mixing designs if your task requires it.

Kindest regards,
Bill</description>
		<content:encoded><![CDATA[<p>Hi Josh,</p>
<p>You will find that some patterns can be combined and others not so much—depending very much on the application. Keep in mind that the pattern is designed to meet development goals and maintain good OOP principles. The important criteria is whether the  combinations (of design patterns) work to enhance re-use and change. Sometimes mixing design patterns works fine as you will see in the Model View Controller (MVC), while in other cases the components conflict with one another.  My advice is to get the basics of each design pattern first, and then you can experiment with mixing designs if your task requires it.</p>
<p>Kindest regards,<br />
Bill</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh</title>
		<link>http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/comment-page-1/#comment-2257</link>
		<dc:creator>Josh</dc:creator>
		<pubDate>Tue, 28 Apr 2009 22:00:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=886#comment-2257</guid>
		<description>Hello again :)

I am quite new to this amazing world of Design Patterns from a learning view, I was just wondering if you were using say,  &#039;Simple Factory&#039; if you were to add a decorate function, eg. decorate(obj:Shape, type:String) could you add this to the factory? Or do you think it should stay within the client.

Thanks for this article!
Josh</description>
		<content:encoded><![CDATA[<p>Hello again :)</p>
<p>I am quite new to this amazing world of Design Patterns from a learning view, I was just wondering if you were using say,  &#8216;Simple Factory&#8217; if you were to add a decorate function, eg. decorate(obj:Shape, type:String) could you add this to the factory? Or do you think it should stay within the client.</p>
<p>Thanks for this article!<br />
Josh</p>
]]></content:encoded>
	</item>
</channel>
</rss>
