创建型(单例模式)
程序员文章站
2022-07-13 23:44:22
...
package com.lwf.create.singleton;
public class Singleton {
private Singleton(){}
private static Singleton st = new Singleton();
public static Singleton getInstance(){
return st;
}
public static void main(String[] args) {
}
}
class LazySingleton{
private LazySingleton(){}
private static LazySingleton st = null;
public static synchronized LazySingleton getInstance(){
if(st == null)
st = new LazySingleton();
return st;
}
}
class Test{
public static void main(String[] args) {
Singleton st = Singleton.getInstance();
LazySingleton lzSt = LazySingleton.getInstance();
}
}