10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In order to provide a broad but by no means exhaustive view of how the Singleton<br />

can be employed, we will step through three examples. First, the Alert example is a<br />

simple one that shows how a single message can be placed on the stage. It is meant<br />

to represent those kinds of interfaces where the user gets a message, such as in a dialog<br />

boxor similar feedback mechanism. Only a single instance of the feedback<br />

should be instantiated, to avoid contradictory messages. However, the example also<br />

shows how to connect the class to display objects on the stage. As such, it is instructive<br />

for working with display programming in <strong>ActionScript</strong> <strong>3.0</strong>.<br />

The second example is used for playing an MP3 file. In most situations where you<br />

play media, whether it’s a MP3 file or a video, you want to hear or see only one<br />

media element at a time. Playing Bach and a song by Gnarls Barkley simultaneously<br />

may create a racket that neither Johann Sebastian nor Gnarls would want to hear. A<br />

Singleton class helps to keep the play sequential, with no more than one playing at<br />

the same time.<br />

The third example uses a simple shopping cart to illustrate how a single instance can<br />

be used to keep track of a running total. In situations where your application needs<br />

an absolutely no-questions-about-it single instance for keeping track of financial<br />

accumulations, a Singleton can be crucial. The example also shows how to link a<br />

class to a stage-created movie clip and embedded text field.<br />

A Single Alert Message<br />

Because clear <strong>com</strong>munication is the cruxof good site design, mixed messages need<br />

to be kept out of all web applications. By using a Singleton pattern, you can help<br />

assure that you have a single source for messages to the user. This way, you’re less<br />

likely to send two contradictory messages. The following application uses a Singleton<br />

for that purpose. It’s designed so that no matter where a message originates, it<br />

has only this one instance to deliver it.<br />

To get started, open a new <strong>ActionScript</strong> file, and type in the script shown in<br />

Example 3-10.<br />

Example 3-10. Alert.as<br />

package<br />

{<br />

public class Alert<br />

{<br />

private var _msg:String;<br />

private static var _instance:Alert;<br />

public function Alert(pvt:PrivateClass) {<br />

}<br />

public static function getInstance( ):Alert<br />

{<br />

if(Alert._instance == null)<br />

When to Use the Singleton Pattern | 113

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!