ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

设计模式——单例模式

2022-02-03 23:30:00  阅读:125  来源: 互联网

标签:Singleton 模式 instance static private 单例 线程 设计模式 class


单例模式

确保在整个软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)。例:数据库客户端,确保一个客户访问就只创建一个访问类。

  • 饿汉式(静态常量)

    class Singleton{
        // 构造器私有化
        private Singleton(){
            
        }
        // 本类内部创建对象实例
        private final static Singleton instance = new Singleton();
        // 提供一个共有的静态方法,返回实例对象
        public static Singleton getInstance(){
            return instance;
        }
    }
    

    在类加载时就完成了实例化,避免了线程同步问题,但如果实例未被使用则造成了内存浪费。

  • 饿汉式(静态代码块)

    class Singleton{
        // 构造器私有化
        private Singleton(){
            
        }
        // 本类内部创建对象实例
        private static Singleton;
        // 在静态代码块中,创建单例对象
        static{ 
            instance = new Singleton();
        }
        // 提供一个共有的静态方法,返回实例对象
        public static Singleton getInstance(){
            return instance;
        }
    }
    

    同上。

  • 懒汉式(线程不安全)

    class Singleton {
        private static Singleton instance;
        private Singleton(){}
        //使用该静态方法时,才去创建instance
        public static Singleton getInstance() {
            if(instance == null){
                instance = new Singleton();
            }
            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;
        private Singleton(){}
        public static Singleton getInstance() {
            if(instance == null){
                synchronized(Singleton.class){
                    if(instance == null){
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
    

    double-check多线程中常用到,进行了两次if检查,可保证线程安全。实例化代码只执行一次,后面再次访问时,判断if,直接return实例化对象,避免反复进行方法同步。线程安全、延迟加载、效率极高。

  • 静态内部类

    class Singleton{
        private static volatile Singleton instance;
        private Singleton(){}
        private static class SingletonInstance{
            private static final Singleton INSTANCE = new Singleton();
        }
        public static synchronized Singleton getInstance(){
            return SingletonInstance.INSTANCE;
        }
    }
    

    静态内部类SingletonInstance在Singleton类加载时不会被加载,当调用getInstance()时,SingletonInstance类会被加载,Singleton类才会被实例化。类的静态属性只会在第一次加载类的时候初始化,且在类加载过程中始终线程安全(JDK确保)。线程安全、利用静态内部类特点实现延迟加载,效率高。推荐使用。

  • 枚举

  • enum Singleton{
        INSTANCE;
        public void sayOK(){
            System.out.printlen("ok");
        }
    }
    

    枚举线程安全、防止反序列化重新创建新的对象,推荐使用。

标签:Singleton,模式,instance,static,private,单例,线程,设计模式,class
来源: https://blog.csdn.net/qq_41594515/article/details/122779970

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有