二十三种设计模式之单例模式

38 篇文章 0 订阅
4 篇文章 0 订阅
  • 单例模式:所谓类的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)。

  • 比如Hibernate的SessionFactory,它充当数据存储源的代理,并负责创建Session对象。SessionFactory并不是轻量级的,一般情况下,一个项目通常只需要一个SessionFactory就够,这是就会使用到单例模式。

  • 单例模式有八种方式:

    • 饿汉式(静态常量)
    // 静态常量式饿汉模式
    class Singleton{
        // 1、构造器私有化,外部能new
        private Singleton(){}
        // 2、本类内部构建对象实例
        private final static Singleton instance = new Singleton();
        // 3、提供一个公共的静态方法,返回实例对象。
        public static Singleton getInstance() {
            return instance;
        }
    }
    public class SingletonTest {
        public static void main(String[] args) {
            Singleton instance = Singleton.getInstance();
            Singleton instance1 = Singleton.getInstance();
            System.out.println(instance == instance1);  // 结果:true
        }
    }
    
    • 饿汉式(静态代码块)
    public class SingletonStaticCode{
        // 1、构造器私有化,外部能new
        private SingletonStaticCode(){
        }
        // 2、本类内部构建对象实例
        private final static SingletonStaticCode instance;
        // 在静态代码块中,创建单例对象
        static{
            instance = new SingletonStaticCode();
        }
        // 3、提供一个公共的静态方法,返回实例对象。
        public static SingletonStaticCode getInstance() {
            return instance;
        }
    }
    
    • 懒汉式(线程不安全)
    public class SingletonLazy01 {
        private static SingletonLazy01 instance;
        
        public SingletonLazy01() {
        }
        //提供一个静态的公有方法,当使用到该方法时,才去创建instance
        // 即懒汉式
        public static SingletonLazy01 getInstance() {
            if (instance == null){
                instance =new SingletonLazy01();
            }
            return instance;
        }
    }
    
    • 懒汉式(线程安全,同步方法)
    public class SingletonLazy01 {
        private static SingletonLazy01 instance;
        public SingletonLazy01() {
        }
        //提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
        public static synchronized SingletonLazy01 getInstance() {
            if (instance == null){
                instance =new SingletonLazy01();
            }
            return instance;
        }
    }
    
    • 双重检查
    public class SingletonLazy {
        private static volatile SingletonLazy instance;
        public SingletonLazy() {}
        //提供一个静态的公有方法,加入双重检查代码,解决线程安全问题,同时解决懒加载问题
        public static SingletonLazy getInstance() {
            if (instance == null){
                synchronized(SingletonLazy.class){
                    if (instance == null){
                        instance =new SingletonLazy();
                    }
                }
            }
            return instance;
        }
    }
    
    • 静态内部类
    public class SingletonLazy {
        private SingletonLazy() {}
        private static class Singleton{
            private static final SingletonLazy instance = new SingletonLazy();
        }
        public static SingletonLazy getInstance(){
            return Singleton.instance;
        }
    }
    
    • 枚举实现单例
    enum Singleton {
        INSTANCE;
        public void sayOK(){
            System.out.println("ok~");
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脑袋不够用的小渣渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值