Decorating with Deadly Sins and Heavenly Virtues | 149
Adding Properties and Methods
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.
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.
Multiple Concrete Components
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
goodnessandvicewill be set to zero. Just give them differentsoulvalues so they can
be differentiated.
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;
}
}
}