-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathstate.dart
50 lines (41 loc) · 971 Bytes
/
state.dart
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
abstract class State {
void handler(Stateful context);
String toString();
}
class StatusOn implements State {
handler(Stateful context) {
print(" Handler of StatusOn is being called!");
context.state = StatusOff();
}
@override
String toString() {
return "on";
}
}
class StatusOff implements State {
handler(Stateful context) {
print(" Handler of StatusOff is being called!");
context.state = StatusOn();
}
@override
String toString() {
return "off";
}
}
class Stateful {
State _state;
Stateful(this._state);
State get state => _state;
set state(State newState) => _state = newState;
void touch() {
print(" Touching the Stateful...");
_state.handler(this);
}
}
void main() {
var lightSwitch = Stateful(StatusOff());
print("The light switch is ${lightSwitch.state}.");
print("Toggling the light switch...");
lightSwitch.touch();
print("The light switch is ${lightSwitch.state}.");
}