package singleton_pattern;
public class Singleton {
private static Singleton instance;
private Singleton() {}//Make sure it cann't be invoked.
public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
/*You should know these:
*Lazy Singleton,Eager Singleton and Double Locked.
*More details please see:
*https://blog.youkuaiyun.com/GZHarryAnonymous/article/details/81567214
*/
package singleton_pattern;
public class Main {
public static void main(String args[]) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if(s1 == s2) {
System.out.println("s1 is same with s2.");
}else {
System.out.println("s1 isn't same with s2.");
}
}
}
See more source code:[GZHarryAnonymous](https://github.com/GZHarryAnonymous/Design_Pattern)