Download presentation
1
The Singleton Pattern (Creational)
Design Patterns The Singleton Pattern (Creational) ©SoftMoore Consulting
2
Motivation Some classes should have exactly one instance These classes usually involve the central management of a resource. Examples print spooler – window manager logger – factory for a family of objects an object that manages database connections an object that interacts with a physical device Singleton example in Java class java.lang.Runtime static method getRuntime() returns a reference to the single instance. ©SoftMoore Consulting
3
Design Patterns Singleton Pattern Intent: Ensure a class has only one instance, and provide a global point of access to it Motivation Sometimes we want just a single instance of a class to exist in the system. We need to have that one instance easily accessible. We want to ensure that additional instances of the class can not be created. ©SoftMoore Consulting
4
Singleton Pattern (continued)
Design Patterns Singleton Pattern (continued) Structure Collaborations: Clients access a Singleton instance solely through Singleton’s getInstance() operation. Singleton instance ... getInstance() return instance ©SoftMoore Consulting
5
Singleton Pattern (continued)
Design Patterns Singleton Pattern (continued) Consequences: controlled access to sole instance reduced name space permits refinement of operations and representation (through subclassing) permits a variable number of instances (in case you change your mind) ©SoftMoore Consulting
6
Implementing the Singleton Pattern in Java (Version 1)
Design Patterns Implementing the Singleton Pattern in Java (Version 1) public class Singleton { // the one and only instance private static Singleton instance = null; ... // other fields protected Singleton() ... // initialization code } ©SoftMoore Consulting
7
Singleton Implementation in Java (Version 1 continued)
Design Patterns Singleton Implementation in Java (Version 1 continued) public static Singleton getInstance() { if (instance == null) instance = new Singleton(); return instance; } ... // other methods Note use of lazy initialization – the instance is not actually created until it is needed. ©SoftMoore Consulting
8
Using the Singleton Class
Singleton s = Singleton.getInstance(); s.someMethod(); ©SoftMoore Consulting
9
Problems with this Implementation of the Singleton Class
Does not ensure only one instance since protected constructors in Java can be called by other classes in the same package. Possible solutions: Make the constructor private. If you do this, you should also declare the Singleton class as final, which makes the intention explicit and allows compiler optimizations. Put the class in its own separate package Not thread-safe Thread 1 Thread 2 time if (instance == null) instance = new Singleton(); if (instance == null) instance = new Singleton(); ©SoftMoore Consulting
10
Singleton Implementation in Java (Version 2 – Synchronized)
Design Patterns Singleton Implementation in Java (Version 2 – Synchronized) public class Singleton { ... // same as before public static synchronized Singleton getInstance() if (instance == null) instance = new Singleton(); return instance; } ... same as before ©SoftMoore Consulting
11
Problem with the Version 2 Implementation
This version is thread-safe. Synchronized methods can be expensive performance-wise, and method getInstance() needs to be synchronized only the first time it is called. The performance penalty may be acceptable in many applications. The performance penalty is less in recent versions of the Java Virtual Machine (JVM) ©SoftMoore Consulting
12
Singleton Implementation in Java (Version 3 – Double-checked Locking)
Design Patterns Singleton Implementation in Java (Version 3 – Double-checked Locking) public class Singleton { private volatile static Singleton instance = null; ... // other fields protected Singleton() ... // initialization code } ©SoftMoore Consulting
13
Singleton Implementation in Java (Version 3 continued)
Design Patterns Singleton Implementation in Java (Version 3 continued) public static Singleton getInstance() { if (instance == null) synchronized (Singleton.class) instance = new Singleton(); } return instance; ... // other methods ©SoftMoore Consulting
14
Comments on Version 3 This version is thread-safe.
Avoids the overhead of unnecessary thread synchronization after the first instance is created but might not be noticeably more efficient than synchronizing the getInstance() method for recent versions of Java Declare the static field instance as volatile to ensure that multiple threads handle it correctly. Not guaranteed to work in Java versions 1.4 and earlier. ©SoftMoore Consulting
15
Singleton Implementation in Java (Version 4 – Eager Initialization)
Design Patterns Singleton Implementation in Java (Version 4 – Eager Initialization) public class Singleton { private static final Singleton instance = new Singleton(); public static Singleton getInstance() { return instance; } protected Singleton() { ... } ... } ©SoftMoore Consulting
16
Note: java.lang.Runtime uses this approach
Comments on Version 4 This version implementation is thread-safe and efficient. For this implementation, method getInstance() does not need to be synchronized in order to guarantee thread-safety. This version does not use lazy initialization – the instance is always created. (not usually a problem) Modern implementations of the JVM will are almost certain to inline the call to the static getInstance() method (a Factory Method). Note: java.lang.Runtime uses this approach ©SoftMoore Consulting
17
Design Patterns Singleton Implementation in Java (Version 5 – Single-element Enum Type) public enum Singleton { INSTANCE; // other methods } Using the singleton class INSTANCE.someMethod(); Note: Enum types were added to Java 5 in 2004. ©SoftMoore Consulting
18
Comments on Version 5 This version is thread-safe for reasons similar to those for version 4. Concise Provides the serialization machinery for free Guarantees against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. Recommended by Joshua Bloch as “the best way to implement a singleton” ©SoftMoore Consulting
19
Singleton Implementation in Java: Other Versions
Nested class (Bill Pugh) ... See references at end of slides for details. ©SoftMoore Consulting
20
Real Example of a Singleton Pattern
package ...; import ...; /** * This class provides general logging support. <br/> * (Implements the Singleton pattern.) */ public class Log { private static final String slash = File.separator; private static Log instance = null; private static Date dateCreated; private PrintWriter logWriter; (continued on next slide) ©SoftMoore Consulting
21
Real Example of a Singleton Pattern (continued)
/** * The default constructor for this class. */ private Log() { logWriter = null; try Properties properties = AppProperties.getInstance(); // get tomcat home from the environment Map<String, String> env = System.getenv(); String tomcatHome = env.get("CATALINA_HOME"); Note: another singleton (continued on next slide) ©SoftMoore Consulting
22
Real Example of a Singleton Pattern (continued)
// set up path for log file java.util.Date today = new java.util.Date(); dateCreated = new Date(today); String timestamp = ... ; String webappHome = ... ; String logPathName = tomcatHome + slash + webappHome + slash + "logs" + slash + logFileName + timestamp + ".log"; logWriter = new PrintWriter (new FileWriter(logPathName, true), true); } catch(IOException ex) { ex.printStackTrace(); logWriter = new PrintWriter(System.err); (continued on next slide) ©SoftMoore Consulting
23
Real Example of a Singleton Pattern (continued)
/** * Returns the single instance of this class */ public static synchronized Log getInstance() { if(instance == null) instance = new Log(); else Date today = new Date(); if (!dateCreated.equals(today)) } return instance; (continued on next slide) ©SoftMoore Consulting
24
Real Example of a Singleton Pattern (continued)
/** * Logs a general informational message that does not * represent a problem. (Not for logging Java Exceptions * or Errors.) */ public synchronized void logMessage(String methodName, String message) { String fullMessage = " Method: " + methodName + "\n Message: " + message; logTimestamp(); logWriter.println(fullMessage); logWriter.println(); logWriter.flush(); } (continued on next slide) ©SoftMoore Consulting
25
Real Example of a Singleton Pattern (continued)
/** * Logs an error message resulting from a Java Exception */ public synchronized void logException(String methodName, Exception ex) { ... } ... // other logging methods public synchronized void print(String message) logWriter.print(message); (continued on next slide) ©SoftMoore Consulting
26
Real Example of a Singleton Pattern (continued)
public synchronized void println(String message) { logWriter.println(message); } public synchronized void println() logWriter.println(); private synchronized void logTimestamp() logWriter.println (Calendar.getInstance().getTime().toString()); ©SoftMoore Consulting
27
References Singleton pattern (Wikipedia)
Singleton Pattern (Object-Oriented Design) Java Singleton Design Pattern Best Practices with Examples (Pankaj) Simply Singleton (JavaWorld) ©SoftMoore Consulting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.