Delving More Deeply into the Decorator Pattern - Adding Properties and Methods (Page 2 of 4 )
Up to now, we’ve used a single string variable with a single getter method for the basic abstract component class. However, like any other class, this basic structure can accommodate more than a single variable or function. Example 4-17 shows three variables and getter functions. Save the script asComponent.as.
Example 4-17. Component.as
package { //Abstract class public class Component { protected var soul:String="All that is inside a spirit"; protected var goodness:Number; protected var vice:Number;
public function getSoul():String { return soul; } public function good():Number { return goodness; } public function evil():Number { return vice; } } }
Like the previous examples, we begin with a string property,soul. (It’s assigned a string literal, but that’s really not necessary because it’s an abstract class and will never be seen or used—just a clarification.) Next, two numeric properties are defined,goodnessandvice. These two properties will collect all the accumulated good and evil in a soul.
Next, three getter functions are supplied to get the values of the string and two numeric variables. Now the abstract component class is all set to go.