ActionScript 3.0 Design Patterns

(Chris Devlin) #1

116 | Chapter 3: Singleton Pattern


However, had we done that, we would have added 15 classes of excess baggage. So


while we would have saved a few strokes typing, we would have burdened the appli-


cation with those unused classes.


You can see a somewhat odd example of abstraction at work in the single property


colorFontincluded in theAlertSingleton class. ThecolorFontproperty is an abstrac-


tion of any unsigned integer (uint). First, thecolorFontproperty is assigned a literal


value, and second, that instance property is assigned to theformat.colorproperty.


Obviously, we could have assigned the color value directly to theformat.colorprop-


erty as a literal, but the purpose of putting thecolorFontproperty in the Singleton is


to insure that only a single instance will be available for coloring the fonts. Thus, the


abstraction of an unsigned integer in theAlertSingleton class insured that no color


mix-up occurs.


To finish up, open a new Flash document, and typeAlertTestas the Document class


name. Save the FLA file in the same folder as theAlertandAlertTestfiles. Figure 3-6


shows what you will see on both the stage and Output window.


In a typical application, you would use dynamic input for the message and color. For


example, you might set up a user feedback system using different error or informa-


tion messages, depending on what action the user takes. However, in the example


you can see the fundamentals of setting up such a system using a Singleton and text


field.


Just One Tune at a Time


Since the advent of MP3 and MP3 players, music has migrated from the CD to MP3


players and devices like Apple’s iPod. Imagine cranking up your iPod to listen to


your favorite tunes, and instead of one at a time, they all start playing simulta-


neously. The software that runs your iPod won’t let that happen, and you shouldn’t


Figure 3-6. Alert message on the stage

Free download pdf