SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
Asma CHERIF
 
Design patterns
Design patternsDesign patterns
Design patterns
abhisheksagi
 
Singleton Pattern (Sole Object with Global Access)
Singleton Pattern (Sole Object with Global Access)Singleton Pattern (Sole Object with Global Access)
Singleton Pattern (Sole Object with Global Access)
Sameer Rathoud
 
Design Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan Gole
Chetan Gole
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
Mudasir Qazi
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
Rana Muhammad Asif
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
Mudasir Qazi
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
Jonathan Simon
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Satheesh Sukumaran
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
paramisoft
 
Creational pattern
Creational patternCreational pattern
Creational pattern
Himanshu
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
Aman Jain
 
Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
Shakil Ahmed
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
Anjan Kumar Bollam
 
Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
Shakil Ahmed
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
07 java collection
07 java collection07 java collection
07 java collection
Abhishek Khune
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
Hitesh-Java
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
Andreas Enbohm
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
Asma CHERIF
 
Singleton Pattern (Sole Object with Global Access)
Singleton Pattern (Sole Object with Global Access)Singleton Pattern (Sole Object with Global Access)
Singleton Pattern (Sole Object with Global Access)
Sameer Rathoud
 
Design Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan Gole
Chetan Gole
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
Mudasir Qazi
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
Mudasir Qazi
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
Jonathan Simon
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
paramisoft
 
Creational pattern
Creational patternCreational pattern
Creational pattern
Himanshu
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
Aman Jain
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
Hitesh-Java
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
Andreas Enbohm
 

Similar to The Singleton Pattern Presentation (20)

Creating and destroying objects
Creating and destroying objectsCreating and destroying objects
Creating and destroying objects
Sandeep Chawla
 
Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017
Arsen Gasparyan
 
Design_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.pptDesign_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.ppt
C Meenakshi Meyyappan
 
Creational - The Singleton Design Pattern
Creational - The Singleton Design PatternCreational - The Singleton Design Pattern
Creational - The Singleton Design Pattern
RagibShahriar8
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
Simple Singleton Java
Simple Singleton JavaSimple Singleton Java
Simple Singleton Java
Christian Hipolito
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design Patterns
Mohammad Shaker
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
Lalit Kale
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
Nishith Shukla
 
Best Practices
Best PracticesBest Practices
Best Practices
Roy Marmelstein
 
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
Shawn Brito
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
Shipra Swati
 
PATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design PatternsPATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design Patterns
Michael Heron
 
Software Architecture and Design Patterns Notes.pptx
Software Architecture and Design Patterns Notes.pptxSoftware Architecture and Design Patterns Notes.pptx
Software Architecture and Design Patterns Notes.pptx
VivekanandaGN2
 
Java basics
Java basicsJava basics
Java basics
Shivanshu Purwar
 
Sda 8
Sda   8Sda   8
Sda 8
AmberMughal5
 
Singleton class in Java
Singleton class in JavaSingleton class in Java
Singleton class in Java
Rahul Sharma
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
Ayush Sharma
 
Creating and destroying objects
Creating and destroying objectsCreating and destroying objects
Creating and destroying objects
Sandeep Chawla
 
Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017
Arsen Gasparyan
 
Creational - The Singleton Design Pattern
Creational - The Singleton Design PatternCreational - The Singleton Design Pattern
Creational - The Singleton Design Pattern
RagibShahriar8
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design Patterns
Mohammad Shaker
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
Lalit Kale
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
Nishith Shukla
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
Shawn Brito
 
PATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design PatternsPATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design Patterns
Michael Heron
 
Software Architecture and Design Patterns Notes.pptx
Software Architecture and Design Patterns Notes.pptxSoftware Architecture and Design Patterns Notes.pptx
Software Architecture and Design Patterns Notes.pptx
VivekanandaGN2
 
Singleton class in Java
Singleton class in JavaSingleton class in Java
Singleton class in Java
Rahul Sharma
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
Ayush Sharma
 
Ad

More from JAINIK PATEL (7)

Facade pattern
Facade patternFacade pattern
Facade pattern
JAINIK PATEL
 
SMS
SMSSMS
SMS
JAINIK PATEL
 
Architecture of Mobile Computing
Architecture of Mobile ComputingArchitecture of Mobile Computing
Architecture of Mobile Computing
JAINIK PATEL
 
security issue
security issuesecurity issue
security issue
JAINIK PATEL
 
Mobile Computing
Mobile ComputingMobile Computing
Mobile Computing
JAINIK PATEL
 
112321 112333 wirless application protocol
112321 112333 wirless application protocol112321 112333 wirless application protocol
112321 112333 wirless application protocol
JAINIK PATEL
 
Facadepattern
FacadepatternFacadepattern
Facadepattern
JAINIK PATEL
 
Architecture of Mobile Computing
Architecture of Mobile ComputingArchitecture of Mobile Computing
Architecture of Mobile Computing
JAINIK PATEL
 
112321 112333 wirless application protocol
112321 112333 wirless application protocol112321 112333 wirless application protocol
112321 112333 wirless application protocol
JAINIK PATEL
 
Ad

Recently uploaded (20)

Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 

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
  翻译: