Download presentation
Presentation is loading. Please wait.
Published byKristopher Hunt Modified over 9 years ago
1
Singleton Pattern Presented By:- Navaneet Kumar ise2007019
2
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.
3
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.
4
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.
5
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.
6
Structure Class Diagram:
7
Participants Singleton Defines an instance operation that lets clients access its unique instance. May be responsible for creating its own unique instance.
8
Collaborations Client access a Singleton instance solely through Singleton’s instance operation. Sequence diagram:
9
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.
10
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;
11
/** * 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! }
12
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());
13
// 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@3e25a5 Singleton data value is: 34 Second reference: Singleton@3e25a5 Singleton data value is: 34
14
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
15
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! }
16
Known Uses One file system one window manager one printer spooler one Test engine one Input/Output socket and etc
17
Related patterns Abstract factory, which is often used to return unique objects. Abstract factory Builder, which is used to construct a complex object, whereas a singleton is used to create a globally accessible object. Builder 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. Prototype
18
References: http://www.javacamp.org/designPattern/ http://www.javabeginner.com/singleton.htm Head First design Patterns >Freeman & Freeman
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.