设计模式-单例模式(饿汉式)

1. 概念

  • 保证一个类只有一个实例
  • 并为该实例提供一个全局唯一的访问节点

2. 饿汉式(静态常量)

2.1 步骤

  1. 构造器私有化(防止 new)
  2. 类的内部创建对象
  3. 向外暴露一个静态的公共方法 -- getInstance()

2.2 代码示例

示例 - 饿汉式(静态常量)
java 复制代码
/**
 * @Description: 单例模式:饿汉式(静态常量)
 */
public class Singleton {

    /**
     * 构造器私有化
     */
    private Singleton() {

    }

    /**
     * 在内部创建对象实例
     */
    private final static Singleton INSTANCE = new Singleton();

    /**
     * 对外提供公有的静态方法
     */
    public static Singleton getInstance() {
        return INSTANCE;
    }
}
java 复制代码
public class SingletonTest01 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance == instance1);
        System.out.println("instance.hashCode= " + instance.hashCode());
        System.out.println("instance1.hashCode= " + instance1.hashCode());
    }
}

3. 饿汉式(静态代码块)

3.1 步骤

  1. 构造器私有化(防止 new)
  2. 类的内部创建静态成员变量
  3. 在代码块中构建成员变量对象
  4. 向外暴露一个静态的公共方法 -- getInstance()

3.2 代码示例

示例 - 饿汉式(静态代码块)
java 复制代码
/**
 * @Description: 单例模式:饿汉式(静态代码块)
 */
public class Singleton02 {

    /**
     * 构造器私有化
     */
    private Singleton02() {
    }

    /**
     * 成员变量
     */
    private static Singleton02 INSTANCE;

    static {
        // 在静态代码块中创建单例对象
        INSTANCE = new Singleton02();
    }

    /**
     * 对外提供公有的静态方法
     */
    public static Singleton02 getInstance() {
        return INSTANCE;
    }
}
java 复制代码
public class SingletonTest02 {
    public static void main(String[] args) {
        Singleton02 instance = Singleton02.getInstance();
        Singleton02 instance1 = Singleton02.getInstance();
        System.out.println(instance == instance1);
        System.out.println("instance.hashCode= " + instance.hashCode());
        System.out.println("instance1.hashCode= " + instance1.hashCode());
    }
}

4. 优缺点

  • 优点
    • 这种写法比较简单,就是在类装载的时候就完成实例化。避免了线程同步问题
  • 缺点
    • 在类装载的时候就完成实例化,没有达到Lazy Loading的效果。如果从始至终从未使用过这个实例,则会造成内存的浪费
      • 这种方式基于classloder机制避免了多线程的同步问题,不过,instance在类装载时就实例化,在单例模式中大多数都是调用getinstance方法,但是导致类装载的原因有很多种,因此不能确定有其他的方式(或者其他的静态法)导致类装载,这时候初始化instance就没有达到lazyloading的效果

5. 结论

  • 这种单例模式可用,但可能造成内存浪费
相关推荐
端庄的战斗机4 小时前
javascript 设计模式(文章很长,请自备瓜子,水果和眼药水)
开发语言·javascript·设计模式
饼干哥哥10 小时前
我开发了一个「吃掉」别人Skill来升级的饕餮.Skill,现在免费开源
人工智能·设计模式·开源
CaffeinePro11 小时前
什么是系统架构?架构师到底在做什么?
设计模式·架构
AI人工智能+电脑小能手14 小时前
【大白话说Java面试题 第156题】【06_Spring篇】第16题:Spring 用到了哪些设计模式?
java·spring·设计模式·代理模式·工厂模式
前端百草阁15 小时前
JavaScript 设计模式(23 种)
开发语言·前端·javascript·设计模式
geovindu15 小时前
java: Singleton Pattern
java·开发语言·后端·单例模式·设计模式·创建型模式
geovindu1 天前
python: Functional Options Pattern
开发语言·后端·python·设计模式·惯用法模式·函数式选项模式
Kel1 天前
Pregel 为什么会成为LangGraph编排的心脏
人工智能·设计模式·架构
会周易的程序员2 天前
microLog 后端开发指南
开发语言·c++·物联网·设计模式·日志·iot·aiot
geovindu2 天前
go: Functional Options Pattern
开发语言·后端·设计模式·golang·函数式选项模式’·惯用法模式