-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommand.dart
67 lines (52 loc) · 1.35 KB
/
Command.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
import 'package:design_pattern_dart/Display/Example.dart';
class Command extends Example {
Command([String filePath = "lib/Behavioral/Command.dart"]) : super(filePath);
@override
String testRun() {
Bulb bulb = Bulb();
BulbCommand turnOn = TurnOn(bulb);
BulbCommand turnOff = TurnOff(bulb);
BulbRemoteControl control = BulbRemoteControl();
return """
${control.submit(turnOn)}
${control.undo(turnOn)}
${control.submit(turnOff)}
""";
}
}
// 最終要處理事情的人 (Receiver) (主廚)
class Bulb {
String turnOn() => "Bulb has been lit.";
String turnOff() => "Darkness!";
}
// Client 只要設定好 Command 丟給 Invoker 即可
abstract class BulbCommand {
String execute();
String undo();
String redo();
}
class TurnOn implements BulbCommand {
Bulb bulb;
TurnOn(this.bulb);
@override
String execute() => bulb.turnOn();
@override
String redo() => execute();
@override
String undo() => bulb.turnOff();
}
class TurnOff implements BulbCommand {
Bulb bulb;
TurnOff(this.bulb);
@override
String execute() => bulb.turnOff();
@override
String redo() => execute();
@override
String undo() => bulb.turnOn();
}
// 搖控器做為 Invoker (服務生)
class BulbRemoteControl {
String submit(BulbCommand command) => command.execute();
String undo(BulbCommand command) => command.undo();
}