TypeScript singleton pattern All In One
singleton pattern / 单例模式
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-03-31
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
const log = console.log;
// 只能创建单个类的实例
class Human {
// 私有构造器函数,不可以通过 new 创建类的实例
constructor(name) {
this.name = name;
}
// public static createInstance() {
static createInstance(name) {
if (!Human.isExist) {
Human.isExist = true;
this.instance = new Human(name);
}
else {
// ignore
}
return this.instance;
}
}
Human.isExist = false;
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
// h1 name abc
// h2 name abc
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-03-31
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
const log = console.log;
class Human {
// 私有构造器函数,不可以通过 new 创建类的实例
constructor(name) {
this.name = name;
}
// public static createInstance() {
static createInstance(name) {
if (!Human.isExist) {
Human.isExist = true;
Human.instance = new Human(name);
}
else {
// ignore
}
return Human.instance;
}
}
Human.isExist = false;
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
// h1 name abc
// h2 name abc
demo
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-03-31
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
const log = console.log;
class Person {
// public name: string;
name: string
constructor(name: string) {
this.name = name;
}
}
// 可以创建多个不同的类的实例
const p1 = new Person('abc');
const p2 = new Person('xyz');
// singleton pattern 单例模式
// 只能创建单个类的实例
class Human {
private static isExist: boolean = false;
private static instance: Human;
// public name: string
name: string
// 私有构造器函数,不可以通过 new 创建类的实例
private constructor(name: string) {
this.name = name;
}
// public static createInstance() {
static createInstance(name: string) {
if (!Human.isExist) {
Human.isExist = true;
Human.instance = new Human(name);
} else {
// ignore
}
return Human.instance;
}
}
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
export {}
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-03-31
* @modified
*
* @description
* @augments
* @example
* @link
*
*/
const log = console.log;
class Person {
constructor(name) {
this.name = name;
}
}
// 可以创建多个不同的类的实例
const p1 = new Person('abc');
const p2 = new Person('xyz');
// singleton pattern 单例模式
// 只能创建单个类的实例
class Human {
// 私有构造器函数,不可以通过 new 创建类的实例
constructor(name) {
this.name = name;
}
// public static createInstance() {
static createInstance(name) {
if (!Human.isExist) {
Human.isExist = true;
Human.instance = new Human(name);
}
else {
// ignore
}
return Human.instance;
}
}
Human.isExist = false;
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
export {};
class Human {
// 初始化
private static isExist: boolean = false;
// 未初始化
private static instance: Human;
// public name: string
name: string
// 私有构造器函数,不可以通过 new 创建类的实例
private constructor(name: string) {
this.name = name;
}
// public static createInstance() {
static createInstance(name: string) {
if (!Human.isExist) {
Human.isExist = true;
// this.instance = new Human(name);
Human.instance = new Human(name);
} else {
// ignore
}
console.log(this.instance === Human.instance);
// return this.instance;
return Human.instance;
}
}
// const human = new Human('eric');
// Constructor of class 'Human' is private and only accessible within the class declaration.ts(2673)
const h1 = Human.createInstance('abc');
const h2 = Human.createInstance('xyz');
log('h1 name', h1.name);
log('h2 name', h2.name);
js singleton pattern
ES5 / ES6
// IIFE
var Singleton = (function () {
var instance;
// 闭包 closure
function createInstance() {
var object = new Object("I am the instance");
return object;
}
return {
// 单例模式 getInstance
getInstance: function () {
if (!instance) {
// 实例化 1 次
instance = createInstance();
}
return instance;
}
};
})();
function run() {
var instance1 = Singleton.getInstance();
var instance2 = Singleton.getInstance();
console.log("Same instance? " + (instance1 === instance2));
}
js custom new keyword
// 1. create object, allocate memory
// 2. bind this
// 3. bind prototype
// 4. return object
function CustomNew (Func, args) {
// const obj = Object.create(func);
const obj = {};
// const obj = Object.assign({});
Func.call(obj, args);
// Func.call(obj, ...args);
// Func.apply(obj, [...args]);
obj.__proto__ = Func.prototype;
return obj;
}
https://www.cnblogs.com/xgqfrms/tag/new
https://www.cnblogs.com/xgqfrms/p/15998513.html
https://www.cnblogs.com/xgqfrms/p/13717265.html
refs
https://www.typescriptlang.org/play
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 ️,侵权必究⚠️!