多种单例模式

/**

  • 一般情况下,不建议使用第 2 种和第 3 种懒汉式单例,建议使用第 1 种饿汉式单例,
  • 如果项目中明确要使用延时加载那么使用第 5 种静态内存类的单例,
  • 如果有序列化反序列化操作可以使用第 6 种单例模式,
  • 如果是其它需求可以使用第 4 种 DCL 单例。
    */
/**
 * 单例类基类 (饿汉式) 
 * 
 * @export
 * @abstract
 * @class SingletonBase
 */
export abstract class HungrySingletonBase {

	// 1、构造方法私有化
    private constructor() {
		
    }
	
	// 2、成员变量静态化  饿汉式直接在类加载的时候就初始化实例
	private static _instance = new (<any> this)();

	// 3、实例公有方法静态化
    public static getInstance<T>(): T {
		let instance = (<any> this)._instance;
        return instance;
    }
	
}
/**
 * 单例类基类 (懒汉式)[线程不安全]
 * 
 * @export
 * @abstract
 * @class SingletonBase
 */
export abstract class LazySingletonBase {

	// 1、构造方法私有化
    private constructor() {
        
    }
	
	// 2、成员变量静态化  懒汉式不直接初始化实例
	private static _instance = null;

	// 3、实例公有方法静态化
    public static getInstance<T>(): T {
        let instance = (<any>this)._instance;
        
        if (!instance) {
			(<any>this)._instance = new (<any>this)();
            instance = (<any>this)._instance;
        }

        return instance;
    }
	
}
/**
 * 单例类基类 (懒汉式[DCL(双重检查锁式)])[线程安全(JVM出错)]
 * 
 * @export
 * @abstract
 * @class SingletonBase
 */
export abstract class LazySingletonBase {

	// 1、构造方法私有化
    private constructor() {
        
    }
	
	// 2、成员变量静态化  懒汉式不直接初始化实例
	private static _instance = null;

	// 3、实例公有方法静态化
    public static getInstance<T>(): T {
        let instance = (<any>this)._instance;
		
        if (!instance) {
			synchronized ( (<any>this).class ) {
				if (!instance) {
					(<any>this)._instance = new (<any>this)();
					instance = (<any>this)._instance;
				}
			}
        }

        return instance;
    }
	
}
/**
 * 单例类基类 (懒汉式[DCL(双重检查锁式)])[反射攻击不安全)]
 * 
 * @export
 * @abstract
 * @class SingletonBase
 */
export abstract class LazySingletonBase {

	// 1、构造方法私有化
    private constructor() {
        
    }
	
	// 2、成员变量静态化  懒汉式不直接初始化实例
	private volatile static _instance = null;

	// 3、实例公有方法静态化
    public static getInstance<T>(): T {
        let instance = (<any>this)._instance;
		
        if (!instance) {
			synchronized ( (<any>this).class ) {
				if (!instance) {
					(<any>this)._instance = new (<any>this)();
					instance = (<any>this)._instance;
				}
			}
        }

        return instance;
    }
	
}

/**
 * 单例类基类 (静态内部类单例模式])[安全)]
 * 
 * @export
 * @abstract
 * @class SingletonBase
 */
export abstract class LazySingletonBase {

	// 1、构造方法私有化
    private constructor() {
        
    }
	
	// 2、成员变量静态化
	static class SingleTonHolder{
		private static final (<any>this)._instance = new (<any>this)();
    }

	// 3、实例公有方法静态化
    public static getInstance<T>(): T {
        let instance = (<any>this).SingleTonHolder._instance;
        return instance;
    }
	
}
/**
 * 单例类基类 (枚举)[安全)]
 * 
 * @export
 * @abstract
 * @class SingletonBase
 */
export abstract class LazySingletonBase {

	// 1、构造方法私有化
    private constructor() {
        
    }
	
	// 2、成员变量静态化
	static class SingleTonHolder{
		private static final (<any>this)._instance = new (<any>this)();
    }

	// 3、实例公有方法静态化
    public static getInstance<T>(): T {
        let instance = (<any>this).SingleTonHolder._instance;
        return instance;
    }
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值