原文地址:xeblog.cn/articles/16git
确保某一个类只有一个实例,并且自行实例化并向整个系统提供这个实例。github
UML类图设计模式
单例类的构造函数是 private
内部私有的,确保外部不能经过 new
的方式建立新对象,内部自行实例化,并对外提供一个访问该单一实例的静态的方法 Instance()
。安全
/** * 普通饿汉式 * * @author anlingyi */
public class Singleton {
/** * 类加载时进行实例化对象 */
private static final Singleton SINGLETON = new Singleton();
/** * 私有构造,防止外部new对象 */
private Singleton() {
}
/** * 经过静态方法获取对象实例 * * @return */
public static Singleton getInstance() {
return SINGLETON;
}
public void say() {
System.out.println("普通饿汉式:Hello World!");
}
}
复制代码
调用方式:多线程
Singleton singleton = Singleton.getInstance();
singleton.say();
复制代码
优势: 类加载时就进行实例化,以后的操做效率会很高。
缺点: 因为类加载时就进行实例化,若是后续不对此类进行任何操做,就会致使内存的浪费。函数
/** * 懒汉式(线程不安全) * * @author anlingyi */
public class SingletonTwo {
private static SingletonTwo instance;
/** * 私有构造,防止外部new对象 */
private SingletonTwo() {
}
/** * 经过静态方法获取对象实例 * * @return */
public static SingletonTwo getInstance() {
if(instance == null) {
instance = new SingletonTwo();
}
return instance;
}
public void say() {
System.out.println("懒汉式(线程不安全):Hello World!");
}
}
复制代码
调用方式:性能
SingletonTwo singleton = SingletonTwo.getInstance();
singleton.say();
复制代码
优势: 在第一次调用的时候才进行实例化。
缺点: 当多个线程同时进入到 if(instance == null) {...}
时,会建立多个对象。测试
/** * 同步锁懒汉式(线程安全,效率低) * * @author anlingyi */
public class SingletonThree {
private static SingletonThree instance;
/** * 私有构造,防止外部new对象 */
private SingletonThree() {
}
/** * 经过静态方法获取对象实例 * * @return */
public static synchronized SingletonThree getInstance() {
if(instance == null) {
instance = new SingletonThree();
}
return instance;
}
public void say() {
System.out.println("同步锁懒汉式(线程安全,效率低):Hello World!");
}
}
复制代码
调用方式:优化
SingletonThree singleton = SingletonThree.getInstance();
singleton.say();
复制代码
优势: 在第一次调用的时候才进行实例化,且线程安全。
缺点: 使用 synchronized
的方式对方法加锁,会影响效率。spa
/** * 双重校验锁懒汉式(线程安全,且多线程环境下能够保持高性能) * * @author anlingyi */
public class SingletonFour {
/** * volatile是为了防止指令重排序 */
private static volatile SingletonFour instance;
/** * 私有构造,防止外部new对象 */
private SingletonFour() {
}
/** * 经过静态方法获取对象实例 * * @return */
public static SingletonFour getInstance() {
if(instance == null) {
synchronized (SingletonFour.class) {
if(instance == null) {
instance = new SingletonFour();
}
}
}
return instance;
}
public void say() {
System.out.println("双重校验锁懒汉式(线程安全,且多线程环境下能够保持高性能):Hello World!");
}
}
复制代码
调用方式:
SingletonFour singleton = SingletonFour.getInstance();
singleton.say();
复制代码
优势: 在第一次调用的时候才进行实例化,且线程安全,效率较高。
缺点: 实现复杂,且 volatile
须要在JDK1.5以后的版本才能确保安全。
/** * 静态内部类懒汉式 * * @author anlingyi */
public class SingletonFive {
/** * 私有构造,防止外部new对象 */
private SingletonFive() {
}
/** * 经过静态方法获取对象实例 * * @return */
public static SingletonFive getInstance() {
return Singleton.SINGLETON;
}
public void say() {
System.out.println("静态内部类懒汉式:Hello World!");
}
/** * 静态内部类实例化对象 */
private static class Singleton {
/** * 类加载时进行实例化对象 */
private static final SingletonFive SINGLETON = new SingletonFive();
}
}
复制代码
调用方式:
SingletonFive singleton = SingletonFive.getInstance();
singleton.say();
复制代码
优势: 只有在调用 getInstance()
方法的时候,静态内部类才会被加载,从而对主类(咱们须要的类)进行实例化,即线程安全,又效率高。
缺点: 多建立一个类。
/** * 枚举类饿汉式(防止反序列化) * * @author anlingyi */
public enum SingletonSix {
INSTANCE;
public void say() {
System.out.println("枚举类饿汉式(防止反序列化):Hello World!");
}
}
复制代码
调用方式:
SingletonSix singleton = SingletonSix.INSTANCE;
singleton.say();
复制代码
优势: 实现简单,防止反序列化生成多个实例,且线程安全。
缺点: Enum
需在JDK1.5以后版本使用。