Skip to content

Commit bb1b7c5

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

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

Behavioral/state.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const net = require('net');
2+
// Socket Object
3+
// Internal State depends on underlying tcp connection
4+
// Can be readt connect error, close
5+
class Socket {
6+
constructor() {
7+
this.state = new ReadyState(this);
8+
}
9+
connect(url) {
10+
this.state.connect(url);
11+
}
12+
setState(state) {
13+
this.state = state;
14+
}
15+
printState() { console.log("State is ", this.state); }
16+
}
17+
18+
class State {
19+
connect() { }
20+
}
21+
class ReadyState extends State {
22+
constructor(socket) {
23+
super();
24+
this.socket = socket;
25+
}
26+
connect(url) {
27+
const connection = net.createConnection({
28+
host: url,
29+
port: "80"
30+
});
31+
connection.on('error', () => this.socket.setState(new ErrorState(this.socket)) );
32+
connection.on('connect', () => this.socket.setState(new ConnectState(this.socket)));
33+
connection.on('close', () => this.socket.setState(new CloseState(this.socket)));
34+
}
35+
}
36+
37+
class ErrorState extends State {
38+
constructor(socket) {
39+
super();
40+
this.socket = socket;
41+
}
42+
connect() {
43+
console.log("cannot connect in error state");
44+
}
45+
}
46+
47+
class ConnectState extends State {
48+
constructor(socket) {
49+
super();
50+
this.socket = socket;
51+
}
52+
}
53+
54+
class CloseState extends State {
55+
constructor(socket) {
56+
super();
57+
this.socket = socket;
58+
}
59+
}
60+
61+
const socket = new Socket();
62+
const url = "www.example.com";
63+
socket.connect(url);
64+
socket.printState();
65+
// After some time state changes to conenct
66+
setTimeout(() => socket.printState(), 1000);

buildDocs.js

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

docs.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [Mediator](#mediator)
2424
* [Memento](#memento)
2525
* [Observer](#observer)
26+
* [State](#state)
2627

2728

2829

@@ -949,5 +950,76 @@ fileManager.addObserver("change", new SizeChangeObserver());
949950
fileManager.monitorFile(process.argv[1]);
950951
```
951952

953+
### State
954+
##### state.js
955+
```Javascript
956+
const net = require('net');
957+
// Socket Object
958+
// Internal State depends on underlying tcp connection
959+
// Can be readt connect error, close
960+
class Socket {
961+
constructor() {
962+
this.state = new ReadyState(this);
963+
}
964+
connect(url) {
965+
this.state.connect(url);
966+
}
967+
setState(state) {
968+
this.state = state;
969+
}
970+
printState() { console.log("State is ", this.state); }
971+
}
972+
973+
class State {
974+
connect() { }
975+
}
976+
class ReadyState extends State {
977+
constructor(socket) {
978+
super();
979+
this.socket = socket;
980+
}
981+
connect(url) {
982+
const connection = net.createConnection({
983+
host: url,
984+
port: "80"
985+
});
986+
connection.on('error', () => this.socket.setState(new ErrorState(this.socket)) );
987+
connection.on('connect', () => this.socket.setState(new ConnectState(this.socket)));
988+
connection.on('close', () => this.socket.setState(new CloseState(this.socket)));
989+
}
990+
}
991+
992+
class ErrorState extends State {
993+
constructor(socket) {
994+
super();
995+
this.socket = socket;
996+
}
997+
connect() {
998+
console.log("cannot connect in error state");
999+
}
1000+
}
1001+
1002+
class ConnectState extends State {
1003+
constructor(socket) {
1004+
super();
1005+
this.socket = socket;
1006+
}
1007+
}
1008+
1009+
class CloseState extends State {
1010+
constructor(socket) {
1011+
super();
1012+
this.socket = socket;
1013+
}
1014+
}
1015+
1016+
const socket = new Socket();
1017+
const url = "www.example.com";
1018+
socket.connect(url);
1019+
socket.printState();
1020+
// After some time state changes to conenct
1021+
setTimeout(() => socket.printState(), 1000);
1022+
```
1023+
9521024

9531025

0 commit comments

Comments
 (0)