SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
Singleton Design Pattern
Presented by:
Patel Nilay (112342)
Bareja Vishal (112354)
Vaghela Sachin(112356)
Shyara haresh(1123)
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.
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 :-
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.
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.
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!
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
}
 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
}
Can we improve multithreading?
 Well , we have few option…
1) Do nothing if the performance of getInstance() isn’t critical to
our application
2) Move to an eagerly created instance rather than a lazily created
one
public 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.
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;
}
}
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
Any Questions…?
Thanks

More Related Content

PPTX
Design Pattern - Singleton Pattern
PPTX
Singleton Design Pattern - Creation Pattern
PPT
Builder pattern
PPTX
Design pattern-presentation
PPTX
Creational pattern
PPTX
Gof design patterns
PDF
Java Design Patterns Tutorial | Edureka
PPT
Software Design Patterns
Design Pattern - Singleton Pattern
Singleton Design Pattern - Creation Pattern
Builder pattern
Design pattern-presentation
Creational pattern
Gof design patterns
Java Design Patterns Tutorial | Edureka
Software Design Patterns

What's hot (20)

PPT
Singleton design pattern
PPTX
Design Pattern - Factory Method Pattern
PPTX
Factory Method Pattern
PPT
Design patterns ppt
PPTX
JAVA AWT
PPT
Adapter pattern
PPTX
PDF
Introduction to Design Pattern
PPTX
Java Spring Framework
PPT
Flyweight pattern
ODP
Multithreading In Java
PPTX
Proxy Design Pattern
PPTX
Android share preferences
PPT
Introduction to Design Patterns and Singleton
PDF
PDF
Software Engineering - chp4- design patterns
PPT
Facade pattern
PPSX
JDBC: java DataBase connectivity
PPT
Prototype pattern
Singleton design pattern
Design Pattern - Factory Method Pattern
Factory Method Pattern
Design patterns ppt
JAVA AWT
Adapter pattern
Introduction to Design Pattern
Java Spring Framework
Flyweight pattern
Multithreading In Java
Proxy Design Pattern
Android share preferences
Introduction to Design Patterns and Singleton
Software Engineering - chp4- design patterns
Facade pattern
JDBC: java DataBase connectivity
Prototype pattern
Ad

Viewers also liked (6)

PPTX
Design pattern (Abstract Factory & Singleton)
PDF
Java - Singleton Pattern
PDF
Java8 - Interfaces, evolved
PDF
Go language presentation
PPTX
Singleton Pattern (Sole Object with Global Access)
PPTX
Desing pattern prototype-Factory Method, Prototype and Builder
Design pattern (Abstract Factory & Singleton)
Java - Singleton Pattern
Java8 - Interfaces, evolved
Go language presentation
Singleton Pattern (Sole Object with Global Access)
Desing pattern prototype-Factory Method, Prototype and Builder
Ad

Similar to The Singleton Pattern Presentation (20)

PPT
Singleton
PPT
Simple Singleton Java
PDF
Singleton Sum
PDF
Mastering Design Patterns in Java: A Comprehensive Guide
PDF
Robust and Scalable Concurrent Programming: Lesson from the Trenches
PDF
Design patterns in Java - Monitis 2017
PPTX
Singleton class in Java
PDF
From Runnable and synchronized To atomically() and parallel()
PPTX
Design Patterns
PPTX
OOPSDesign PPT ( introduction to opps and design (
PPT
Introduction to design_patterns
PPT
Design_Patterns_Dr.CM.ppt
PPTX
Design Patterns every Android developer should know
PPS
Singletons
PDF
Java Concurrency Gotchas
PPTX
design_pattern.pptx design_pattern design_pattern
PPTX
Java concurrency
PPT
10-design-patterns1.ppt.software engineering
PPT
Software Design Patterns
PDF
Javaoneconcurrencygotchas 090610192215 Phpapp02
Singleton
Simple Singleton Java
Singleton Sum
Mastering Design Patterns in Java: A Comprehensive Guide
Robust and Scalable Concurrent Programming: Lesson from the Trenches
Design patterns in Java - Monitis 2017
Singleton class in Java
From Runnable and synchronized To atomically() and parallel()
Design Patterns
OOPSDesign PPT ( introduction to opps and design (
Introduction to design_patterns
Design_Patterns_Dr.CM.ppt
Design Patterns every Android developer should know
Singletons
Java Concurrency Gotchas
design_pattern.pptx design_pattern design_pattern
Java concurrency
10-design-patterns1.ppt.software engineering
Software Design Patterns
Javaoneconcurrencygotchas 090610192215 Phpapp02

More from JAINIK PATEL (6)

PPTX
PPT
Architecture of Mobile Computing
PPT
security issue
PPT
Mobile Computing
PPTX
112321 112333 wirless application protocol
PPTX
Facadepattern
Architecture of Mobile Computing
security issue
Mobile Computing
112321 112333 wirless application protocol
Facadepattern

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Classroom Observation Tools for Teachers
PDF
Insiders guide to clinical Medicine.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Lesson notes of climatology university.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Computing-Curriculum for Schools in Ghana
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
Sports Quiz easy sports quiz sports quiz
Complications of Minimal Access Surgery at WLH
Microbial diseases, their pathogenesis and prophylaxis
Classroom Observation Tools for Teachers
Insiders guide to clinical Medicine.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Lesson notes of climatology university.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
TR - Agricultural Crops Production NC III.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Computing-Curriculum for Schools in Ghana
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Types and Its function , kingdom of life

The Singleton Pattern Presentation

  • 1. Singleton Design Pattern Presented by: Patel Nilay (112342) Bareja Vishal (112354) Vaghela Sachin(112356) Shyara haresh(1123)
  • 2. 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.
  • 3. 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 :-
  • 4. 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.
  • 5. 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.
  • 6. 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!
  • 7. 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 }
  • 8.  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 }
  • 9. Can we improve multithreading?
  • 10.  Well , we have few option… 1) Do nothing if the performance of getInstance() isn’t critical to our application 2) Move to an eagerly created instance rather than a lazily created one public 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.
  • 11. 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; } }
  • 12. 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