-
Notifications
You must be signed in to change notification settings - Fork 1
/
command_test.dart
85 lines (69 loc) · 1.52 KB
/
command_test.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
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
84
85
import 'package:test/test.dart';
void main() {
test("should process order commands", () {
// given
final cook = Cook();
final cakeCommand = CakeOrderCommand(
receiver: cook,
cakeName: "cheesecake",
);
final coffeeCommand = CoffeeOrderCommand(
receiver: cook,
coffeeName: "latte",
);
final waiter = Invoker();
// when
waiter.placeOrder(cakeCommand);
waiter.placeOrder(coffeeCommand);
// then
final actualOrder = cook.getOrder();
expect(actualOrder, equals("cheesecake, latte"));
});
}
abstract class Command {
void execute();
}
class CakeOrderCommand implements Command {
final Cook receiver;
final String cakeName;
CakeOrderCommand({
required this.receiver,
required this.cakeName,
});
@override
void execute() {
receiver.prepareCake(cakeName);
}
}
class CoffeeOrderCommand implements Command {
final Cook receiver;
final String coffeeName;
CoffeeOrderCommand({
required this.receiver,
required this.coffeeName,
});
@override
void execute() {
receiver.prepareCoffee(coffeeName);
}
}
class Cook {
String _currentOrder = "";
void prepareCake(String name) {
_appendOrder(name);
}
void prepareCoffee(String name) {
_appendOrder(name);
}
void _appendOrder(String name) {
_currentOrder = _currentOrder.isEmpty ? name : "$_currentOrder, $name";
}
String getOrder() {
return _currentOrder;
}
}
class Invoker {
void placeOrder(Command command) {
command.execute();
}
}