一:单例模式
二:设计原则
三:实例
/**
*经典单例
*/
public class Singleton {
private static Singleton uniqueInstance;//唯一实列
private Singleton() {};
public static Singleton getInstance() {
if(uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}
/**
*
*多线程中的单例
*/
public class Singleton_thread {
private volatile static Singleton_thread uniqueInstance;//唯一实列
private Singleton_thread() {};
public static Singleton_thread getInstance() {
if(uniqueInstance == null) {
synchronized (Singleton_thread.class) {
if(uniqueInstance == null) {
uniqueInstance = new Singleton_thread();
}
}
}
return uniqueInstance;
}
}