-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingletonPattern.js
More file actions
36 lines (29 loc) · 794 Bytes
/
Copy pathSingletonPattern.js
File metadata and controls
36 lines (29 loc) · 794 Bytes
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
"use strict";
/**
* Singleton Pattern - Simplest pattern of all that ensures only one class
* has only one instance and it is a global property.
*
* Example - Usually for configuration settings where you don't want to create
* a chaos by having many instances of the same class
*
*/
let Singleton = (() => {
let instance = undefined;
class ConfigModule {
someReallyUsefulFunction() {
console.log("Hello World");
}
}
let getInstance = () => {
if (!instance) {
instance = new ConfigModule()
}
return instance;
}
return {
getInstance: getInstance
};
})();
let instance1 = Singleton.getInstance();
let instance2 = Singleton.getInstance();
console.log(instance1 === instance2);