标签:技术 懒汉式 个人 写文件 lang container 实现 null 多线程
序言:在写单例模式之前,我知道网上有一堆博客已经写过单例模式了,之所以再写,只是单纯想做一个记录,个人认为单例模式是最为基础且最简单的设计模式之一,不然面试怎么逢人就问,但是单例模式,据我所了解能用的应该有4种常规写法(非线程安全的可以理解为没办法在真实生产环境运用)。
单例模式是创建型设计模式的一种,意图保证一个类只有一个实例,并提供一个访问它的全局访问点
优点:
场景:

?
//最为推荐的懒汉式写法
class Singleton {
	
	private static volatile Singleton instance;
	
	private Singleton(){}
	
	//提取一个静态的公有办法,加入双重检查机制,解决线程安全问题,同时解决懒加载问题
	public static Singleton getInstance(){
		if(instance == null){
			synchronized (Singleton.class) {
				if(instance == null){
					instance = new Singleton();
				}
			}
		}
		return instance;
	}
	
}
??
// 饿汉式(静态变量)
class Singleton {
	//本类内部创建对象实例
	private static final Singleton _INSTANCE = new Singleton();
	
	// 构造器私有化
	private Singleton() {}
	
	//提供一个公有的静态方法
	public static Singleton getInstance(){
		return _INSTANCE;
	}
}
?
//内部类实现单例模式,线程安全,懒加载
class Singleton{
	
	//构造器私有化
	private Singleton(){}
	
	//写一个静态内部类,该类中有一个静态属性 Singleton
	private static class SingletonInstance {
		private static final Singleton INSTANCE = new Singleton();
	}
	
	
	//提供一个静态的公有办法,直接返回Singleton.instance
	public static synchronized Singleton getInstance(){
		return SingletonInstance.INSTANCE;
	}
	
}
?
//枚举 天然单例
enum Singleton{
	INSTANCE;
}
?
总结: 个人认为枚举最为简单,毕竟天然单例,其他写法看自己所需要的场景吧
标签:技术 懒汉式 个人 写文件 lang container 实现 null 多线程
原文地址:https://www.cnblogs.com/dwlovelife/p/13296524.html