终于可以休息了,寻一把躺椅,安置于庭院,携一壶好茶,品一番风轻云淡。。。
自由自在的呼吸,伸手即可触摸阳光的温度,此时此刻,我就是我,像一个单例。
想起『设计模式』,就像想起了很久很久以前的故事,今日重新拾起,不妨用java结合微信公众号来尝尝这经典的二十三式。
我是一个快乐的单例
说起单例的经典应用,一定要提 spring的依赖注入,可谓是行业经典。
在spring4的版本中,单例是这么出来的:
/**
* Return the (raw) singleton object registered under the given name.
* <p>Checks already instantiated singletons and also allows for an early
* reference to a currently created singleton (resolving a circular reference).
* @param beanName the name of the bean to look for
* @param allowEarlyReference whether early references should be created or not
* @return the registered singleton object, or {@code null} if none found
*/
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}既用到缓存,又有线程锁进行防护,可谓双管齐下。
Java单例模式可能是最简单也是最常用的设计模式,一个完美的单例需要做到哪些事呢?
单例六要素
- 单例
- 延迟加载
- 线程安全
- 没有性能问题
- 防止序列化产生新对象
- 防止反射攻击
而青鸟的脑子就比较简单,想不了这么复杂的事情,平时是这么用的:
package org.yi.parttern;
/**
* @ClassName: SingletonMe
* @author 青鸟
* @date 2018年2月16日 下午2:11:16
* @describe
* 我是一个快乐的单例
* 微信公众号:青鸟的记忆
* 博客:www.cnblogs.com/stobird/p/8450140.html
*
*/
public class SingletonMe {
/**
* 类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例没有绑定关系,而且只有被调用到时才会装载(装在过程是由jvm保证线程安全)
* ,从而实现了延迟加载
*/
private static class SingletonHolder {
/**
* 静态初始化器,由JVM来保证线程安全
*/
private static SingletonMe instance = new SingletonMe();
}
/**
* 私有化构造方法
*/
private SingletonMe() {
}
/**
* 这个模式的优势在于:getInstance方法并没有被同步,并且只是执行一个域的访问,因此延迟初始化并没有增加任何访问成本
*/
public static SingletonMe getInstance() {
return SingletonHolder.instance;
}
}使用内部类来维护单例的实例,当SingletonMe 被加载时,其内部类并不会被初始化,故可以确保当 SingletonMe 类被载入JVM时,不会初始化单例类。
只有 getInstance() 方法调用时,才会初始化 instance。
同时,由于实例的建立是时在类加载时完成,故天生对多线程友好,getInstance() 方法也无需使用同步关键字。
下一篇预告:站在工厂的角度看单例,喜欢的朋友也可以关注体验一下哦。。。
公众号回复 单例,试一下吧。