Skip to content

Commit 6228097

Browse files
committed
add strategy design patttern
Signed-off-by: Nimit <nimitagg95@gmail.com>
1 parent bb1b7c5 commit 6228097

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

Behavioral/lib.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const net = require('net');
2+
const http = require('http');
3+
4+
// Abstract Product Class
5+
class ApiRequest {
6+
makeGetRequest(url) {
7+
return Error(`Implement make Get Request for ${url}`);
8+
}
9+
}
10+
11+
// Product A
12+
class tcpApiRequest extends ApiRequest {
13+
makeGetRequest(url) {
14+
// Handling simple get request without query params
15+
return new Promise((resolve, reject) => {
16+
const socket = net.createConnection({
17+
host: "www.example.com",
18+
port: "80"
19+
});
20+
21+
socket.on('data', (data) => resolve(data.toString()));
22+
23+
socket.on('error', err => reject(err));
24+
25+
socket.end(`GET / HTTP/1.1\r\nHost: ${url}\r\n\r\n`);
26+
});
27+
28+
}
29+
}
30+
31+
// Product B
32+
class httpApiRequest extends ApiRequest {
33+
makeGetRequest(url) {
34+
// Handling simple get request without query params
35+
return new Promise((resolve, reject) => {
36+
http.request(`http://${url}`, (res) => {
37+
res.on('data', data => resolve(data.toString()));
38+
res.on('error', err => reject(err));
39+
}).end();
40+
});
41+
}
42+
}
43+
44+
/**
45+
* This is an abstract factory interface. Uses a static function to
46+
* generate family of products.
47+
*/
48+
class ApiRequestFactory {
49+
static createApiRequest(kind) {
50+
// This can easily be extended to include HTTPS, HTTP2, HTTP3
51+
switch(kind) {
52+
case "tcp":
53+
return new tcpApiRequest();
54+
case "http":
55+
return new httpApiRequest();
56+
}
57+
}
58+
}
59+
60+
module.exports = ApiRequestFactory;

Behavioral/strategy.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* ApiRequestFactory gives underlying startegies for Ali request
3+
*/
4+
const ApiRequestFactory = require("./lib");
5+
class UpstreamFile {
6+
getFileUpstream() { }
7+
}
8+
/**
9+
* Here by default Stategy for API request is http
10+
*/
11+
class Config extends UpstreamFile {
12+
constructor(url, apiRequest) {
13+
super();
14+
this.url = url;
15+
this.apiRequest = apiRequest || ApiRequestFactory.createApiRequest("http");
16+
}
17+
getFileUpstream() {
18+
this.apiRequest
19+
.makeGetRequest(this.url)
20+
.then(response => console.log(response))
21+
.catch(err => console.log(err));
22+
}
23+
}
24+
25+
/**
26+
* AbstractFactory is used to generate related implementation for these
27+
* classes
28+
*/
29+
const config = new Config("jsonplaceholder.typicode.com/posts/1");
30+
// Using default config http
31+
config.getFileUpstream();
32+
33+
const config2 = new Config("https://jsonplaceholder.typicode.com/todos/1",
34+
ApiRequestFactory.createApiRequest("tcp"));
35+
// Get tcp strategy
36+
config2.getFileUpstream();

buildDocs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const sections = [
3737
{ name: 'memento', path: './Behavioral' },
3838
{ name: 'observer', path: './Behavioral' },
3939
{ name: 'state', path: './Behavioral' },
40+
{ name: 'strategy', path: './Behavioral' },
4041
]
4142
}
4243
];

docs.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* [Memento](#memento)
2525
* [Observer](#observer)
2626
* [State](#state)
27+
* [Strategy](#strategy)
2728

2829

2930

@@ -1021,5 +1022,47 @@ socket.printState();
10211022
setTimeout(() => socket.printState(), 1000);
10221023
```
10231024

1025+
### Strategy
1026+
##### strategy.js
1027+
```Javascript
1028+
/**
1029+
* ApiRequestFactory gives underlying startegies for Ali request
1030+
*/
1031+
const ApiRequestFactory = require("./lib");
1032+
class UpstreamFile {
1033+
getFileUpstream() { }
1034+
}
1035+
/**
1036+
* Here by default Stategy for API request is http
1037+
*/
1038+
class Config extends UpstreamFile {
1039+
constructor(url, apiRequest) {
1040+
super();
1041+
this.url = url;
1042+
this.apiRequest = apiRequest || ApiRequestFactory.createApiRequest("http");
1043+
}
1044+
getFileUpstream() {
1045+
this.apiRequest
1046+
.makeGetRequest(this.url)
1047+
.then(response => console.log(response))
1048+
.catch(err => console.log(err));
1049+
}
1050+
}
1051+
1052+
/**
1053+
* AbstractFactory is used to generate related implementation for these
1054+
* classes
1055+
*/
1056+
const config = new Config("jsonplaceholder.typicode.com/posts/1");
1057+
// Using default config http
1058+
config.getFileUpstream();
1059+
1060+
const config2 = new Config("https://jsonplaceholder.typicode.com/todos/1",
1061+
ApiRequestFactory.createApiRequest("tcp"));
1062+
// Get tcp strategy
1063+
config2.getFileUpstream();
1064+
1065+
```
1066+
10241067

10251068

0 commit comments

Comments
 (0)