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

PPT
Builder pattern
PPTX
Design Pattern - Singleton Pattern
PDF
Polymorphism in oop
PPSX
Robotic Process Automation
PPT
Number system
PPTX
ReactJS presentation.pptx
PPTX
Design Pattern - Factory Method Pattern
PPTX
Singleton Design Pattern - Creation Pattern
Builder pattern
Design Pattern - Singleton Pattern
Polymorphism in oop
Robotic Process Automation
Number system
ReactJS presentation.pptx
Design Pattern - Factory Method Pattern
Singleton Design Pattern - Creation Pattern

What's hot (20)

PPT
Singleton design pattern
PDF
Java Thread Synchronization
PPTX
Factory Method Pattern
PPT
Design patterns ppt
PPTX
Singleton Pattern (Sole Object with Global Access)
PPT
SOLID Design Principles
PDF
Design Patterns Presentation - Chetan Gole
PPTX
PDF
PPT
Introduction to Design Patterns and Singleton
PPT
Mvc architecture
PDF
Java Design Patterns Tutorial | Edureka
PPTX
Exception handling
PPT
Design Pattern For C# Part 1
PPTX
Gof design patterns
PPTX
Java package
PPT
Java static keyword
PPTX
MULTI THREADING IN JAVA
PPTX
Introduction to Spring Framework
Singleton design pattern
Java Thread Synchronization
Factory Method Pattern
Design patterns ppt
Singleton Pattern (Sole Object with Global Access)
SOLID Design Principles
Design Patterns Presentation - Chetan Gole
Introduction to Design Patterns and Singleton
Mvc architecture
Java Design Patterns Tutorial | Edureka
Exception handling
Design Pattern For C# Part 1
Gof design patterns
Java package
Java static keyword
MULTI THREADING IN JAVA
Introduction to Spring Framework
Ad

Similar to The Singleton Pattern Presentation (20)

PPTX
Creating and destroying objects
PDF
Design patterns in Java - Monitis 2017
PPT
10-design-patterns1.ppt.software engineering
PPT
Design_Patterns_Dr.CM.ppt
PPTX
Design pattern-presentation
PPTX
Creational - The Singleton Design Pattern
PDF
Design patterns in java script, jquery, angularjs
PPT
P Training Presentation
PPT
Simple Singleton Java
PDF
C# Advanced L07-Design Patterns
PPS
Jump start to OOP, OOAD, and Design Pattern
PPT
Jump Start To Ooad And Design Patterns
PDF
Best Practices
PDF
Design patterns
PDF
Java design patterns
PDF
Java unit 7
PPTX
PATTERNS02 - Creational Design Patterns
PPTX
Software Architecture and Design Patterns Notes.pptx
PPTX
Java basics
PPTX
Creating and destroying objects
Design patterns in Java - Monitis 2017
10-design-patterns1.ppt.software engineering
Design_Patterns_Dr.CM.ppt
Design pattern-presentation
Creational - The Singleton Design Pattern
Design patterns in java script, jquery, angularjs
P Training Presentation
Simple Singleton Java
C# Advanced L07-Design Patterns
Jump start to OOP, OOAD, and Design Pattern
Jump Start To Ooad And Design Patterns
Best Practices
Design patterns
Java design patterns
Java unit 7
PATTERNS02 - Creational Design Patterns
Software Architecture and Design Patterns Notes.pptx
Java basics
Ad

More from JAINIK PATEL (7)

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

Recently uploaded (20)

PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Lesson notes of climatology university.
PPTX
Cell Types and Its function , kingdom of life
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharma ospi slides which help in ospi learning
PPTX
GDM (1) (1).pptx small presentation for students
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
A systematic review of self-coping strategies used by university students to ...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
O7-L3 Supply Chain Operations - ICLT Program
Anesthesia in Laparoscopic Surgery in India
Lesson notes of climatology university.
Cell Types and Its function , kingdom of life
Microbial disease of the cardiovascular and lymphatic systems
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharma ospi slides which help in ospi learning
GDM (1) (1).pptx small presentation for students
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
01-Introduction-to-Information-Management.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Complications of Minimal Access Surgery at WLH
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

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