-
Notifications
You must be signed in to change notification settings - Fork 1
/
singleton.js
24 lines (18 loc) · 853 Bytes
/
singleton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// A Singleton restrict the instantiation of a class to a single object.
// If we instantiate the first, and single object, it will use the same reference in the next instantiation
const singleton = (() => {
let instance;
const privateRandomNumber = Math.random();
const init = () => ({
getRandomNumber: () => privateRandomNumber
});
return {
getInstance: () => instance || (instance = init())
};
})();
const instance1 = singleton.getInstance();
const instance2 = singleton.getInstance();
console.log(instance1.getRandomNumber() === instance2.getRandomNumber()); // always true
// We use the module pattern to create the local scope and define the (single) instance
// The get instance method will access the defined `instance` first. If has no instance yet,
// it will call `init`, assign to instance, and return the instance