/**
- 一般情况下,不建议使用第 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;
}
}