Singleton Pattern

Pattern

Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.

The most common way to implement the pattern follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public final class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return INSTANCE;
    }
}

The code sample above provides the following pattern:

  • Make the default constructor private, to prevent other objects from using the new operator with the Singleton class.
  • Create a static creation method that acts as a constructor.

Singleton design pattern is used in core java classes, for example java.lang.Runtime.

Form JDK Documents

Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.

An application cannot create its own instance of this class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/**
 * Runtime class can be used to get the memory used by a Java applications.
 */
public class RuntimeDemo {
    private static final long KB = 1024;

    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        long startMemory, endMemory, netMemory;

        startMemory = runtime.totalMemory() - runtime.freeMemory()
        // run some application
        endMemory = runtime.totalMemory() - runtime.freeMemory();

        netMemory = endMemory - startMemeory;
        System.out.printf("Application used %d kb memory.\n", 
            netMemory / KB);
    }
}

The Runtime.getRuntime(); is similar to the Singleton.getInstance();.

When to use this pattern?

Use the Singleton pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program; a single logging object, hardware drivers' objects, etc.

Advantage

You can be sure that a class has only a single instance. Moreover, you gain a global access point to that instance.1


  1. Unlike global variables that are unsafe (since any code can potentially overwrite the contents of those variables and crash the app), the Singleton pattern lets you safely access some object from anywhere in the program; it protects the single instance from being overwritten by other code.