Created
March 29, 2018 15:56
-
-
Save kenanhancer/37e560633d75d6322d353488b9b46184 to your computer and use it in GitHub Desktop.
SingletonPatternExample1 created by kenanhancer - https://repl.it/@kenanhancer/SingletonPatternExample1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rclass Main { | |
public static void main(String[] args) { | |
SingletonV1 singletonv1 = SingletonV1.getInstance(); | |
singletonv1.doSomething(); | |
SingletonV2 singletonV2 = SingletonV2.getInstance(); | |
singletonV2.doSomething(); | |
SingletonV3 singletonV3 = SingletonV3.getInstance(); | |
singletonV3.doSomething(); | |
SingletonV4 singletonV4 = SingletonV4.getInstance(); | |
singletonV4.doSomething(); | |
SingletonV5 singletonV5 = SingletonV5.getInstance(); | |
singletonV5.doSomething(); | |
SingletonV6 singletonV6 = SingletonV6.getInstance(); | |
singletonV6.doSomething(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SingletonV1{ | |
private SingletonV1(){} | |
private static class SingletonHolder{ | |
private static final SingletonV1 INSTANCE = new SingletonV1(); | |
} | |
public static SingletonV1 getInstance(){ | |
return SingletonHolder.INSTANCE; | |
} | |
public void doSomething(){ | |
System.out.println("TEST"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class SingletonV2{ | |
private static volatile SingletonV2 _instance; | |
private SingletonV2(){} | |
public static SingletonV2 getInstance(){ | |
if(_instance == null){ | |
synchronized(SingletonV2.class){ | |
if(_instance == null){ | |
_instance = new SingletonV2(); | |
} | |
} | |
} | |
return _instance; | |
} | |
public void doSomething(){ | |
System.out.println("TEST"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Classic implementation of Singleton Design Pattern | |
public class SingletonV3{ | |
private static SingletonV3 _instance; | |
//private constructor to force use of getInstance() to create a single instance. | |
private SingletonV3(){} | |
public static SingletonV3 getInstance(){ | |
if(_instance == null){ | |
_instance = new SingletonV3(); | |
} | |
return _instance; | |
} | |
public void doSomething(){ | |
System.out.println("TEST"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Thread synchronized implementation of Singleton Design Pattern | |
public class SingletonV4{ | |
private static SingletonV4 _instance; | |
private SingletonV4(){} | |
//Only one thread can execute this at a time. | |
public static synchronized SingletonV4 getInstance(){ | |
if(_instance == null){ | |
_instance = new SingletonV4(); | |
} | |
return _instance; | |
} | |
public void doSomething(){ | |
System.out.println("TEST"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Eager instantiation implementation of Singleton Design Pattern | |
public class SingletonV5{ | |
private final static SingletonV5 _instance = new SingletonV5(); | |
private SingletonV5(){} | |
public static SingletonV5 getInstance(){ | |
return _instance; | |
} | |
public void doSomething(){ | |
System.out.println("TEST"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Double checked locking based implementation of Singleton Desgin Pattern | |
public final class SingletonV6{ | |
private static SingletonV6 _instance; | |
private SingletonV6(){} | |
public static SingletonV6 getInstance(){ | |
if(_instance == null){ | |
synchronized(SingletonV6.class){ | |
if(_instance == null){ | |
_instance = new SingletonV6(); | |
} | |
} | |
} | |
return _instance; | |
} | |
public void doSomething(){ | |
System.out.println("TEST"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment