What is a singleton?
A singleton is a design pattern used when only one instance of an object
is created and shared; that is, it only allows one instance of itself to be
created. Any attempt to create another instance simply returns a reference to
the first one. Singleton classes are created by defining all class constructors
as private. In addition, a private static member is created as the same type of
the class, along with a public static member that returns an instance of the
class.
The advantages of a Singleton Pattern
are:
1.
Singleton pattern can be implemented interfaces.
2.
It can be also inherit from other classes.
3.
It can be lazy loaded.
4.
It has Static Initialization.
5.
It can be extended into a factory pattern.
6.
It helps to hide dependencies.
7.
It provides a single point of access to a particular instance, so it is
easy to maintain.
Disadvantages
The disadvantages of a Singleton
Pattern are:
1.
Unit testing is more difficult (because it introduces a global state
into an application).
2.
This pattern reduces the potential for parallelism within a program,
because to access the singleton in a multi-threaded system, an object must be
serialized (by locking).
For more
What is a singleton?
Reviewed by NEERAJ SRIVASTAVA
on
6:10:00 PM
Rating:
No comments: