singleton pattern presented by:- navaneet kumar ise2007019

20
Singleton Pattern Presented By:- Navaneet Kumar ise2007019

Upload: kristopher-hunt

Post on 18-Jan-2018

219 views

Category:

Documents


0 download

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

Page 1: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

Singleton Pattern

Presented By:- Navaneet Kumar

ise2007019

Page 2: 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.

Page 3: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

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.

Page 4: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

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.

Page 5: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

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.

Page 6: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

Structure

Class Diagram:

Page 7: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

Participants

Singleton Defines an instance operation that lets clients

access its unique instance . May be responsible for creating its own unique

instance.

Page 8: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

Collaborations

Client access a Singleton instance solely through Singleton’s instance operation.

Sequence diagram:

Page 9: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

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.

Page 10: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

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;

Page 11: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

/** * 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!}

Page 12: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

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());

Page 13: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

// 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

Page 14: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

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

Page 15: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

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!

}

Page 16: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

Known Uses

One file system one window manager one printer spooler one Test engine one Input/Output socket and etc

Page 17: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

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.

Page 18: Singleton Pattern Presented By:- Navaneet Kumar ise2007019

References:

http://www.javacamp.org/designPattern/

http://www.javabeginner.com/singleton.htm Head First design Patterns

>Freeman & Freeman

Page 19: Singleton Pattern Presented By:- Navaneet Kumar ise2007019
Page 20: Singleton Pattern Presented By:- Navaneet Kumar ise2007019