0% found this document useful (0 votes)
49 views

Singleton Pattern: Bryan Hansen

The Singleton pattern ensures that only one instance of a class is created. It provides a global point of access to the instance and guarantees thread safety. Examples include runtime environments, loggers, and graphic managers. The class controls its own lifecycle and is implemented as a private constructor with a static method to retrieve the sole instance. While useful, singletons should not be overused as they can be difficult to test and confuse responsibilities.

Uploaded by

Kuby Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Singleton Pattern: Bryan Hansen

The Singleton pattern ensures that only one instance of a class is created. It provides a global point of access to the instance and guarantees thread safety. Examples include runtime environments, loggers, and graphic managers. The class controls its own lifecycle and is implemented as a private constructor with a static method to retrieve the sole instance. While useful, singletons should not be overused as they can be difficult to test and confuse responsibilities.

Uploaded by

Kuby Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Singleton Pattern

Bryan Hansen
twitter: bh5k | http://www.linkedin.com/in/hansenbryan
Concepts
▪ Only one instance created
▪ Guarantees control of a resource
▪ Lazily loaded
Drag picture to
▪ Examples: placeholder or click
▪ Runtime icon to add a graphic
▪ Logger
▪ Spring Beans
▪ Graphic Managers
Design

Class is responsible for lifecycle


Static in nature
Needs to be thread safe
Private instance
Private constructor
No parameters required for construction
Everyday Example - Runtime Env
Runtime  singletonRuntime  =  Runtime.getRuntime();  

singletonRuntime.gc();  
     
System.out.println(singletonRuntime);  
     
Runtime  anotherInstance  =  Runtime.getRuntime();  
     
System.out.println(anotherInstance);  
     
if(singletonRuntime  ==  anotherInstance)  {  
  System.out.println(“They  are  the  same  instance”);  
}
Exercise Singleton

Create Singleton
Demonstrate only one instance
created
Lazy Loaded
Thread safe operation
Pitfalls
▪ Often overused
▪ Difficult to unit test
▪ If not careful, not thread-safe
▪ Sometimes confused for Factory
▪ java.util.Calendar is NOT a Singleton
▪ Prototype
Contrast

Singleton Factory
▪ Returns same instance ▪ Returns various instances
▪ One constructor method - no ▪ Multiple constructors
args ▪ Interface driven
▪ No Interface ▪ Adaptable to environment more
easily
Singleton Summary

• Guarantee one instance


• Easy to implement
• Solves a well defined problem
• Don’t abuse it

You might also like