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

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. 结论

  • 这种单例模式可用,但可能造成内存浪费
相关推荐
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
烛阴2 天前
【TS 设计模式完全指南】懒加载、缓存与权限控制:代理模式在 TypeScript 中的三大妙用
javascript·设计模式·typescript
李广坤2 天前
工厂模式
设计模式
幂简集成explinks2 天前
e签宝签署API更新实战:新增 signType 与 FDA 合规参数配置
后端·设计模式·开源
大飞pkz3 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
努力也学不会java3 天前
【设计模式】抽象工厂模式
java·设计模式·oracle·抽象工厂模式
青草地溪水旁3 天前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁3 天前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
Magnetic_h3 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa