单例设计模式的八种方式

27 篇文章 0 订阅
2 篇文章 0 订阅
//饿汉式(静态常量)
class Singleton{
    private Singleton{}
    private final static Singleton instance = new Singleton();
    public static Singleton getInstance(){
        return instance;
    }
}
//优点:类加载的时候就完成实例化,避免了线程同步的问题
//不足:没有懒加载,如果实例没有用到,会造成内存浪费
//饿汉式(静态代码块)
class Singleton{
    private Singleton{}
    private static Singleton instance
    static{
        instance = new Singleton();
    } 
    public static Singleton getInstance(){
        return instance;
    }
}
//优缺点都同第一种模式
//懒汉式(线程不安全)
class Singleton{
    private static Singleton instance;
    private Singleton{
        
    }
    public static Singleton getInstance(){
        if(instance == null){//这里会出现线程不安全的问题
            instance = new instance();
        }
        return instance;
    }
}
//懒汉式(线程安全,同步方法)
class Singleton{
    private static Singleton instance;
    private Singleton(){
        
    }
    public static synchronized Singleton getInstance(){
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}
//不足,每次获取是咧都要进行同步,效率太低
//懒汉式(线程不安全,同步代码块)
class Singleton{
    private static Singleton instance;
    private Singleton(){
        
    }
    public static  Singleton getInstance(){
        if(instance == null){//这里会出现并发问题
            synchronized(Singleton.class){
                instance = new Singleton();
           }     
        }
        return instance;
    }
}
//双重检查
class Singleton{
    private static volatile Singleton instance;//必须声明为volatile,防止指令重排序
    private Singleton(){
        
    }
    public static  Singleton getInstance(){
        if(instance == null){
            synchronized(Singleton.class){
                if(instance == null){
                    instance = new Singleton();
                }
           }     
        }
        return instance;
    }
}
//优点:延迟加载,效率高
//静态内部类
class Singleton{
    private Singleton{       
    }
    private static class SingletonInstance{
        public static final Singleton instance = new Singleton();
    }
    public static Singleton getInstance(){
        return SingletonInstance.instance;
    }
}
//避免线程不安全,利用静态内部类特定实现延迟加载,效率高
//枚举
public static class SingletonObject {
    private SingletonObject(){
    }
    /**
     * 枚举类型是线程安全的,并且只会装载一次
     */
    private enum Singleton{
        INSTANCE;
        private final SingletonObject instance;
        private Singleton(){
            instance = new SingletonObject();
        }
        private SingletonObject getInstance(){
            return instance;
        }
    }
   public static SingletonObject getInstance(){
         return Singleton.INSTANCE.getInstance();
   }
}
//优点:不仅能避免多线程问题,还能防止反序列化重新创建新的对象
//枚举中我们明确了构造方法限制为私有,在我们访问枚举实例时会执行构造方法,
//同时每个枚举实例都是static final类型的(可反编译验证),
//也就表明只能被实例化一次。在调用构造方法时,我们的单例被实例化。
//也就是说,因为enum中的实例被保证只会被实例化一次,所以我们的INSTANCE也被保证实例化一次
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值