单例模式实现方式比较

单例模式实现方式线程安全延迟加载反射安全序列化安全
饿汉模式×××
懒汉模式(非空判断)×××
懒汉模式(方法加锁)××
懒汉模式(双重检查锁)××
静态内部类××
枚举
/**
 * 饿汉
 * @author zhouxiaobing
 *
 */
public class Singleton implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Singleton() {
	}
	private static Singleton SINGLETON = new Singleton();
	public static Singleton getInstance() {
		return SINGLETON;
	}
}

/**
 * 懒汉-非线程安全
 * @author zhouxiaobing
 *
 */
public class Singleton implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Singleton() {
	}
	private static Singleton SINGLETON;
	public static Singleton getInstance() {
		if (SINGLETON == null) {
			SINGLETON = new Singleton();
		}
		return SINGLETON;
	}
}
/**
 * 懒汉-线程安全
 * @author zhouxiaobing
 *
 */
public class Singleton implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Singleton() {
	}
	private static Singleton SINGLETON;
	public static synchronized Singleton getInstance() {
		if (SINGLETON == null) {
			SINGLETON = new Singleton();
		}
		return SINGLETON;
	}
}
/**
 * 懒汉-双重校验锁
 * @author zhouxiaobing
 *
 */
public class Singleton implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Singleton() {
	}
	private volatile static Singleton SINGLETON;// 内存可见性 
	public static  Singleton getInstance() {
		if (SINGLETON == null) {
			synchronized (Singleton.class) {//synchronized 原子性 有序性 内存可见性
				if (SINGLETON == null) {
					SINGLETON = new Singleton();
				}
			}
		}
		return SINGLETON;
	}
}
/**
 * 静态内部类
 * @author zhouxiaobing
 *
 */
public class Singleton implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Singleton() {
	}
	private static class Inner{
		private static Singleton SINGLETON = new Singleton();
	}
	
	public static  Singleton getInstance() {
		return Inner.SINGLETON;
	}
}
/**
 * 枚举
 * @author zhouxiaobing
 *
 */

public enum Singleton implements Serializable{
	SINGLETON;
	// 可以防止使用 反射 来生成新对象
	// 可以防止使用 序列化 方式生成新对象
}

public class SingletonTest {
	public static void main(String[] args) throws Exception {
		Singleton instance = Singleton.getInstance();
		System.out.println(instance);
		// 拿到所有的构造函数,包括非public的
        Constructor<Singleton> constructor = Singleton.class.getDeclaredConstructor();
        constructor.setAccessible(true);
        // 使用空构造函数new一个实例。即使它是private的~~~
        Singleton sReflection = constructor.newInstance();
        System.out.println(sReflection);
        System.out.println(instance == sReflection);
       
	}
}

public class SingletonSerializableTest {
    // Singleton类需要实现Serializable序列化接口
	public static void main(String[] args) throws Exception {
		Singleton s = Singleton.getInstance();

        byte[] serialize = SerializationUtils.serialize(s);
        Object deserialize = SerializationUtils.deserialize(serialize);

        System.out.println(s);
        System.out.println(deserialize);
        System.out.println(s == deserialize);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值