-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvending_machine.dart
56 lines (47 loc) · 1.31 KB
/
vending_machine.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
51
52
53
54
55
56
/// State Interface
abstract interface class VendingMachineState {
const VendingMachineState();
void handle();
}
/// Concrete States
class ReadyState implements VendingMachineState {
const ReadyState();
@override
void handle() => print("ReadyState");
}
class ProductSelectedState implements VendingMachineState {
const ProductSelectedState();
@override
void handle() => print("ProductSelectedState");
}
class PaymentPendingState implements VendingMachineState {
const PaymentPendingState();
@override
void handle() => print("PaymentPendingState");
}
class OutOfStockState implements VendingMachineState {
const OutOfStockState();
@override
void handle() => print("OutOfStockState");
}
/// Context
class VendingMachineContext {
VendingMachineState _state;
VendingMachineContext(this._state);
void setState(VendingMachineState state) => _state = state;
void request() => _state.handle();
}
///
/// Main
///
void main(List<String> args) {
/// init state
final VendingMachineState readyState = ReadyState();
/// context with init state
final VendingMachineContext context = VendingMachineContext(readyState);
context.request();
/// change state
final VendingMachineState productSelectedState = ProductSelectedState();
context.setState(productSelectedState);
context.request();
}