Delving More Deeply into the Decorator Pattern - Multiple Concrete Components
(Page 3 of 4 )
We could look at one soul at a time, but what fun is that? More important, in a lot of applications, having a single component to decorate isn’t too useful either. So instead of having a single soul to decorate, we’ll add two. For the time being, we’ll forego any debates about original sin and start our souls with a clean slate. Both goodness and vice will be set to zero. Just give them different soulvalues so they can be differentiated.
Examples 4-18 and 4-19 provide the concrete components for DickandJaneclasses. Each class just needs a string value forsouland numeric values forgoodnessandvice—all inherited properties from theComponentclass. Save Example 4-18 asDick.as, and Example4-19 asJane.as.
Example 4-18. Dick.as
package
{
public class Dick extends Component
{
public function Dick()
{
soul = "Dick's soul\n";
goodness=0;
vice=0;
}
}
}
Example 4-19. Jane.as
package
{
public class Jane extends Component
{
public function Jane()
{
soul = "Jane's soul\n";
goodness=0;
vice=0;
}
}
}
These concrete component classes are exactly like the previous examples we’ve examined, except we have two instead of one. Furthermore, instead of using a single string variable, two additional numeric variables are added, along with their assigned values. Otherwise, they’re just the same.
In these two concrete classes, the properties are inherited directly from the abstractComponentclass. No overrides or other special adjustments are needed. This is because theDickandJaneclasses are components, both to be decorated by the decorator classes. The overrides that generate unique methods and characteristics for the concrete decorators are accomplished by the decorator classes. Therefore, overrides by the concrete component classes are unnecessary.
Next: Decorating with Multiple Properties >>
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.
|
|