Advantages
(+)
-
Can control object creation.
-
The getInstance operation can control
the creation process,
for example, to allow more than
one instance of a class.
Disadvantages
(–)
-
Makes clients dependent on the concrete
singleton class.
-
This stops client classes from being reusable
and testable.
-
"Making a class a singleton can
make it difficult to test its clients,
as it's impossible to substitute a mock
implementation for a singleton
unless it implements an interface
that serves as its type."
[JB08 , p17]
-
See also Abstract Factory / Sample Code / Example 3 /
Creating families of objects.
-
Can cause problems in a
multi-threaded environment.
-
In multi-threaded applications,
a singleton that holds mutable data
(i.e., data that can be changed
after the object is created)
must be implemented carefully (synchronized).