-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path02-创建型模式-单例模式.js
More file actions
64 lines (53 loc) · 1.63 KB
/
Copy path02-创建型模式-单例模式.js
File metadata and controls
64 lines (53 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* Person 类 */
class Person {
constructor(name, age) {
this.name = name
this.age = age
}
}
/* IIFE */
// const Singleton = (function() {
// let _instance = null
// const ProxySingleton = function(name, age) {
// if(_instance) return _instance
// _instance = new Person(name, age)
// return _instance
// }
// ProxySingleton.getInstance = function(name, age) {
// if (_instance) return _instance
// _instance = new Singleton(name, age)
// return _instance
// }
// return ProxySingleton
// })()
// const person1 = new Singleton('张小帅', 25) // '张小帅', 25
// const person2 = new Singleton('李小美', 23) // '张小帅', 25
// console.log('person1 :', person1);
// console.log('person2 :', person2);
/* Proxy */
function Singleton(FuncClass) {
let _instance
return new Proxy(FuncClass, {
construct(target, args) {
return _instance || (_instance = Reflect.construct(FuncClass, args)) // 使用 new FuncClass(...args) 也可以
}
})
}
const PersonInstance = Singleton(Person)
const person1 = new PersonInstance('张小帅', 25) // '张小帅', 25
const person2 = new PersonInstance('李小美', 23) // '张小帅', 25
console.log('person1 === person2 :', person1 === person2); // true
// 饿汉式
const HungrySingleton = (function() {
const _instance = new FuncClass()
return function() {
return _instance
}
})()
// 懒汉式
const LazySingleton = (function() {
let _instance = null
return function() {
return _instance || (_instance = new FuncClass())
}
})()