Thursday, 17 July 2014

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.

Explicitly call super constructor in sub constructor

The following code is OK:
sub-constructor-with-parameter
The Sub constructor explicitly call the super constructor with parameter. The super constructor is defined, and good to invoke.
4. The Rule
In brief, the rules is: sub class constructor has to invoke super class instructor, either explicitly by programmer or implicitly by compiler. For either way, the invoked super constructor has to be defined.

A Common Error Message: Implicit super constructor is undefined for default constructor

This is a compilation error message seen by a lot of Java developers:
"Implicit super constructor is undefined for default constructor. Must define an explicit constructor"
Implicit super constructor is undefined for default constructor
This compilation error occurs because the default super constructor is undefined. In Java, if a class does not define a constructor, compiler will insert a default no-argument constructor for the class by default. If a constructor is defined in Super class, in this case Super(String s), compiler will not insert the default no-argument constructor. This is the situation for the Super class above.
The constructors of the Sub class, either with-argument or no-argument, will call the no-argument Super constructor. Since compiler tries to insert super() to the 2 constructors in the Sub class, but the Super's default constructor is not defined, compiler reports the error message.
To fix this problem, simply 1) add a Super() constructor to the Super class like
public Super(){
    System.out.println("Super");
}
, or 2) remove the self-defined Super constructor, or 3) add super(value) to sub constructors.

Why creating an object of the sub class invokes also the constructor of the super class?


class Super {
    String s;
 
    public Super(){
     System.out.println("Super");
    }
}
 
public class Sub extends Super {
 
    public Sub(){
     System.out.println("Sub");
    }
 
    public static void main(String[] args){
     Sub s = new Sub();
    }
}
It prints:
Super
Sub
When inheriting from another class, super() has to be called first in the constructor. If not, the compiler will insert that call. This is why super constructor is also invoked when a Sub object is created.
This doesn't create two objects, only one Sub object. The reason to have super constructor called is that if super class could have private fields which need to be initialized by its constructor.
After compiler inserts the super constructor, the sub class constructor looks like the following:
    public Sub(){
     super();
     System.out.println("Sub");
    }

Constructor of Super and Sub



This compilation error occurs because the default super constructor is undefined. In Java, if a class does not define a constructor, compiler will insert a default no-argument constructor for the class by default. If a constructor is defined in Super class, in this case Super(String s), compiler will not insert the default no-argument constructor. This is the situation for the Super class above.
The constructors of the Sub class, either with-argument or no-argument, will call the no-argument Super constructor. Since compiler tries to insert super() to the 2 constructors in the Sub class, but the Super's default constructor is not defined, compiler reports the error message.
To fix this problem, simply 1) add a Super() constructor to the Super class like
public Super(){
    System.out.println("Super");
}
, or 2) remove the self-defined Super constructor, or 3) add super(value) to sub constructors.