ActionScript 3.0 Design Patterns

(Chris Devlin) #1

170 | Chapter 4: Decorator Pattern


For this particular application, we’re not concerned with separating the individual


costs. In fact, we want each cost to be accumulated with the others, including the


cost of the concrete component we’re decorating. So while the string value needs the


tilde (~) for demarcation purposes, we don’t need it for the numeric value.


Setting Up the User Interface


The largest single class we’re going to use is the one to create the user interface. The


line numbers appear in the Example 4-48 for the purpose of referencing lines in the


code. A lot of the work done by this class, namedDeal, is to set up the interface


objects. These include the radio button, checkbox, and button components. Addi-


tionally, a good hunk of code is required for the text field output window and for-


matting for the output. So while the interface to the decorator pattern may look


unwieldy, it’s not decorator pattern’s fault. In fact, only thegetCar( )(beginning on


line 115) andgetOptions( )(beginning on line146) private functions are employed to


pull out the information generated by the Decorator pattern.


To get a handle on what the class does, enter the code from Example 4-48 and save it


asDeal.as.


}

override public function getInformation( ):String
{
return auto.getInformation( ) + " MP3 Player~";
}
override public function price( ):Number
{
return 267.55 + auto.price( );
}
}
}


Example 4-48. Deal.as


1 package
2 {
3 import fl.controls.CheckBox;
4 import fl.controls.RadioButton;
5 import fl.controls.Button;
6 import flash.display.Sprite;
7 import flash.display.MovieClip;
8 import flash.events.MouseEvent;
9 import flash.text.TextField;
10 import flash.text.TextFormat;
11
12 public class Deal extends Sprite
13 {
14 internal var checks:Array=[];
15 internal var cars:Array=[];

Example 4-47. MP3.as (continued)

Free download pdf