
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
The Singleton Pattern in the Logger Class
|
383
it causes this error:
The member is private and cannot be accessed.
Thus, any class that wishes to create a Singleton instance is forced to use the
Singleton.getInstance( ) method, which guarantees that only one Singleton instance is
ever created.
Notice that because the Singleton instance is stored in a property of the same class, it
is automatically accessible to any other class that can access Singleton. (In theory, we
could have made our instance accessible to the application by defining it as a global
variable, but that global variable’s name might conflict with another global variable.)
Here’s how we’d use our Singleton class, somewhere in our application:
var s:Singleton = Singleton.getInstance( );
s.doSomething( );
In some cases, it’s not necessary to store the Singleton instance in a variable or prop-
erty. When we have only temporary need of the Singleton instance, we could reduce
the previous code to:
Singleton.getInstance().doSomething( );
Now that we’ve seen how the Singleton pattern is implemented in general, let’s look
at a specific Singleton implementation example—the Logger class from Chapter 16.
The Singleton Pattern in the Logger Class
In Chapter 16, we created an application log using the Observer pattern. That appli-
cation ...