When is a singleton not a singleton?
Avoid multiple singleton instances by keeping these tips in mind
By Joshua Fox, JavaWorld.com, 01/12/01
The singleton is a useful design pattern for allowing only one instance of your class, but common mistakes can inadvertently
allow more than one instance to be created. In this article, I'll show you how that can happen and how to avoid it.
The singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more
objects if the situation changes. Since there is only one singleton instance, any instance fields of a singleton will occur
only once per class, just like static fields.
Singletons often control access to resources such as database connections or sockets. For example, if you have a license for
only one connection for your database or your JDBC driver has trouble with multithreading, the singleton makes sure that only
one connection is made or that only one thread can access the connection at a time. If you add database connections or use
a JDBC driver that allows multithreading, the singleton can be easily adjusted to allow more connections.
Moreover, singletons can be stateful; in this case, their role is to serve as a unique repository of state. If you are implementing
a counter that needs to give out sequential and unique numbers (such as the machine that gives out numbers in the deli), the
counter needs to be globally unique. The singleton can hold the number and synchronize access; if later you want to hold counters
in a database for persistence, you can change the private implementation of the singleton without changing the interface.
1 |
2 |
3 |
4 |
5 |
6 |
7 |
Next >
Resources
- I have created classes that illustrate some of the points made in this article and offer suggestions for illustrating others.
To download the source code, go to
http://www.javaworld.com/javaworld/jw-01-2001/singleton/jw-0112-singleton.zip
- "Singleton's Rule" by Tony Sintes (JavaWorld, December 2000) outlines why singletons promote good OO design compared to static classes
http://www.javaworld.com/javaworld/javaqa/2000-12/03-qa-1221-singleton.html
- "Programming Java Threads in the Real World, Part 7," Allen Holub (JavaWorld, April 1999) describes threading issues relevant to singletons
http://www.javaworld.com/javaworld/jw-04-1999/jw-04-toolbox.html
- "Java Tip 67Lazy Instantiation," by Philip Bishop and Nigel Warren (JavaWorld) extensively discusses singletons in the context of defered object creation
http://www.javaworld.com/javaworld/javatips/jw-javatip67_p.html
- "Create a Custom Java 1.2-style ClassLoader," Ken McCrary (JavaWorld, March 2000)
http://www.javaworld.com/javaworld/jw-03-2000/jw-03-classload.html
- For answers to your pressing design patterns questions, check out the JavaWorld Programming Theory & Practice discussion
http://www.itworld.com/jump/jw-0112-singleton/forums.itworld.com/webx?14@@.ee6b806
- To quickly search for other important JavaWorld articles based on subject, visit our useful Topical Index
http://www.javaworld.com/javaworld/topicalindex/jw-ti-index.html
- Sign up for the JavaWorld This Week free weekly email newsletter and keep up with what's new at JavaWorld
http://www.idg.net/jw-subscribe
- The Patterns Home Page
http://hillside.net/patterns
- "The Double-Checked Locking is Broken Declaration," David Bacon, et al.
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
- "Class Loaders as a Namespace Mechanism," Stuart Halloway (Java Developer Connection Tech Tips, October 31, 2000)
http://developer.java.sun.com/developer/TechTips/2000/tt1027.html#tip1
- "Garbage Collection," Chapter 9 of Inside the Java 2 Virtual Machine, Bill Venners (McGraw-Hill Professional Publishing, 1999)
http://www.amazon.com/exec/obidos/ASIN/0071350934/o/qid=977184545/sr=8-1/ref=aps_sr_b_1_1/002-5013741-2144051
- "Implementing the Singleton Pattern in Java," Rodney Waldhoff (August 8, 1998) explains a few ways to implement the singleton
in Java, including a couple that allow subclassing
http://members.tripod.com/rwald/java/articles/Singleton_in_Java.html