单例模式

定义

单例模式(Singleton Pattern)是java最简单的设计模式之一,用于保证某个类在运行期间只有一个实例对外提供服务,我们称这种类型的类为单例类。

设计原则

  1. 保证一个类只有一个实例;

  2. 为实例提供一个全局访问点。

实现方式

饿汉式

代码示例

java 复制代码
package com.sumlv.javase.note;

/**
 * 单例示例
 *
 * @Auther: yuzhuo.song
 * @Date: 2025-03-14
 */
public class Singleton {

    private static Singleton instance = new Singleton();

    public static Singleton getInstance() {
        return instance;
    }

    /**
     * 用于避免序列化对单例的破坏
     * 如果单例类没有实现序列化接口则不需要该方法
     * 
     * @return 实例对象
     */
    private Object readResolve() {
        return instance;
    }

    /**
     * 用于避免反射对单例的破坏
     */
    private Singleton() {
        if (instance != null) {
            throw new RuntimeException("illegal access");
        }
    }

}

补充说明

  1. 线程安全;

  2. 不支持延时加载;

  3. 获取实例对象速度较快;

  4. 如果实例对象较大且一直未使用会造成内存浪费。

懒汉式

代码示例

java 复制代码
package com.sumlv.javase.note;

/**
 * 单例示例
 *
 * @Auther: yuzhuo.song
 * @Date: 2025-03-14
 */
public class Singleton {

    private static Singleton instance;

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }

        return instance;
    }

    /**
     * 用于避免序列化对单例的破坏
     * 如果单例类没有实现序列化接口则不需要该方法
     * 
     * @return 实例对象
     */
    private Object readResolve() {
        return instance;
    }

    /**
     * 用于避免反射对单例的破坏
     */
    private Singleton() {
        if (instance != null) {
            throw new RuntimeException("illegal access");
        }
    }

}

补充说明

  1. 支持延迟加载;

  2. 保证多线程模式下实例的唯一性;

  3. 并发度低,在频繁调用时会产生性能瓶颈。

双重检查锁

代码示例

java 复制代码
package com.sumlv.javase.note;

/**
 * 单例示例
 *
 * @Auther: yuzhuo.song
 * @Date: 2025-03-14
 */
public class Singleton {

    private static volatile Singleton instance;

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }

        return instance;
    }

    /**
     * 用于避免序列化对单例的破坏
     * 如果单例类没有实现序列化接口则不需要该方法
     * 
     * @return 实例对象
     */
    private Object readResolve() {
        return instance;
    }

    /**
     * 用于避免反射对单例的破坏
     */
    private Singleton() {
        if (instance != null) {
            throw new RuntimeException("illegal access");
        }
    }

}

补充说明

  1. 支持延迟加载;

  2. 并发度高,在频繁调用时不会产生性能瓶颈。

静态内部类

代码示例

java 复制代码
package com.sumlv.javase.note;

/**
 * 单例示例
 *
 * @Auther: yuzhuo.song
 * @Date: 2025-03-14
 */
public class Singleton {

    public static Singleton getInstance() {
        return InnerClass.instance;
    }

    /**
     * 用于避免序列化对单例的破坏
     * 如果单例类没有实现序列化接口则不需要该方法
     * 
     * @return 实例对象
     */
    private Object readResolve() {
        return InnerClass.instance;
    }

    /**
     * 用于避免反射对单例的破坏
     */
    private Singleton() {
        if (InnerClass.instance != null) {
            throw new RuntimeException("illegal access");
        }
    }

    private static class InnerClass {

        private static Singleton instance = new Singleton();

    }

}

补充说明

  1. 支持延迟加载;

  2. 并发度高,在频繁调用时不会产生性能瓶颈。

  3. 实现方式相较于双重检查锁更为简洁。

枚举

代码示例

java 复制代码
package com.sumlv.javase.note;

/**
 * 单例示例
 *
 * @Auther: yuzhuo.song
 * @Date: 2025-03-14
 */
public enum Singleton {

    INSTANCE;

    public static Singleton getInstance() {
        return INSTANCE;
    }

}

补充说明

  1. 最简单的单例实现方式;

  2. 线程安全;

  3. 无需关心反射和序列化对单例造成破坏;

相关推荐
YA33321 小时前
java设计模式一、单例模式
java·单例模式·设计模式
枫景Maple1 天前
Unity中多线程与高并发下的单例模式
unity·单例模式·游戏引擎
jingfeng5145 天前
线程池及线程池单例模式
linux·开发语言·单例模式
Brookty6 天前
深入解析Java并发编程与单例模式
java·开发语言·学习·单例模式·java-ee
Meteors.7 天前
23种设计模式——单例模式(Singleton)详解
单例模式·设计模式
东北南西11 天前
设计模式-单例模式
前端·javascript·单例模式·设计模式
Ni-Guvara11 天前
两种单例模式
单例模式
Leo来编程13 天前
设计模式1-单例模式
单例模式·设计模式
万象.13 天前
IO流与单例模式
单例模式