1. Eager Initialization Approach
package edu.xmu.designPattern.DesingPattern_Singleton;
public class Singleton
{
private static Singleton singleton = new Singleton();
private Singleton()
{
}
public static Singleton getInstance()
{
return singleton;
}
}
package edu.xmu.designPattern.DesingPattern_Singleton;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AppTest
{
@Test
public void test()
{
Singleton singleton1 = Singleton.getInstance();
Singleton singleton2 = Singleton.getInstance();
assertEquals(singleton1, singleton2);
}
}
2. Lazy Initialization Approach
package edu.xmu.designPattern.DesingPattern_Singleton;
public class Singleton
{
private static Singleton singleton = null;
private Singleton()
{
}
public static Singleton getInstance()
{
if (null == singleton)
{
singleton = new Singleton();
}
return singleton;
}
}
package edu.xmu.designPattern.DesingPattern_Singleton;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class AppTest
{
@Test
public void test()
{
Singleton singleton1 = Singleton.getInstance();
Singleton singleton2 = Singleton.getInstance();
assertEquals(singleton1, singleton2);
}
}
Comments:
1) Problems may occur when under the circumstance of multi-thread.
package edu.xmu.designPattern.DesingPattern_Singleton;
public class Singleton
{
private static Singleton singleton = null;
private Singleton()
{
}
public static Singleton getInstance()
{
if (null == singleton)
{
try
{
System.out.println("Current Thread: " + Thread.currentThread());
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
singleton = new Singleton();
}
return singleton;
}
}
package edu.xmu.designPattern.DesingPattern_Singleton;
public class AppTest
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread().start();
}
}
class MyThread extends Thread
{
private Singleton singleton = null;
@Override
public void run()
{
singleton = Singleton.getInstance();
System.out.println(singleton);
}
}
Current Thread: Thread[Thread-0,5,main]
Current Thread: Thread[Thread-1,5,main]
edu.xmu.designPattern.DesingPattern_Singleton.Singleton@1b67f74
edu.xmu.designPattern.DesingPattern_Singleton.Singleton@69b332
Comments:
1) We can find out that there has been two different instance of Singleton created.
This violates Singleton.
2) We can use getInstance() to mark that we are using Singleton.
We can use newInstance() to mark that we are using Reflection to create a new instance.
3. Enum Approach
package edu.xmu.oop.singleton;
public enum Singleton {
SINGLETON("Davy", 24);
private String name;
private int age;
Singleton(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "name: " + name + ", age: " + age;
}
}
package edu.xmu.oop.singleton;
import static org.junit.Assert.assertEquals;
import org.apache.log4j.Logger;
import org.junit.Test;
public class SingletonTest {
private static final Logger logger = Logger.getLogger(SingletonTest.class);
@Test
public void singletonTest() {
Singleton instance = Singleton.valueOf("SINGLETON");
logger.info(instance.toString());
assertEquals("name: Davy, age: 24", instance.toString());
instance = Singleton.SINGLETON;
logger.info(instance.toString());
assertEquals("name: Davy, age: 24", instance.toString());
instance.setName("Cal");
instance.setAge(22);
logger.info(instance.toString());
assertEquals("name: Cal, age: 22", instance.toString());
instance = Singleton.valueOf("SINGLETON");
logger.info(instance.toString());
assertEquals("name: Cal, age: 22", instance.toString());
}
}
Advantages:
1> Thread safe: Enum is thread safe and implementation of Singleton through Enum ensures that your singleton will have only one instance even in a multithread env.
2> Deserialization safe: When we serialize a class and deserialize it then it creates another instance of the singleton class. Basically as many times as you deserialize the singleton instance,
it will create multiple instance. In this case, the best way is to make the singleton as enum, in that way, the underlying Java implementation takes caare of all the details,
if this is not possible when we will need to override the readobject() method to return the same singleton instance.
Disadvantates:
1> Enum instance is defined at compile time, we cannot make any changes to the properties of this instance if the properties are final.
These properties are assigned with default value before we can even have access to that.
Conclusion:
A single-element enum type is the best way to implement a singleton.
Reference Links:
1) Effective Java 2nd Edition -Joshua Blosh
2) http://java.dzone.com/articles/singleton-design-pattern-%E2%80%93
3) http://stackoverflow.com/questions/18882612/is-it-possible-to-have-a-java-singleton-using-an-enum-that-uses-constructor-argu