
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
381
Chapter 17
CHAPTER 17
The Singleton Design Pattern
[The Singleton pattern’s purpose is to] ensure a class has only one instance, and pro-
vide a global point of access to it.
——from Design Patterns: Elements of Reusable Object-
Oriented Software (the so-called GoF book discussed in
Chapter 15)
Sometimes an application needs only a single instance of a particular class and
should not create more than that one instance. For example, an order form might
need one FormProcessor. A game might need one LevelManager. A text editor might
need one GUIBuilder. Or a chat might need one SocketManager. In each of those
applications, creating more than one instance of the FormProcessor, LevelManager,
GUIBuilder,orSocketManager classes could cause problems. For example, having
multiple SocketManagers might lead to multiple open socket connections, which
would waste resources and potentially disrupt communications. To prevent an appli-
cation from creating more than one instance of a class and to give various parts of
the application access to that one instance we use the Singleton pattern.
Implementing Singleton in ActionScript 2.0
The Singleton pattern has a relatively simple implementation, particularly consider-
ing its general usefulness. Most of the pattern implementation resides in a class we’ll ...