-
Notifications
You must be signed in to change notification settings - Fork 74
/
State.js
57 lines (48 loc) · 1.12 KB
/
State.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
class Context {
constructor(state) {
console.log("Context Class created");
switch (state) {
case "A":
this.state = new ConcreteStateA()
break
case "B":
this.state = new ConcreteStateB()
break
default:
this.state = new ConcreteStateA()
}
}
request() {
console.log('Context.request invoked');
this.state.handle(this);
}
}
class State {
constructor() {
console.log("State Class created");
}
handle() {
console.log('State.handle invoked');
}
}
class ConcreteStateA extends State {
constructor() {
super();
console.log("ConcreteStateA Class created");
}
handle(context) {
console.log('ConcreteStateA.handle invoked');
}
}
class ConcreteStateB extends State {
constructor() {
super();
console.log("ConcreteStateB Class created");
}
handle(context) {
console.log('ConcreteStateB.handle invoked');
}
}
let context = new Context("A")
context.request()