A Simple Decorator Pattern Example - Abstract Decorator Class
(Page 2 of 4 )
Next, the abstract decorator is a subclass class of the Component class that inherits the informationvariable, so nothing is needed as far as theinformation variable is concerned. In fact, nothing’s required for this simple example other than defining the class as an extension of theComponentclass. However, thegetInformation()method is re-implemented independently of theComponent class, using theoverridestatement—which does what it says on the tin; it overrides parent class methods. This is done to distinguish the same method being used for theDecoratorclass from the method being used for theComponent class. All the concrete decorations are subclassed from theDecoratorclass, and all concrete components are subclassed directly from theComponentclass. Further on, the concrete components will be wrapped in concrete decorators, and such distinctions become important in more complex implementations of the Decorator design pattern. Thetrace()statement is used to show you where in the process the abstract Decorator class appears. Save Example 4-2 asDecorator.as.
Example 4-2. Decorator.as
package
{
//Abstract Decorator in Decorator Design Pattern
//**************
//Abstract class
public class Decorator extends Component
{
trace("|*|Decorator|*|");
override public function getInformation():String
{
return information;
}
}
}
Once the two abstract classes,ComponentandDecorator, have been established, it’s time to work with the concrete classes. For this example, only a single concrete component is created. Cleverly namedConcreteComponent, this class represents whatever will be decorated in a Decorator design pattern. You can have multiple concrete components that all use the same set of decorations, or only a single one. Later in this chapter, you will see an application where multiple concrete classes are decorated by a single set of decorators. The nice thing about the Decorator is that you can add as many concrete components as you want. Imagine a business web site where the concrete component represents an e-business site that you’ve worked on for several months. Using a Decorator design pattern, you’ve developed several useful and tested elements that are applied using decorators. Shortly after the project is complete, 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 theComponentclass and add a constructor. It inherits all of the features of the Component class, and uses the informationvariable to place a message in the Output window to point to the decorators. Save the code in Example 4-3 asConcreteComponent.as.
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";
}
}
}
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 affix thecomponents variable to the object being wrapped. The following code shows how the component being wrapped is passed to the decorator’s constructor.
public function DecConA(components:Component)
{
this.components=components;
}
Finally, you need a getter function to get the unique information from the concrete decorator. The public method inherited from theDecoratorclass must be set up using theoverridestatement. Here, the method gets both the concrete component’s information [components.getInformation()],and adds on that of the concrete decoration [+ " Decoration Alpha:"]:
override public function getInformation():String
{
return components.getInformation() + " Decoration Alpha:";
}
Next: Concrete Decorations >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of ActionScript 3.0 Design Patterns Object Oriented Programming Techniques, written by William B. Sanders and Chandima Cumaranatunge (O'Reilly, 2007 ISBN: 0596528469). Check it out today at your favorite bookstore. Buy this book now.
|
|