ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Minimalist Abstract Decorator | 137

eral months. Using a Decorator design pattern, you’ve developed several useful and


tested elements that are applied using decorators. Shortly after the project is com-


plete, you get a request to develop another site with a different main product and an


overlapping set of elements. Rather than starting from scratch, all you have to do is


to add a different concrete component class and some new decorators that aren’t


available in the original set.


Example 4-3 shows that the concrete component does little more than extend the


Componentclass and add a constructor. It inherits all of the features of the Component


class, and uses theinformationvariable to place a message in the Output window to


point to the decorators. Save the code in Example 4-3 asConcreteComponent.as.


In Example 4-3, you see that Unicode is inserted using the format \u
+ character code value. In the example, an arrow character is
formed using Unicode 2794. To search for a character you may want
to use, see http://www.fileformat.info/info/unicode/char/search.htm.
You’ll find several different arrow characters you can use, including
\u0363, \u2192, \u21aa, and \u21d2.

The next step is to build concrete decorator classes. In this abstract example, two


concrete decorators will wrap themselves around the concrete component. So, to get


started, we’ll need a Component instance variable to hold the component we’re


wrapping.


var components:Component;

The variable is namedcomponents, with an “s,” becausecomponentis a built-in word


in ActionScript 3.0. This variable is referenced in the decorator’s methods. Next, we


need a way to affi xthecomponents variable to the object being wrapped. The


following code shows how the component being wrapped is passed to the decora-


tor’s constructor.


Example 4-3. ConcreteComponent.as


package
{
//Concrete Component
public class ConcreteComponent extends Component
{
public function ConcreteComponent( )
{
//\u2794 is Unicode for a right-pointing arrow
information = "Concrete Component is decorated with \u2794";
}
}
}

Free download pdf