File tree Expand file tree Collapse file tree 3 files changed +139
-0
lines changed Expand file tree Collapse file tree 3 files changed +139
-0
lines changed Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ const sections = [
36
36
{ name : 'mediator' , path : './Behavioral' } ,
37
37
{ name : 'memento' , path : './Behavioral' } ,
38
38
{ name : 'observer' , path : './Behavioral' } ,
39
+ { name : 'state' , path : './Behavioral' } ,
39
40
]
40
41
}
41
42
] ;
Original file line number Diff line number Diff line change 23
23
* [ Mediator] ( #mediator )
24
24
* [ Memento] ( #memento )
25
25
* [ Observer] ( #observer )
26
+ * [ State] ( #state )
26
27
27
28
28
29
@@ -949,5 +950,76 @@ fileManager.addObserver("change", new SizeChangeObserver());
949
950
fileManager .monitorFile (process .argv [1 ]);
950
951
```
951
952
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
+
952
1024
953
1025
You can’t perform that action at this time.
0 commit comments