singleton pattern presented by:- navaneet kumar ise2007019
DESCRIPTION
Intent It’s important for some classes to have exactly one instance. How we ensure that a class has only one instance and the instance is easily accessible ??? A global variable makes an object accessible but it doesn’t keep you from instantiating multiple objects. Motivation Ensure a class only has one instance, and provide a global point of access to it.TRANSCRIPT
Singleton Pattern
Presented By:- Navaneet Kumar
ise2007019
Singleton Pattern
singleton: an object that is the only object of its type ensures that a class has at most one instance provides a global access point to that instance
It is a creational pattern.
Intent
It’s important for some classes to have exactly one instance.
How we ensure that a class has only one instance and the instance is easily accessible ???
A global variable makes an object accessible but it doesn’t keep you from instantiating multiple objects.
Motivation
Ensure a class only has one instance, and provide a global point of access to it.
A better solution is to make the class itself responsible for keeping track of its sole instance .
The class can ensure that no other instance can be created ,and it can provide a way to access the instance.
Applicability
Use singleton pattern when: There must be exactly one instance of a class, and
it must be accessible to client from well-known access point.
Structure
Class Diagram:
Participants
Singleton Defines an instance operation that lets clients
access its unique instance . May be responsible for creating its own unique
instance.
Collaborations
Client access a Singleton instance solely through Singleton’s instance operation.
Sequence diagram:
Consequences
Controlled access to sole instance: Because the Singleton class encapsulates its
sole instance, it can have strict control over how and when client access it.
Define one value shared by all instances by making it static.
Implementation
OK, so how do we implement the Singleton pattern? We'll use a static method to allow clients to get a
reference to the single instance and we’ll use a private constructor!
/*** Class Singleton is an implementation of a class that* only allows one instantiation.*/public class Singleton {
// The private reference to the one and only instance.private static Singleton uniqueInstance = null;
// An instance attribute.private int data = 0;
/** * Returns a reference to the single instance. * Creates the instance if it does not yet exist. * (This is called lazy instantiation.) */public static Singleton instance() {
if(uniqueInstance == null) uniqueInstance = new Singleton();return uniqueInstance;
}/** * The Singleton Constructor. * Note that it is private! * No client can instantiate a Singleton object! */private Singleton() { }
// Accessors and mutators here!}
Here's a test program:
public class TestSingleton {public static void main(String args[]) {
// Get a reference to the single instance of Singleton.Singleton s = Singleton.instance();
// Set the data value.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());
}}
output:First reference: Singleton@3e25a5Singleton data value is: 34Second reference: Singleton@3e25a5Singleton data value is: 34
Note that the singleton instance is only created when needed.
This is called lazy instantiation. Thought experiment: What if two threads concurrently
invoke the instance() method? Any problems? Yup! Two instances of the singleton class could be
created! How could we prevent this? Several methods:
Make the instance() synchronized. Synchronization is expensive, however and is really only needed the first time the unique instance is created.
Do an eager instantiation of the instance rather than a lazy instantiation
Here is Singleton with eager instantiation. We'll create the singleton instance in a static initializer.
This is guaranteed to be thread safe.public class Singleton {
private static Singleton uniqueInstance = new Singleton();private int data = 0; public static Singleton instance() {
return uniqueInstance;} private Singleton() {}// Accessors and mutators here!
}
Known Uses
One file system one window manager one printer spooler one Test engine one Input/Output socket and etc
Related patterns
Abstract factory, which is often used to return unique objects.
Builder, which is used to construct a complex object, whereas a singleton is used to create a globally accessible object.
Prototype, which is used to copy an object, or create an object from its prototype, whereas a singleton is used to ensure that only one prototype is guaranteed.
References:
http://www.javacamp.org/designPattern/
http://www.javabeginner.com/singleton.htm Head First design Patterns
>Freeman & Freeman