设计模式——单例模式

2020-11-24  本文已影响0人  颓废骚年

1、懒汉式(线程安全)

public class Singleton {  
    private static Singleton instance;  
    private Singleton (){}  
    public static synchronized Singleton getInstance() {  
    if (instance == null) {  
        instance = new Singleton();  
    }  
    return instance;  
    }  
}
上一篇 下一篇

猜你喜欢

热点阅读