Java 单例模式几种实现方式
程序员文章站
2022-03-07 18:11:06
...
单例模式(singleton pattern )是一种在开发中经常用到的设计模式。
模式定义
确保一个类仅有一个实例,并且提供一个访问它的全局访问点。
实现
1、单例模式-饿汉式
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton(){
System.out.println("new Singleton instance");
}
public static Singleton getInstance(){
return INSTANCE;
}
}
2、单例模式-懒汉式(非线程安全)
public class Singleton {
private static Singleton instance;
private Singleton() {
System.out.println("new Singleton instance");
}
//非线程安全
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
3、单例模式-懒汉式(线程安全)
public class Singleton {
private volatile static Singleton instance;
private Singleton(){
System.out.println("new Singleton instance");
}
//double-check
public static Singleton getInstance(){
if(instance==null){
synchronized (Singleton.class){
if(instance==null){
instance = new Singleton();
}
}
}
return instance;
}
}
4、单例模式-懒汉式
public class Singleton {
private Singleton(){
System.out.println("new Singleton instance");
}
public static Singleton getInstance(){
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder{
public static final Singleton INSTANCE = new Singleton();
}
}
5、单例模式-枚举
public enum Singleton {
INSTANCE("singleton");
private String value;
Singleton(String value){
this.value = value;
}
public void doSomeAction(){
System.out.println("doSomeAction");
}
}
源代码
https://github.com/TiFG/design-patterns/tree/master/singleton