The Decorator Pattern in Action
(Page 1 of 4 )
In this third part of a six-part series on the Decorator pattern, we move from last week's conceptual example to something more concrete: using the pattern to decorate a paper doll. 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). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Applying a Simple Decorator Pattern in Flash: Paper Doll
You can move from a conceptual example to a concrete one with a computerized paper doll game. The paper doll component is the doll being dressed, and the decorations are clothing items selected for the doll. Using ActionScript’s new Loader class, different GIF files are used to decorate a base GIF image of a Victorian paper doll. As in the previous abstract example, this example uses a single concrete component and several decorations.
This concrete example does not have an interface—keeping the focus on the application’s use of a Decorator pattern. However, it does have a graphic output so you can see the pattern’s ultimate output. (Later in this chapter, there’s an example with a full interface.)
Setting Up the Component Class
The first class is the component class, Model. From this, all other classes are subclasses. It’s very simple but key to the success of the application. As you can see, it’s very close to the previous abstract Decorator. It consists of a method, getDressed(), and a string variable,whatToWear,as shown in Example 4-7. Save the file asModel.as.
Example 4-7. Model.as
package
{
//Abstract class
public class Model
{
protected var whatToWear:String;
public function getDressed():String
{
return whatToWear;
}
}
}
Keep in mind that Example 4-7 should be treated as an abstract class. So the methodgetDressed()needs to be created as an override public function for it to work the way we want. However, the propertywhatToWear doesn’t need to be changed from its inherited characteristics.
Next: Decorator Class: Dressing the Dolls >>
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.
|
|