单例模式主要集中在三点:
1.私有构造方法和instance防止被调用
2.考虑线程问题,需要在第一次初始化时候
3.public方法供我们调用
synchronized 进行限制
<pre name="code" class="java">public static Singleton getInstance() {
if (instance == null) {
synchronized (instance) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}