-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.ts
83 lines (72 loc) · 2.17 KB
/
state.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* **State**
*
* > "Allow an object to alter its behavior when its internal state changes.
* The object will appear to change its class."
*
* In front-end applications and games it's super common to encounter state,
* because these types of systems are heavily reliant on states of UIs, players,
* inventories, and whatever knick-knacks you can think of.
*
* The State pattern, then, is a pattern meant to express the states of an object
* and how the object can change its state based on those states. Consequently,
* making state transitions explicit is an effect of using the pattern.
*
* While state can be shared, don't forget to think about who "owns" the transitions.
* In our demo, this is clear, but your case may get more polluted.
*
* In closing, It's quite straightforward to implement, and it's useful in numerous cases.
*
* Below, let's check it out for a lighting system.
*
* @see https://refactoring.guru/design-patterns/state
* @see https://en.wikipedia.org/wiki/State_pattern
* @see Page 305 in `Design Patterns - Elements of Reusable Object-Oriented Software`
*/
function stateDemo() {
interface LightState {
turnOn(context: LightBulb): void;
turnOff(context: LightBulb): void;
}
// Concrete State classes
class OnState implements LightState {
turnOn() {
console.log('The light is already on.');
}
turnOff(context: LightBulb) {
console.log('Turning off the light.');
context.setState(new OffState());
}
}
class OffState implements LightState {
turnOn(context: LightBulb) {
console.log('Turning on the light.');
context.setState(new OnState());
}
turnOff() {
console.log('The light is already off.');
}
}
// Context - what the user will interact with
class LightBulb {
private state: LightState = new OffState();
setState(state: LightState) {
this.state = state;
}
turnOn() {
this.state.turnOn(this);
}
turnOff() {
this.state.turnOff(this);
}
}
// Toggles the modes a bit
const bulb = new LightBulb();
bulb.turnOn();
bulb.turnOn();
bulb.turnOff();
bulb.turnOn();
bulb.turnOff();
bulb.turnOff();
}
stateDemo();