SlideShare a Scribd company logo
10
Most read
11
Most read
14
Most read
SINGLETON
DESIGN
PATTERN
Index
 Intent
 Structure
 Example program
 Explanation
 Participants
 Consequences
 Limitations
 Practical examples
 Intent
 Ensure a class only has one instance, and
provide a global point of access to it.
 Motivation
 It’s important for some classes to have
exactly one instance.
 Although there can be many printers in a
system, there should be only one printer
spooler.
 There should be only one file system (or file
system manager) and one window manager.
Structure
 Example program:
public class TestSingleton {
public static void main(String args[]) {
// Get a reference to the single instance of Singleton.
Singleton s = Singleton.instance();
s.setData(34);
System.out.println("First reference: " + s);
System.out.println("Singleton data value is: " +
s.getData());
// 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());
} }
The test program output is as shown:
First reference:
Singleton data value is: 34
Second reference:
Singleton data value is: 34
 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?
 If invoked concurrently, two instances of the singleton
class could be created. However, this could be
prevented using the following 2 techniques:
i) Make the instance() synchronized. Synchronization
is expensive, however, and it may have a negative
impact on the performance. Moreover, it is really
only needed the first time the unique instance is
created.
ii) Do an eager instantiation of the instance rather
than a lazy instantiation. An eager class loader
loads all the classes comprising the application
code at startup. Such an implementation requires
a static initializer and is thread safe.
Singleton Participants
 Singleton - Defines an instance operation that
lets clients access its unique instance. Instance
is a class operation (static method). May be
responsible for creating its own unique
instance
 Client - Accesses a singleton instance solely
through the singleton’s instance() method.
Singleton Consequences
 Controlled access to sole instance facilitates
strict control over when and how the clients
access it.
 The singleton pattern is an improvement over
global variables.
 It may be subclassed and it is easy to configure
an instance of an application that extends the
functionality of singleton at run-time.
 More flexible than class operations.
Singleton Limitations
 The main limitation of the singleton pattern is
that it permits the creation of only one instance
of the class, while most practical applications
require multiple instances to be initialized.
 Furthermore, in case of singleton, the system
threads fight to access the single instance
thereby degrading the performance of the
application.
Examples
 Cache – The cache can be used as a singleton
object, having a global point of reference for
all future references to the cache object and the
client application uses this in-memory object.
 Configuration File – Any application has a
single point of reference through its
configuration files. Any changes to be made to
the application will have to be made via this
single configuration file.
Thank You

More Related Content

PPTX
Singleton Design Pattern - Creation Pattern
PPTX
Design Pattern - Singleton Pattern
PPT
Introduction to Design Patterns and Singleton
PPTX
Singleton Pattern (Sole Object with Global Access)
PPTX
The Singleton Pattern Presentation
PPTX
Proxy Design Pattern
PDF
Spring Security
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Singleton Design Pattern - Creation Pattern
Design Pattern - Singleton Pattern
Introduction to Design Patterns and Singleton
Singleton Pattern (Sole Object with Global Access)
The Singleton Pattern Presentation
Proxy Design Pattern
Spring Security
Basic Concepts of OOPs (Object Oriented Programming in Java)

What's hot (20)

PPT
Java interfaces
PPT
Software Design Patterns
PPT
Composite Design Pattern
PDF
JPA and Hibernate
PPSX
PPTX
Design pattern (Abstract Factory & Singleton)
PPTX
Springboot Microservices
PPT
C# Exceptions Handling
PDF
ES6 presentation
PDF
Introduction to Spring Framework
PDF
Automation Testing using Selenium
PDF
Spring Framework - Core
PDF
Spring Boot
PPTX
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
PPTX
Introduction to Spring Framework
PPTX
Introduction to AngularJS
PPTX
JAVA AWT
PPT
Structural patterns
PDF
Spring MVC Framework
Java interfaces
Software Design Patterns
Composite Design Pattern
JPA and Hibernate
Design pattern (Abstract Factory & Singleton)
Springboot Microservices
C# Exceptions Handling
ES6 presentation
Introduction to Spring Framework
Automation Testing using Selenium
Spring Framework - Core
Spring Boot
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Introduction to Spring Framework
Introduction to AngularJS
JAVA AWT
Structural patterns
Spring MVC Framework
Ad

Similar to Singleton design pattern (20)

PPT
Singleton
PDF
Mastering Design Patterns in Java: A Comprehensive Guide
PDF
Singleton Sum
PPTX
Design pattern-presentation
PPTX
Design Patterns
PPT
Software Design Patterns
PDF
Design patterns in Java - Monitis 2017
PPT
Introduction to design_patterns
PPTX
OOPSDesign PPT ( introduction to opps and design (
PDF
Robust and Scalable Concurrent Programming: Lesson from the Trenches
PPT
10-design-patterns1.ppt.software engineering
PPTX
PDF
Java - Singleton Pattern
PDF
Design Patterns - GOF
PPT
Design_Patterns_Dr.CM.ppt
PPTX
Cs 1023 lec 8 design pattern (week 2)
PPTX
design_pattern.pptx design_pattern design_pattern
PDF
Design Patterns
PPTX
Evolution of Patterns
PPTX
Design patterns
Singleton
Mastering Design Patterns in Java: A Comprehensive Guide
Singleton Sum
Design pattern-presentation
Design Patterns
Software Design Patterns
Design patterns in Java - Monitis 2017
Introduction to design_patterns
OOPSDesign PPT ( introduction to opps and design (
Robust and Scalable Concurrent Programming: Lesson from the Trenches
10-design-patterns1.ppt.software engineering
Java - Singleton Pattern
Design Patterns - GOF
Design_Patterns_Dr.CM.ppt
Cs 1023 lec 8 design pattern (week 2)
design_pattern.pptx design_pattern design_pattern
Design Patterns
Evolution of Patterns
Design patterns
Ad

Recently uploaded (20)

PDF
Quality Control Management for RMG, Level- 4, Certificate
PDF
Urban Design Final Project-Context
PDF
Africa 2025 - Prospects and Challenges first edition.pdf
PPTX
AD Bungalow Case studies Sem 2.pptxvwewev
PPTX
12. Community Pharmacy and How to organize it
PDF
Phone away, tabs closed: No multitasking
PPTX
Wisp Textiles: Where Comfort Meets Everyday Style
DOCX
actividad 20% informatica microsoft project
PDF
SEVA- Fashion designing-Presentation.pdf
PDF
Key Trends in Website Development 2025 | B3AITS - Bow & 3 Arrows IT Solutions
PPTX
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
PDF
UNIT 1 Introduction fnfbbfhfhfbdhdbdto Java.pptx.pdf
PPTX
An introduction to AI in research and reference management
PPTX
YV PROFILE PROJECTS PROFILE PRES. DESIGN
PDF
Benefits_of_Cast_Aluminium_Doors_Presentation.pdf
PPT
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt
PDF
High-frequency high-voltage transformer outline drawing
PPTX
Special finishes, classification and types, explanation
PDF
GREEN BUILDING MATERIALS FOR SUISTAINABLE ARCHITECTURE AND BUILDING STUDY
PPTX
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx
Quality Control Management for RMG, Level- 4, Certificate
Urban Design Final Project-Context
Africa 2025 - Prospects and Challenges first edition.pdf
AD Bungalow Case studies Sem 2.pptxvwewev
12. Community Pharmacy and How to organize it
Phone away, tabs closed: No multitasking
Wisp Textiles: Where Comfort Meets Everyday Style
actividad 20% informatica microsoft project
SEVA- Fashion designing-Presentation.pdf
Key Trends in Website Development 2025 | B3AITS - Bow & 3 Arrows IT Solutions
Complete Guide to Microsoft PowerPoint 2019 – Features, Tools, and Tips"
UNIT 1 Introduction fnfbbfhfhfbdhdbdto Java.pptx.pdf
An introduction to AI in research and reference management
YV PROFILE PROJECTS PROFILE PRES. DESIGN
Benefits_of_Cast_Aluminium_Doors_Presentation.pdf
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt
High-frequency high-voltage transformer outline drawing
Special finishes, classification and types, explanation
GREEN BUILDING MATERIALS FOR SUISTAINABLE ARCHITECTURE AND BUILDING STUDY
Causes of Flooding by Slidesgo sdnl;asnjdl;asj.pptx

Singleton design pattern

  • 2. Index  Intent  Structure  Example program  Explanation  Participants  Consequences  Limitations  Practical examples
  • 3.  Intent  Ensure a class only has one instance, and provide a global point of access to it.  Motivation  It’s important for some classes to have exactly one instance.  Although there can be many printers in a system, there should be only one printer spooler.  There should be only one file system (or file system manager) and one window manager.
  • 5.  Example program: public class TestSingleton { public static void main(String args[]) { // Get a reference to the single instance of Singleton. Singleton s = Singleton.instance(); s.setData(34); System.out.println("First reference: " + s); System.out.println("Singleton data value is: " + s.getData());
  • 6. // 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()); } }
  • 7. The test program output is as shown: First reference: Singleton data value is: 34 Second reference: Singleton data value is: 34
  • 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?  If invoked concurrently, two instances of the singleton class could be created. However, this could be prevented using the following 2 techniques: i) Make the instance() synchronized. Synchronization is expensive, however, and it may have a negative impact on the performance. Moreover, it is really only needed the first time the unique instance is created.
  • 9. ii) Do an eager instantiation of the instance rather than a lazy instantiation. An eager class loader loads all the classes comprising the application code at startup. Such an implementation requires a static initializer and is thread safe.
  • 10. Singleton Participants  Singleton - Defines an instance operation that lets clients access its unique instance. Instance is a class operation (static method). May be responsible for creating its own unique instance  Client - Accesses a singleton instance solely through the singleton’s instance() method.
  • 11. Singleton Consequences  Controlled access to sole instance facilitates strict control over when and how the clients access it.  The singleton pattern is an improvement over global variables.  It may be subclassed and it is easy to configure an instance of an application that extends the functionality of singleton at run-time.  More flexible than class operations.
  • 12. Singleton Limitations  The main limitation of the singleton pattern is that it permits the creation of only one instance of the class, while most practical applications require multiple instances to be initialized.  Furthermore, in case of singleton, the system threads fight to access the single instance thereby degrading the performance of the application.
  • 13. Examples  Cache – The cache can be used as a singleton object, having a global point of reference for all future references to the cache object and the client application uses this in-memory object.  Configuration File – Any application has a single point of reference through its configuration files. Any changes to be made to the application will have to be made via this single configuration file.