Skip to content

Commit 0b4650a

Browse files
authored
Add: Design Pattern
1 parent 2077eee commit 0b4650a

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ One place JavaScript!
44

55
### Table of Contents
66

7+
- [Design Pattern](#pattern)
78
- [Callback/ Higher-order Function](#callback)
89
- [Promises](#promises)
910
- [Closures](#closures)
@@ -12,6 +13,57 @@ One place JavaScript!
1213
- [DOM](#dom)
1314
- [Equality comparisons](#eqality)
1415

16+
<a name=“pattern”/>
17+
18+
#### Design Pattern
19+
20+
- [Singleton Pattern](#singleton)
21+
22+
23+
<a name=“singleton”/>
24+
25+
##### Singleton Pattern:
26+
The singleton design pattern restricts the instantiation of a function/class to one variable/object.
27+
28+
Uses:
29+
1. When exactly one variable/object is needed to coordinate actions across the system.
30+
2. Reduce the need of global variables.
31+
3. The getInstance method demonstates another design pattern called Lazy Load.
32+
33+
``` javascript
34+
var Singleton = (function () {
35+
var instance;
36+
37+
function createInstance() {
38+
var object = new Object("I am the instance");
39+
return object;
40+
}
41+
42+
return {
43+
getInstance: function () {
44+
if (!instance) {
45+
instance = createInstance();
46+
}
47+
return instance;
48+
}
49+
};
50+
})();
51+
52+
function run() {
53+
54+
var instance1 = Singleton.getInstance();
55+
var instance2 = Singleton.getInstance();
56+
57+
alert("Same instance? " + (instance1 === instance2)); //Output: Same instance? true
58+
}
59+
```
60+
61+
Ref:
62+
63+
https://blog.mgechev.com/2014/04/16/singleton-in-javascript/
64+
65+
https://www.dofactory.com/javascript/singleton-design-pattern
66+
1567
<a name=“callback”/>
1668

1769
#### Callback/ Higher-order Function

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy