设计模式之单例模型
1.什么是单例模型
2.懒汉模式
public class Singleton {
private static Singleton instance = null;
private final static ReentrantLock lock = new ReentrantLock();
private Singleton() {
}
public static Singleton getIntance() {
lock.lock();
try {
if (instance == null) {
instance = new Singleton();
}
} finally {
lock.unlock();
}
return instance;
}
}
3.饿汉模式
public class Singleton2 {
private final static Singleton2 instance
= new Singleton2();
private Singleton2() {}
public static Singleton2 getIntance() {
return instance;
}
}