• 26、Java 简单实现单例设计模式(饿汉式和懒汉式)


    一、概念

    ✏️【Singleton Pattern】如果一个类被设计成单例设计模式,则在整个应用程序运行过程中,该类只能存在一个实例。

    二、饿汉式

    思考:如何实现在整个应用程序运行过程中,某个类只能存在一个实例:

    public class HungrySingleton {
        // static: 保证 HungrySingleton 的实例只占用一份内存
        private static HungrySingleton instance = new HungrySingleton();
    
        // 1.构造方法私有化
        private HungrySingleton() {
    
        }
    
        // 2.提供公共的静态方法返回该类的唯一实例
        public static HungrySingleton getInstance() {
            return instance;
        } 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    📖 ① 构造方法私有化(不让别人有创建该类实例的权利
    📖 ② 在类的内部创建该类的唯一对象(该类实例只能有一个,且在该类中由自己创建
    📖 ③ 向外暴露一个公共的静态方法(以返回该类的唯一对象)(通过public static 静态方法向外界返回唯一的该类的对象

    ❓ 为什么是饿汉式呢?
    📗 HungrySingleton 的唯一对象在类中被直接创建(new),并且被定义为类变量
    📗 当 HungrySingleton 类被加载的时候,HungrySingleton 的唯一对象就会在堆空间存在
    📗 假如 HungrySingleton 中存在其他类变量或类方法。我只是想使用一下里面的哪些类变量和类方法,并不想获得它的唯一实例。但是,只要我使用了 HungrySingleton 里面的其他静态方法或类变量就会导致 HungrySingleton 的唯一对象在堆空间产生(因为 HungrySingleton 的唯一对象实例也是类变量)
    📗 很饥渴,饿汉式
    📗 可能导致内存浪费

    Java 中的 Runtime 类是饿汉式单例设计模式
    在这里插入图片描述


    public class HungrySingleton {
        public static int one = 1;
    
        private static HungrySingleton instance = new HungrySingleton();
    
        private HungrySingleton() {
            System.out.println("private HungrySingleton()");
        }
    
        public static HungrySingleton getInstance() {
            return instance;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    public class Whatever {
        public static void main(String[] args) {
            // private HungrySingleton()
            // 1
            System.out.println(HungrySingleton.one);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    📗 在上面的代码中,博主仅仅是使用一下 HungrySingleton 类中的公共静态变量 one,博主并不想获得该类的唯一实例
    📗 但是 HungrySingleton 的构造方法依然被调用了
    📗 类被加载的时候,类中的类变量会被初始化
    📗 private static HungrySingleton instance 类变量的初始化会创建 HungrySingleton 的唯一对象

    三、懒汉式

    public class LazySingleton {
        private static LazySingleton instance = null;
    
        private LazySingleton() {
    
        }
    
        public static LazySingleton getInstance() {
            if (instance == null) {
                instance = new LazySingleton();
            }
    
            return instance;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    public class Whatever {
        public static void main(String[] args) {
            LazySingleton instance1 = LazySingleton.getInstance();
            LazySingleton instance2 = LazySingleton.getInstance();
            LazySingleton instance3 = LazySingleton.getInstance();
            System.out.println(instance1);
            System.out.println(instance2);
            System.out.println(instance3);
            
            /*
                com.gq.LazySingleton@1540e19d
                com.gq.LazySingleton@1540e19d
                com.gq.LazySingleton@1540e19d
             */
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    📗 懒汉式:类加载不会导致该类对象的创建
    📗 只有调用 getInstance 方法的时候才有可能创建该类的对象【懒创建】
    📗 可能有线程安全问题

    如有错误,请不吝赐教!

  • 相关阅读:
    【Langchain Agent研究】SalesGPT项目介绍(四)
    [附源码]SSM计算机毕业设计餐厅卫生安全系统JAVA
    模型压缩- 剪枝/量化/蒸馏/AutoML
    Redis之Lua脚本讲解
    A-level 物理实验题目
    50从零开始用Rust编写nginx,原来TLS证书还可以这么申请
    【SwiftUI项目】0009、SwiftUI项目-费用跟踪-记账App项项目-第1/3部分 - 本地数据
    Word怎么删除空白页?6个方法随便用!
    Etcd教程 — 第五章 Etcd之etcdctl的使用
    STAAD.Pro CONNECT Edition
  • 原文地址:https://blog.csdn.net/m0_54189068/article/details/126922457