Java Design Pattern: Singleton

Singleton pattern is one of the most commonly used patterns in Java. It is used to control the number of objects created by preventing external instantiation and modification. This concept can be generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects, such as:

  1. private constructor - no other class can instantiate a new object.
  2. private reference - no external modification.
  3. public static method is the only place that can get an object.

The Story for Singleton

Here is a simple use case. A country can have only one president. So whenever a president is needed, the only president should be returned instead of creating a new one. The getPresident() method will make sure there is always only one president created.

Class Diagram and Code

singleton

Eager Mode:

public class AmericaPresident {
	private static final AmericaPresident thePresident = new AmericaPresident();
 
	private AmericaPresident() {}
 
	public static AmericaPresident getPresident() {
		return thePresident;
	}
}

thePresident is declared as final, so it will always contain the same object reference.

Lazy Mode:

public class AmericaPresident {
	private static AmericaPresident thePresident;
 
	private AmericaPresident() {}
 
	public static AmericaPresident getPresident() {
		if (thePresident == null) {
			thePresident = new AmericaPresident();
		}
		return thePresident;
	}
}

Singleton Pattern Used in Java Stand Library

java.lang.Runtime#getRuntime() is a frequently used method from Java standard library. getRunTime() returns the runtime object associated with the current Java application.

class Runtime {
	private static Runtime currentRuntime = new Runtime();
 
	public static Runtime getRuntime() {
		return currentRuntime;
	}
 
	private Runtime() {}
 
	//... 
}

Here is a simple example of using getRunTime(). It reads a webpage on a Windows system.

Process p = Runtime.getRuntime().exec(
		"C:/windows/system32/ping.exe programcreek.com");
//get process input stream and put it to buffered reader
BufferedReader input = new BufferedReader(new InputStreamReader(
		p.getInputStream()));
 
String line;
while ((line = input.readLine()) != null) {
	System.out.println(line);
}
 
input.close();

Output:

Pinging programcreek.com [198.71.49.96] with 32 bytes of data:
Reply from 198.71.49.96: bytes=32 time=53ms TTL=47
Reply from 198.71.49.96: bytes=32 time=53ms TTL=47
Reply from 198.71.49.96: bytes=32 time=52ms TTL=47
Reply from 198.71.49.96: bytes=32 time=53ms TTL=47

Ping statistics for 198.71.49.96:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 52ms, Maximum = 53ms, Average = 52ms

Another Implementation of Singleton Pattern

As private constructor doesn't protect from instantiation via reflection, Joshua Bloch (Effective Java) proposes a better implementation of Singleton. If you are not familiar with Enum, here is a good example from Oracle.

public enum AmericaPresident{
	INSTANCE;
 
	public static void doSomething(){
		//do something
	}
}

* Thanks to Dan's comment.

Category >> Design Patterns Stories  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. yunche.ch on 2013-10-30

    The example had not show the case of concurrent

  2. Dao on 2013-12-4

    what the example saying?

  3. Ma#E$# on 2013-12-6

    NIce idea dude…

  4. Hans on 2014-3-12

    Runtime.java

    public class Runtime {
    private static Runtime currentRuntime = new Runtime();

    public static Runtime getRuntime() {
    return currentRuntime;
    }

    private Runtime() {}

    // …
    }

  5. Neo on 2014-3-25

    Lazy mode and Eager mode?

  6. dan on 2014-5-6

    Private constructor doesn’t protect from instantiation via reflection.

    Joshua Bloch(Effective Java) proposes a better implementation of Singleton:

    public enum Singleton{
    INSTANCE;
    }

  7. ryanlr on 2014-5-8

    Good point, thanks!

  8. Anil Nivargi on 2014-6-28

    Nice Article..here another one blog explained same concept in different approaches please go through this blog http://adnjavainterview.blogspot.in/2014/06/singleton-design-pattern-in-java-with.html

  9. Nathan on 2014-9-29

    There is something wrong with the Lazy Mode, the method is not threadsafe, when first invoking, Maybe it will return different instance. so i think the right code is:

    public class AmericaPresident {

    private static AmericaPresident thePresident;

    private AmericaPresident() {}

    public synchronized static AmericaPresident getPresident() {
    if (thePresident == null) {
    thePresident = new AmericaPresident();
    }
    return thePresident;
    }
    }

  10. Hi on 2014-12-6

    It’s ok man. The main object of this tutorial is Design pattern not concurrency. All the online tutorials would then require synchronised blocks all the time. Just consider this as the tutorial to Singleton.

Leave a comment

*