设计模式——单例模式(Singleton Pattern)+ Spring相关源码

一、单例模式定义

类型: 创建型模式
介绍: 确保一个类只有一个实例,并提供了一个全局访问点来访问该实例。
目的: 避免一个全局使用的类频繁地创建与销毁。
注意:
1、单例类只能有一个实例。
2、单例类必须自己创建自己的唯一实例。
3、单例类必须给所有其他对象提供这一实例。

二、例子

单例模式在JAVA有多种实现方式。

根据不同实现还分为懒汉式和饿汉式。

  • 饿汉式:不管有没有使用都会创建对象。
  • 懒汉式:第一次使用时才创建对象。

2.1 双检锁/双重校验锁(DCL,即 double-checked locking)

public class Singleton {  
	private Singleton (){}  
	
    private volatile static Singleton singleton;  
    
    public static Singleton getSingleton() {  
	    if (singleton == null) {  
	        synchronized (Singleton.class) {  
	            if (singleton == null) {  
	                singleton = new Singleton();  
	            }  
	        }  
	    }  
	    return singleton;  
    }  
}

2.2 基于枚举的单例模式

public enum SingleObject {
	INSTANCE
}

2.3 JDK源码——Runtime

基于静态实例实现的饿汉式单例模式

public class Runtime {
    private static final Runtime currentRuntime = new Runtime();

	private Runtime() {}
	
    public static Runtime getRuntime() {
        return currentRuntime;
    }
}

2.4 Spring源码——DefaultSingletonBeanRegistry

Spring的DefaultSingletonBeanRegistry单例模式是采用了双检锁实现,但并非23种设计模式的单例模式。
如果只使用Spring容器创建单例对象确实可以保证对象唯一,但是这个单例是可以被破坏,比如我们手动new一个…

public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
	private final Map<String, Object> singletonObjects = new ConcurrentHashMap(256);
	private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap(16);

	@Nullable
    protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null && this.isSingletonCurrentlyInCreation(beanName)) {
            singletonObject = this.earlySingletonObjects.get(beanName);
            if (singletonObject == null && allowEarlyReference) {
                synchronized(this.singletonObjects) {
                    singletonObject = this.singletonObjects.get(beanName);
                    if (singletonObject == null) {
                        singletonObject = this.earlySingletonObjects.get(beanName);
                        if (singletonObject == null) {
                            ObjectFactory<?> singletonFactory = (ObjectFactory)this.singletonFactories.get(beanName);
                            if (singletonFactory != null) {
                                singletonObject = singletonFactory.getObject();
                                this.earlySingletonObjects.put(beanName, singletonObject);
                                this.singletonFactories.remove(beanName);
                            }
                        }
                    }
                }
            }
        }

        return singletonObject;
    }
}

三、其他设计模式

创建型模式
结构型模式

行为型模式

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码鹿的笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值