Professional Documents
Culture Documents
Singleton
Singleton
The Singleton pattern can be used for this purpose. This pattern is
effective for limiting the maximum number of instances of a class to
exactly one. In this case, if more than one object needs to use an
instance of the Singleton class, those objects share the same Singleton
class instance. A class that implements the Singleton pattern is called
a Singleton class.
Simply Singleton
By: By David Geary
Navigate the deceptively simple Singleton pattern
ometimes it's appropriate to have exactly one instance of a class:
window managers, print spoolers, and filesystems are prototypical
examples. Typically, those types of objects?known as singletons?are
accessed by disparate objects throughout a software system, and
therefore require a global point of access. Of course, just when you're
certain you will never need more than one instance, it's a good bet
you'll change your mind.
The Singleton design pattern addresses all of the previous paragraph's
concerns. With the Singleton design pattern you can:
Ensure that only one instance of a class is created
Provide a global point of access to the object
Allow multiple instances in the future without affecting a singleton
class's clients
Although the Singleton design pattern?as evidenced below by the
figure below?is one of the simplest design patterns, it presents a
number of pitfalls for the unwary Java developer. This article discusses
the Singleton design pattern and addresses those pitfalls.
Note: You can download this article's source code from Resources .
The Singleton pattern
In Design Patterns , the authors describe the Singleton pattern like
this:
Ensure a class has only one instance, and provide a global point of
access to it.
The figure below illustrates the Singleton design pattern class diagram.