Java Reference
In-Depth Information
Singleton
Pattern Properties
Type: Creational
Level: Object
Purpose
To have only one instance of this class in the system, while allowing other classes to get access to this instance.
Introduction
Once in a while, you need a global object: one that's accessible from anywhere but which should be created only
once. You want all parts of the application to be able to use the object, but they all should use the same instance.
An example is a history list—a list of actions a user has taken while using the application. Multiple parts of the
application use the same HistoryList object to either add actions a user has taken or to undo previous actions.
One way to achieve this is to have the main application create a global object, then pass its reference to every
object that might ever need it. However, it can be very difficult to determine how you want to pass the reference,
and to know up front which parts of the application need to use the object. Another drawback to this solution is
that it doesn't prevent another object from creating another instance of the global object—in this case,
HistoryList .
Another way to create global values is by using static variables. The application has several static objects inside
of a class and accesses them directly.
This approach has several drawbacks.
A static object will not suffice because a static object will be created at the time the class loads and thus gives you
no opportunity to supply any data before it instantiates.
You have no control over who accesses the object. Anybody can access a publicly available static instance.
If you realize that the singleton should be, say, a trinity, you're faced with modifying every piece of client code.
This is where the Singleton pattern comes in handy. It provides easy access for the whole application to the global
object.
Applicability
Use the Singleton when you want only one instance of a class, but it should be available everywhere.
Description
The Singleton ensures a maximum of one instance is created by the JVM (not surprisingly, that's why it's called a
singleton). To ensure you have control over the instantiation, make the constructor private.
This poses a problem: it's impossible to create an instance, so an accessor method is provided by a static method
( getInstance ()). That method creates the single instance, if it doesn't already exist, and returns the reference of
the singleton to the caller of the method. The reference to the singleton is also stored as a static private attribute of
the singleton class for future calls.
Although the accessor method can create the singleton, most of the times it is created as the class is loaded.
Postponing the construction is only necessary if some form of initialization has to be done before the singleton is
instantiated.
An example of a singleton is the president of the United States of America. At any given time there should only
be one president. When the president of Russia picks up the red phone, he expects to get a handle to the current
United States president.
 
Search WWH ::




Custom Search