Brief introduction to Java Singleton Classes

The Singleton Pattern is quite simple but at the same time extremely useful. If you have done any serious programming then there is a good chance that you have fudged the behavior into a class or forced a class to act as a singleton without knowing the pattern. In this very short article we will examine the common method to construct a singleton.

The main purposes of a singleton are to:

  • Provide that only one instance of the class is created
  • Provide a single point of obtaining that instance

The occasions where you want to ensure only one instance of a class are numerous and mostly obvious. Consider an application that performs many validation tests on a database and combines the results into a consolidated report. The simple way to consolidate the differing results is to allow each test to store it’s results into a central repository and then when desired the repository would combine the results and provide the formatted output. In such a situation you want only one instance of the result repository to exist so that all test results are stored in the same holding point.

The time honored method to provide a singleton is make the constructor non-public and provide a method called getInstance that returns the single instance of the class. Let’s look at how a simple singleton class achieves this.

public class SimpleSingleton {
	private static SimpleSingleton instance = null;

	private SimpleSingleton(){}

	public synchronized SimpleSingleton getInstance() {
		if (instance == null) {
			instance = new SimpleSingleton();
		}
		return instance;
	}
}

Some notes about derivations to this method. Obviously if you intended to subclass then the constructor must be protected instead of private. However if you make it protected it’s recommended that you put the class in it’s own package to prevent accidental access to the constructor from other classes in the package. Also it’s possible to reduce the overhead of the synchronization by syncing on the instance variable but in practice you won’t call getInstance enough to make it worth worrying about. Personally I recommend just taking the easy approach and creating a class variable at the top of each class that would access the singleton and saving all the method calls.

That’s it for the basics of singleton creation. There are numerous ways to expand on this simple example and we’ll cover several of them in an upcoming post.

For more coverage here are some selected readings on the singleton pattern:

http://en.wikipedia.org/wiki/Singleton_pattern

http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html

http://blogs.techrepublic.com.com/programming-and-development/?p=449

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">