Dr. Axel Rauschmayer is a freelance software engineer, blogger and educator, located in Munich, Germany. Axel is a DZone MVB and is not an employee of DZone and has posted 246 posts at DZone. You can read more from them at their website. View Full User Profile

The Singleton Pattern and its Simplest Implementation in Java

04.11.2011
| 10055 views |
  • submit to reddit

This post motivates the Singleton design pattern and explains its implementation in Java.

The Singleton is a design pattern [1] to ensure that a class has only a single instance. The name is derived from the mathematical term singleton which means a set with exactly one element. This pattern is mainly used if a class implements a service (which is usually instantiated only once). A typical Singleton looks as follows:

    public class Singleton {
 
        public static final Singleton INSTANCE = new Singleton();
 
        // Private constructor prevents external instantiation
        private Singleton() {
        }
    }
Making the constructor private prevents code that is external to the class from instantiating it. This is done for two reasons:
  • Enforce the constraint of exactly one instance.
  • Hint at how this class is to be used: To get an instance, one doesn’t invoke the constructor, but accesses the static variable. Thus, one cannot accidentally misuse the class. Classes with factory methods have private constructors for the same reason.
And now for the simplest way of implementing a Singleton in Java: as an enum.
    public enum Singleton {
       INSTANCE;
       
       // Instance variables and methods go here
    }
The enum automatically ensures that the Singleton cannot be instantiated externally, and the enum constant INSTANCE is a convenient way of creating the only instance.

As an alternative to the above solutions, one could also use static methods and store internal state in static variables. But then one loses some of the amenities of instances, such as the ability to conform to an interface. Or the ability to pass around a reference and invoke methods. It should be noted that the Singleton pattern hampers testability, because Singletons are harder to replace in a test environment. If you want to implement services (or components) via classes, it is better to use dependency injection (e.g. via Guice [3]). In addition to increased configurability, you also get explicit dependencies stated in constructors.

Related reading:

  1. The classic book on design patterns, recommended: “Design Patterns: Elements of Reusable Object-Oriented Software” by Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides.
  2. The Wikipedia article on the Singleton pattern (which inspired this post and from which the enum implementation is taken).
  3. Google Guice, a lightweight dependency injection framework for Java. The website also explains dependency injection well.

 

From http://www.2ality.com/2011/04/singleton-pattern-and-its-simplest.html

Published at DZone with permission of Axel Rauschmayer, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Slim Ouertani replied on Mon, 2011/04/11 - 3:51am

Yes, I summerised this pattern step by step in this presentation:

http://ouertani.com/2009/10/summarise-singleton-pattern/

Jerome Meuret replied on Mon, 2011/04/11 - 6:41am

This is well explained in Effective Java by Joshua Bloch. Enum's approach also prevents instanciation via deserialization.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.