the singleton pattern presentation

14
Singleton Design Pattern Presented by: Patel Nilay (112342) Bareja Vishal (112354) Vaghela Sachin(112356) Shyara haresh(1123)

Upload: jainikpatel12

Post on 08-Aug-2015

52 views

Category:

Education


1 download

TRANSCRIPT

Page 1: The Singleton Pattern Presentation

Singleton Design Pattern

Presented by:Patel Nilay (112342)Bareja Vishal (112354)Vaghela Sachin(112356)Shyara haresh(1123)

Page 2: The Singleton Pattern Presentation

Intent :- the singleton pattern ensures a class has only one instance , and provides

a global point of access to it. (just like a global variable, but without the downsides.)

Motivation :- There are many objects we only need one of : thread pools, caches,

dialog boxes, objects that handle preferences and registry setting, objects used for logging, and objects that act as devise drivers to devices like printer.

In fact, many of these types of object, if we were to instantiate more than one we’d run into all sorts of problem like incorrect program behavior, overuse of resources, or inconsistent results. Singleton pattern is the solution of this problem.

Page 3: The Singleton Pattern Presentation

Applicability :-Use the Singleton pattern when

There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.

When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Structure :-

Page 4: The Singleton Pattern Presentation

Participants :-• Singleton defines an instance operation that lets clients access its unique instance. May be responsible for creating its own unique instance.

Collaborations :- Clients access a singleton instance solely through singleton’s instance

operation.

Page 5: The Singleton Pattern Presentation

Consequences :-

The singleton pattern has several benefits:

Controlled access to sole instance.- it can have strict control over how and when clients access it.

Reduces name space.- it avoids polluting the name space with global variables that store sole

instances.

Permits refinement of operations and representation.- you can configure the application with an instance of the class you need at run time.

Permit a variable number of instances.- you can use the same approach to control the number of instance that the

application uses.

Page 6: The Singleton Pattern Presentation

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!

Page 7: The Singleton Pattern Presentation

public class singleton {private static singleton uniqueInstance;

//we have a static variable to hold our one instance of the class singleton.private singleton() { }

// our constructor is declared private only singleton can instantiate this class.public static singleton getInstance() {

if (uniqueInstance == null) {uniqueInstance = new singleton();

}return uniqueInstance;

} /*the getInstance() method gives us a way to instantiate the class and also to return an instance of it. (this is called lazy instantiation)*/

// othere useful methods here}

Page 8: The Singleton Pattern Presentation

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?

Our multithreading woes are almost trivially fixed by making getInstance() a synchronized method:

public class singleton{private static singleton uniqueInstance;

private singleton() { }public static synchronized singleton getInstance() {

if (uniqueInstance == null){uniqueInstance = new singleton();

}return uniqueInstance;

}// othere useful methods here

}

Page 9: The Singleton Pattern Presentation

Can we improve multithreading?

Page 10: The Singleton Pattern Presentation

Well , we have few option…1) Do nothing if the performance of getInstance() isn’t critical to our

application2) Move to an eagerly created instance rather than a lazily created

onepublic class singleton{private static singleton uniqueInstance = new singleton();

private singleton() { }public static synchronized singleton getInstance() {

return uniqueInstance;}

} Using this approach, we rely on the JVM to create the instance of the singleton

when class is loaded.the JVM guarantees that instance will be created before any thread accesses the static uniqueInstance variable.

Page 11: The Singleton Pattern Presentation

3) Use “double-checked locking” to reduce the use of synchronization in getInstance()

With double-checked locking, we first check to see if an instance is created, and if not, THEN we synchronize. This way, we only synchronize the first time through, just what we want.

public class singleton{private volatile static singleton uniqueInstance;

/* the volatile keyword ensure that multiple threads handle the uniqueInstance variable correctly when it is being initialized to the singleton instance */

private singleton() { }public static synchronized singleton getInstance() {

if ( uniqueInstance == null ){synchronized (singleton.class) {

if ( uniqueInstance == null ){ uniqueInstance = new singleton();}

}}return uniqueInstance;

}}

Page 12: The Singleton Pattern Presentation

Known Uses :- Example s:-

- Java.lang.Runtime,Java.awt.desktop- Top level GUI (window/frame)- logging

Related Patterns :-- Abstract factory pattern- Builder pattern- Prototype pattern

Page 13: The Singleton Pattern Presentation

Any Questions…?

Page 14: The Singleton Pattern Presentation

Thanks