TypeScript singleton pattern All In One
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 {};
https://www.typescriptlang.org/play?#code/EQVwzgpgBGAuBOBLAxrYBuAUJg9AKj0ykOKgAEBDEWACwHt4oAPAcwEcAzeAWzCL3IAbFBAB2kKAFkAkgBV+5ZHQAOATyQsasZuy68FZZPAgVYEACZQATAAYrVgLQ2AzA+cBGA9zrnEHRBb8BuYQYEaIyrCIdKIGVCzcYrB8pGQQTBTcyoIQBsKiANZBxDjYSuLagnQsUAC8UOVgdDkAdFUsWJjIghRgYFAAChDwTaJQAN5EUDg4UMogAEbCyFCimRAAXDAIiKIdU2uJW3BIe1ONCCCoDAAUh5vbpywAlBNTUFC0iGAt93Wr6ywHwAvphQbhZoB75UAp3KAbCVAF96gCxNQBUcoBYOUAMCqAELdAN4+mMAedqAaPkujE4HN3P9RBAAO6DYajG4AcgoC2QDOeWAucysFOptJGMUZTFUAC82Z0ITBdiwcrAYnNTGZ4GNAKrKBMAhFaAeH0JYAr5UAvwEI5XI3GEso9PpQAASIG4FDGkw+yiQADdTNA4KYUFBvgBRJjfWBbBZ0ZomMb1DgUQSQIFzZ2u7Yela7d2iZAPK020QxmZzRbLAFHR5Sg7rY47M4fHOAQc9AJDmgBDzQAECYALNUAv4qAB1NADD-qJhgCwEwDj8ateQjjUSHXGzA0SZdrvA7qWi3tXvaPp8aN9fusKYCpqDK7N5ktPe6oisjCYzNIKra0zcl1Nj56z67Lymb-cy087yuvRwoDcAIQZraLQ+n6cBft+lrWsBoH+v8lwQDG35AaIIFXqm0D1JSNIoXOiTsu8UDAlAEBRtAy7fjmiAsKIDC5N+u4rsYsAgEqUGZmhr6ITuYLYDmnI0NBYaDjhQmMsMKBihKADCU7wFcsqMHQv7dL0-QMihDJev0jqIC6E62pYMSCKoUAUMgaZ9IgSzQFSiBfGMtDQKp5ohKp8AejELTJDcVgAGwAOzOM8ZRTlANDkvUKEtE+F7oTeTIslJAnclFQkxcYz7xRAgoilJmDtIyEUFhADIADThe4G74VghUMjQ3L3OV4VWNVEAEZg6TKAw2jjKCQA
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
refs
https://www.typescriptlang.org/play
?xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有??xgqfrms, 禁止转载 ???,侵权必究??!