ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Minimalist Abstract Decorator | 139

Wrapping Up


To execute the Decorator design pattern, the whole key lies in knowing how to wrap


a component in a concrete decorator. First, you need to instantiate a concrete


component.


var testComponent:Component = new ConcreteComponent( );

Then, you wrap the component in one or more decorations using the following


format:


componentInstance=new ConcreteDecorator(testComponent);

So, in our example, with two concrete decorations, we’d write:


testComponent=new DecConA(testComponent);
testComponent=new DecConB(testComponent);

At this point,testComponentis decorated with two decorations. We could duplicate


the above lines adding the same two decorations as often as we wanted. Think of the


decorations as red and green Christmas tree ornaments. The tree could be covered


with nothing but red and green ornaments, rather than just one of each. The Decora-


tor design pattern is employed cumulatively. That is, as you add each decoration, it’s


added to those already wrapping the concrete component.


Finally, to see what havoc we’ve wrought, we use the getter method—


getInformation( ):


trace(testComponent.getInformation( ));

To see how all of this works, save Example 4-6 asDecTest.asin an ActionScript file.


Then open a new Flash document file, and typeDecTestin the Document class win-


dow in the Properties panel.


Example 4-5. DecConB.as


package
{
//Concrete Decorator "Beta"
public class DecConB extends Decorator
{
var components:Component;
public function DecConB(components:Component) {
this.components=components;
}
override public function getInformation( ):String
{
return components.getInformation( ) + " Decoration Beta:";
}
}
}

Free download pdf