单例模式 Singleton Pattern

一、模式介绍

1.1、定义

确保一个类只有一个实例,并提供一个全局的访问点。单例模式有 3 个特点:

  1. 单例类只有一个实例对象
  2. 该单例对象必须由单例类自行创建
  3. 单例类对外提供一个访问该单例的全局访问点

1.2、优点

  1. 单例模式可以保证内存里只有一个实例,减少内存的开销
  2. 可以避免对资源的多重占用
  3. 单例模式设置全局访问点,可以优化和共享资源的访问

1.3、缺点

  1. 单例模式一般没有接口,扩展困难。如果要扩展,则除了修改原来代码,没有第二种途径,违背开闭原则
  2. 在并发测试中,单例模式不利于代码调试。
  3. 单例模式的功能代码通常写在一个类中,如果功能设计不合理,则很容易违背单一职责原则

二、实现

在这里插入图片描述

2.1、懒汉式单例

package com.erlang.singleton;

/**
 * @description: 懒汉式单例
 * @author: erlang
 * @since: 2022-02-10 22:46
 */
public class LazySingleton {
    /**
     * 保证 instance 在所有线程中同步
     */
    private static volatile LazySingleton instance = null;

    private LazySingleton() {
    }    //private 避免类在外部被实例化

    public static synchronized LazySingleton getInstance() {
        //getInstance 方法前加同步
        if (instance == null) {
            instance = new LazySingleton();
        }
        return instance;
    }
}

2.2、饿汉式单例

package com.erlang.singleton;

/**
 * @description: 饿汉式单例
 * @author: erlang
 * @since: 2022-02-10 22:47
 */
public class HungrySingleton {
    
    private static final HungrySingleton instance = new HungrySingleton();

    private HungrySingleton() {
    }

    public static HungrySingleton getInstance() {
        return instance;
    }
}

2.3、DCL 实现

package com.erlang.singleton;

/**
 * @description: DCL 双重检查加锁
 * @author: erlang
 * @since: 2022-02-10 22:48
 */
public class DCLSingleton {
    /**
     * 使用 volatile 禁止指令重排序
     */
    public static volatile DCLSingleton singleton;     
	
	private DCLSingleton() {}
    public static DCLSingleton getSingleton() {     
        if (singleton == null) {                    
            synchronized (DCLSingleton.class) {     
                if (singleton == null) {            
                    singleton = new DCLSingleton(); 
                }                                   
            }                                       
        }                                           
        return singleton;                           
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值