O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

When to Use the Singleton Pattern

As noted at the beginning of this chapter, the Singleton design pattern is best employed when you need one and only one instance of a class in an application. A couple of examples were discussed, but the use of the Singleton is so ubiquitous in object-oriented programming that it's sort of like asking when to use a variable.

Moreover, developers often combine the Singleton design pattern with other patterns. Where the developer needs a single, global entry point and a single instance, you'll often find a Singleton class. Likewise, multiple Singleton classes can be used in conjunction when you have several different objects where only a single instance of each object should be instantiated at any one time.

In order to provide a broad but by no means exhaustive view of how the Singleton can be employed, we will step through three examples. First, the Alert example is a simple one that shows how a single message can be placed on the stage. It is meant to represent those kinds of interfaces where the user gets a message, such as in a dialog box or similar feedback mechanism. Only a single instance of the feedback should be instantiated, to avoid contradictory messages. However, the example also shows how to connect the class to display objects on the stage. As such, it is instructive for working with display programming in ActionScript 3.0.

The second example is used for playing an MP3 file. In most situations where you play media, whether it's a MP3 file ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required