Skip to content

Commit fdace44

Browse files
committed
update docs
Signed-off-by: Nimit <nimitagg95@gmail.com>
1 parent 0b91cfb commit fdace44

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

buildDocs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const sections = [{
88
name: 'Creational', topics: [
99
{ name: 'abstractFactory', path: './Creational' },
1010
{ name: 'builder', path: './Creational' },
11-
{ name: 'factoryMethod', path: './Creational' }
11+
{ name: 'factoryMethod', path: './Creational' },
12+
{ name: 'prototype', path: './Creational' },
13+
{ name: 'singleton', path: './Creational' }
1214
]
1315
}];
1416

docs.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* [Abstract Factory](#abstract-factory)
55
* [Builder](#builder)
66
* [Factory Method](#factory-method)
7+
* [Prototype](#prototype)
8+
* [Singleton](#singleton)
79

810

911

@@ -77,7 +79,7 @@ class ApiRequestFactory {
7779
const availableOptions = ["tcp", "http"];
7880
const apiRequest = ApiRequestFactory.createApiRequest(availableOptions[Math.floor(Math.random() * 2)]);
7981
apiRequest.makeGetRequest("example.com")
80-
.then(respone => console.log(respone))
82+
.then(response => console.log(response))
8183
.catch(err => console.log(err));
8284
```
8385

@@ -234,5 +236,60 @@ let c = new ClientHTTP;
234236
c.main();
235237
```
236238

239+
### Prototype
240+
##### prototype.js
241+
```Javascript
242+
class Server {
243+
244+
constructor(port) {
245+
this._port = port;
246+
}
247+
listen() {
248+
console.log("Listening on port");
249+
}
250+
clone() {
251+
return new Server(this._port);
252+
}
253+
}
254+
255+
const server = new Server();
256+
const newServer = server.clone();
257+
newServer.listen();
258+
```
259+
260+
### Singleton
261+
##### singleton.js
262+
```Javascript
263+
class Server {
264+
constructor(port) {
265+
this._port = port;
266+
}
267+
static init(port) {
268+
if (typeof Server.instance === 'object') {
269+
return Server.instance;
270+
}
271+
Server.instance = new Server(port);
272+
return Server.instance;
273+
}
274+
static getInstance() {
275+
if (typeof Server.instance === 'object') {
276+
return Server.instance;
277+
}
278+
Server.instance = new Server(8080);
279+
return Server.instance;
280+
}
281+
status() {
282+
console.log("Server listening on port " + this._port);
283+
}
284+
}
285+
286+
/**
287+
* Client calls init, and getInstance would give that instance
288+
* always. Singleton is used for heavy single use objects like DB
289+
*/
290+
Server.init(1234);
291+
Server.getInstance().status();
292+
```
293+
237294

238295

0 commit comments

Comments
 (0)