You are on page 1of 14

SINGLETON

DESIGN
PATTERN
Index
 Intent
 Structure
 Example program
 Explanation
 Participants
 Consequences
 Limitations
 Practical examples
 Intent
 Ensure a class only has one instance, and

provide a global point of access to it.

 Motivation
 It’s important for some classes to have

exactly one instance.


 Although there can be many printers in a

system, there should be only one printer


spooler.
 There should be only one file system (or file

system manager) and one window manager.


Structure
 Example program:
public class TestSingleton {
public static void main(String args[]) {
// Get a reference to the single instance of Singleton.
Singleton s = Singleton.instance();
s.setData(34);
System.out.println("First reference: " + s);
System.out.println("Singleton data value is: " +
s.getData());
// Get another reference to the Singleton.
// Is it the same object?
s = null;
s = Singleton.instance();
System.out.println("\nSecond reference: " + s);
System.out.println("Singleton data value is: " +
s.getData());
}}
The test program output is as shown:
First reference:
Singleton data value is: 34
Second reference:
Singleton data value is: 34
 Note that the singleton instance is only created when
needed. This is called lazy instantiation.
What if two threads concurrently invoke the instance()
method? Any problems?
 If invoked concurrently, two instances of the singleton
class could be created. However, this could be
prevented using the following 2 techniques:
i) Make the instance() synchronized. Synchronization
is expensive, however, and it may have a negative
impact on the performance. Moreover, it is really
only needed the first time the unique instance is
created.
ii) Do an eager instantiation of the instance rather
than a lazy instantiation. An eager class loader
loads all the classes comprising the application
code at startup. Such an implementation requires
a static initializer and is thread safe.
Singleton Participants

 Singleton - Defines an instance operation that


lets clients access its unique instance. Instance
is a class operation (static method). May be
responsible for creating its own unique
instance
 Client - Accesses a singleton instance solely
through the singleton’s instance() method.
Singleton Consequences

 Controlled access to sole instance facilitates


strict control over when and how the clients
access it.
 The singleton pattern is an improvement over
global variables.
 It may be subclassed and it is easy to configure
an instance of an application that extends the
functionality of singleton at run-time.
 More flexible than class operations.
Singleton Limitations
 The main limitation of the singleton pattern is
that it permits the creation of only one instance
of the class, while most practical applications
require multiple instances to be initialized.
 Furthermore, in case of singleton, the system
threads fight to access the single instance
thereby degrading the performance of the
application.
Examples

 Cache – The cache can be used as a singleton


object, having a global point of reference for
all future references to the cache object and the
client application uses this in-memory object.
 Configuration File – Any application has a
single point of reference through its
configuration files. Any changes to be made to
the application will have to be made via this
single configuration file.
Thank You

You might also like