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

More Related Content

What's hot (20)

Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
Haris Bin Zahid
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
Mindfire Solutions
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Anuja Arosha
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
Aman Jain
 
Activity diagram
Activity diagramActivity diagram
Activity diagram
LOKENDRA PRAJAPATI
 
INTRODUCTION TO UML DIAGRAMS
INTRODUCTION TO UML DIAGRAMSINTRODUCTION TO UML DIAGRAMS
INTRODUCTION TO UML DIAGRAMS
Ashita Agrawal
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
Sanae BEKKAR
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
Anton Keks
 
DESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy PatternsDESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy Patterns
International Institute of Information Technology (I²IT)
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
Jadson Santos
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
Mudasir Qazi
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
Shakil Ahmed
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
Anjan Kumar Bollam
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
Ramakant Soni
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
Prawesh Shrestha
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
Shakib Hasan Sumon
 
Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
Shakil Ahmed
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Iterator Design Pattern
Iterator Design PatternIterator Design Pattern
Iterator Design Pattern
Varun Arora
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
Srikanth R Vaka
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
Haris Bin Zahid
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
Mindfire Solutions
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
Aman Jain
 
INTRODUCTION TO UML DIAGRAMS
INTRODUCTION TO UML DIAGRAMSINTRODUCTION TO UML DIAGRAMS
INTRODUCTION TO UML DIAGRAMS
Ashita Agrawal
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
Sanae BEKKAR
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
Anton Keks
 
Introduction to angular with a simple but complete project
Introduction to angular with a simple but complete projectIntroduction to angular with a simple but complete project
Introduction to angular with a simple but complete project
Jadson Santos
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
Mudasir Qazi
 
Collaboration diagram- UML diagram
Collaboration diagram- UML diagram Collaboration diagram- UML diagram
Collaboration diagram- UML diagram
Ramakant Soni
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
Prawesh Shrestha
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
Shreyans Pathak
 
Iterator Design Pattern
Iterator Design PatternIterator Design Pattern
Iterator Design Pattern
Varun Arora
 

Similar to Singleton design pattern (20)

P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
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
 
Most Useful Design Patterns
Most Useful Design PatternsMost Useful Design Patterns
Most Useful Design Patterns
Steven Smith
 
Design_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.pptDesign_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.ppt
C Meenakshi Meyyappan
 
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Steven Smith
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Steven Smith
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
sukumarraju6
 
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
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
amitarcade
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Rafael Coutinho
 
OOPSDesign PPT ( introduction to opps and design (
OOPSDesign PPT ( introduction to opps and design (OOPSDesign PPT ( introduction to opps and design (
OOPSDesign PPT ( introduction to opps and design (
bhfcvh531
 
Design patterns
Design patternsDesign patterns
Design patterns
mudabbirwarsi
 
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
 
Singletons
SingletonsSingletons
Singletons
Iwan Beer
 
Observer & singleton pattern
Observer  & singleton patternObserver  & singleton pattern
Observer & singleton pattern
babak danyal
 
ActionScript Design Patterns
ActionScript Design Patterns ActionScript Design Patterns
ActionScript Design Patterns
Yoss Cohen
 
Sda 8
Sda   8Sda   8
Sda 8
AmberMughal5
 
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
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
SajidTk2
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
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
 
Most Useful Design Patterns
Most Useful Design PatternsMost Useful Design Patterns
Most Useful Design Patterns
Steven Smith
 
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013Common ASP.NET Design Patterns - Telerik India DevCon 2013
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Steven Smith
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Steven Smith
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
sukumarraju6
 
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
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
amitarcade
 
OOPSDesign PPT ( introduction to opps and design (
OOPSDesign PPT ( introduction to opps and design (OOPSDesign PPT ( introduction to opps and design (
OOPSDesign PPT ( introduction to opps and design (
bhfcvh531
 
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
 
Observer & singleton pattern
Observer  & singleton patternObserver  & singleton pattern
Observer & singleton pattern
babak danyal
 
ActionScript Design Patterns
ActionScript Design Patterns ActionScript Design Patterns
ActionScript Design Patterns
Yoss Cohen
 
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
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
SajidTk2
 
Ad

Recently uploaded (20)

The Irrational City | Unseen Forces of Placemaking
The Irrational City | Unseen Forces of PlacemakingThe Irrational City | Unseen Forces of Placemaking
The Irrational City | Unseen Forces of Placemaking
Leanne Munyori
 
Are you Transitioning or Refining Now..?
Are you Transitioning or Refining Now..?Are you Transitioning or Refining Now..?
Are you Transitioning or Refining Now..?
Gregory Vigneaux
 
Steam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptxSteam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptx
andripapa1
 
Emirates Agriculture Prensentation Badges GOLD.pdf
Emirates Agriculture Prensentation Badges GOLD.pdfEmirates Agriculture Prensentation Badges GOLD.pdf
Emirates Agriculture Prensentation Badges GOLD.pdf
asfianoor1
 
Internet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free DownloadInternet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free Download
Designer
 
Design of a Low-Power VLSI Router for Network-on-Chip.pptx
Design of a Low-Power VLSI Router for Network-on-Chip.pptxDesign of a Low-Power VLSI Router for Network-on-Chip.pptx
Design of a Low-Power VLSI Router for Network-on-Chip.pptx
BapujiBanothu
 
4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free
Designer
 
10.1155-2024-1048933Figurefig0008.pptx.ppt
10.1155-2024-1048933Figurefig0008.pptx.ppt10.1155-2024-1048933Figurefig0008.pptx.ppt
10.1155-2024-1048933Figurefig0008.pptx.ppt
suchandasaha7
 
An updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdfAn updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdf
Elle Geraghty
 
Minimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptxMinimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptx
ESTEFANOANDREYGARCIA
 
Wondershare Filmora Crack Free Download 2025
Wondershare Filmora Crack Free Download 2025Wondershare Filmora Crack Free Download 2025
Wondershare Filmora Crack Free Download 2025
Designer
 
PPT UNTUK ISU STRATEGIS (1).pptx PPT UNTUK ISU STRATEGIS (1).pptx
PPT UNTUK ISU STRATEGIS (1).pptx PPT UNTUK ISU STRATEGIS (1).pptxPPT UNTUK ISU STRATEGIS (1).pptx PPT UNTUK ISU STRATEGIS (1).pptx
PPT UNTUK ISU STRATEGIS (1).pptx PPT UNTUK ISU STRATEGIS (1).pptx
rachmatunnisa29
 
masterddedeeeeeeeeedded seminar (1).pptx
masterddedeeeeeeeeedded seminar (1).pptxmasterddedeeeeeeeeedded seminar (1).pptx
masterddedeeeeeeeeedded seminar (1).pptx
tgavel7869
 
Emily's slide design 101 - training module
Emily's slide design 101 - training moduleEmily's slide design 101 - training module
Emily's slide design 101 - training module
yourmisswright
 
Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.
vanzan01
 
Presentation mockup using lots of animals
Presentation mockup using lots of animalsPresentation mockup using lots of animals
Presentation mockup using lots of animals
ChunChihChenPhD
 
dFdDVWeb_Design_Basics_Presentation.pptx
dFdDVWeb_Design_Basics_Presentation.pptxdFdDVWeb_Design_Basics_Presentation.pptx
dFdDVWeb_Design_Basics_Presentation.pptx
AKSHAYKAMBLE806728
 
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdfEEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
CastroAngeloReoD
 
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdfMOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
asfianoor1
 
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
Friends of Figm a, Sydney
 
The Irrational City | Unseen Forces of Placemaking
The Irrational City | Unseen Forces of PlacemakingThe Irrational City | Unseen Forces of Placemaking
The Irrational City | Unseen Forces of Placemaking
Leanne Munyori
 
Are you Transitioning or Refining Now..?
Are you Transitioning or Refining Now..?Are you Transitioning or Refining Now..?
Are you Transitioning or Refining Now..?
Gregory Vigneaux
 
Steam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptxSteam-Education-PowerPoint-Templates.pptx
Steam-Education-PowerPoint-Templates.pptx
andripapa1
 
Emirates Agriculture Prensentation Badges GOLD.pdf
Emirates Agriculture Prensentation Badges GOLD.pdfEmirates Agriculture Prensentation Badges GOLD.pdf
Emirates Agriculture Prensentation Badges GOLD.pdf
asfianoor1
 
Internet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free DownloadInternet Download Manager Crack Patch Latest IDM Free Download
Internet Download Manager Crack Patch Latest IDM Free Download
Designer
 
Design of a Low-Power VLSI Router for Network-on-Chip.pptx
Design of a Low-Power VLSI Router for Network-on-Chip.pptxDesign of a Low-Power VLSI Router for Network-on-Chip.pptx
Design of a Low-Power VLSI Router for Network-on-Chip.pptx
BapujiBanothu
 
4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free4K Video Downloader Crack (2025) + License Key Free
4K Video Downloader Crack (2025) + License Key Free
Designer
 
10.1155-2024-1048933Figurefig0008.pptx.ppt
10.1155-2024-1048933Figurefig0008.pptx.ppt10.1155-2024-1048933Figurefig0008.pptx.ppt
10.1155-2024-1048933Figurefig0008.pptx.ppt
suchandasaha7
 
An updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdfAn updated content measurement model - Elle Geraghty Content Strategy.pdf
An updated content measurement model - Elle Geraghty Content Strategy.pdf
Elle Geraghty
 
Minimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptxMinimalist Pitch Deck by slide Slidesgo.pptx
Minimalist Pitch Deck by slide Slidesgo.pptx
ESTEFANOANDREYGARCIA
 
Wondershare Filmora Crack Free Download 2025
Wondershare Filmora Crack Free Download 2025Wondershare Filmora Crack Free Download 2025
Wondershare Filmora Crack Free Download 2025
Designer
 
PPT UNTUK ISU STRATEGIS (1).pptx PPT UNTUK ISU STRATEGIS (1).pptx
PPT UNTUK ISU STRATEGIS (1).pptx PPT UNTUK ISU STRATEGIS (1).pptxPPT UNTUK ISU STRATEGIS (1).pptx PPT UNTUK ISU STRATEGIS (1).pptx
PPT UNTUK ISU STRATEGIS (1).pptx PPT UNTUK ISU STRATEGIS (1).pptx
rachmatunnisa29
 
masterddedeeeeeeeeedded seminar (1).pptx
masterddedeeeeeeeeedded seminar (1).pptxmasterddedeeeeeeeeedded seminar (1).pptx
masterddedeeeeeeeeedded seminar (1).pptx
tgavel7869
 
Emily's slide design 101 - training module
Emily's slide design 101 - training moduleEmily's slide design 101 - training module
Emily's slide design 101 - training module
yourmisswright
 
Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.Lori Vanzant Portfolio. Take a look! ty.
Lori Vanzant Portfolio. Take a look! ty.
vanzan01
 
Presentation mockup using lots of animals
Presentation mockup using lots of animalsPresentation mockup using lots of animals
Presentation mockup using lots of animals
ChunChihChenPhD
 
dFdDVWeb_Design_Basics_Presentation.pptx
dFdDVWeb_Design_Basics_Presentation.pptxdFdDVWeb_Design_Basics_Presentation.pptx
dFdDVWeb_Design_Basics_Presentation.pptx
AKSHAYKAMBLE806728
 
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdfEEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
EEE178-PPT-Theme iasodhajsdkjashdlaskdjbaksdkashdlkasdlkja;dj;kdada.pptx.pdf
CastroAngeloReoD
 
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdfMOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
MOCCAE SUSTAINABLE TROPHY 2025 Presentation.pdf
asfianoor1
 
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
𝗗𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗳𝗼𝗿 𝗮𝗹𝗹: 𝗜𝗻𝗰𝗹𝘂𝘀𝗶𝘃𝗲 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲 𝗳𝗼𝗿 𝗕𝗲𝘁𝘁𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲𝘀
Friends of Figm a, Sydney
 
Ad

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.