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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
| /**
* 宏命令 - 组合多个命令
*/
public class MacroCommand implements Command {
private Command[] commands;
private String description;
public MacroCommand(Command[] commands, String description) {
this.commands = commands;
this.description = description;
}
@Override
public void execute() {
System.out.println("🎬 执行宏命令: " + description);
for (Command command : commands) {
System.out.print(" ");
command.execute();
}
}
@Override
public void undo() {
System.out.println("🎬 撤销宏命令: " + description);
// 反向撤销所有命令
for (int i = commands.length - 1; i >= 0; i--) {
System.out.print(" ");
commands[i].undo();
}
}
@Override
public String getDescription() {
return "宏命令: " + description;
}
}
/**
* 智能家居场景管理器
*/
public class SmartHomeSceneManager {
private Map<String, MacroCommand> scenes;
public SmartHomeSceneManager() {
this.scenes = new HashMap<>();
}
public void createScene(String sceneName, Command[] commands) {
MacroCommand scene = new MacroCommand(commands, sceneName);
scenes.put(sceneName, scene);
System.out.println("🏠 创建场景: " + sceneName);
}
public void activateScene(String sceneName) {
MacroCommand scene = scenes.get(sceneName);
if (scene != null) {
System.out.println("🏠 激活场景: " + sceneName);
scene.execute();
} else {
System.out.println("❌ 场景不存在: " + sceneName);
}
}
public void deactivateScene(String sceneName) {
MacroCommand scene = scenes.get(sceneName);
if (scene != null) {
System.out.println("🏠 取消场景: " + sceneName);
scene.undo();
} else {
System.out.println("❌ 场景不存在: " + sceneName);
}
}
public void listScenes() {
System.out.println("=== 可用场景 ===");
for (String sceneName : scenes.keySet()) {
System.out.println(" " + sceneName);
}
}
}
// 智能家居场景演示
public class SmartHomeSceneDemo {
public static void main(String[] args) {
System.out.println("=== 智能家居场景宏命令演示 ===");
// 创建设备
Light livingRoomLight = new Light("客厅");
Light bedroomLight = new Light("卧室");
AirConditioner livingRoomAC = new AirConditioner("客厅");
AirConditioner bedroomAC = new AirConditioner("卧室");
// 创建场景管理器
SmartHomeSceneManager sceneManager = new SmartHomeSceneManager();
System.out.println("\n=== 创建观影场景 ===");
Command[] movieCommands = {
new LightDimCommand(livingRoomLight, 20), // 客厅灯调暗
new LightOffCommand(bedroomLight), // 关闭卧室灯
new AirConditionerOnCommand(livingRoomAC), // 开启客厅空调
new AirConditionerTempCommand(livingRoomAC, 24) // 设置适宜温度
};
sceneManager.createScene("观影模式", movieCommands);
System.out.println("\n=== 创建睡眠场景 ===");
Command[] sleepCommands = {
new LightOffCommand(livingRoomLight), // 关闭客厅灯
new LightOffCommand(bedroomLight), // 关闭卧室灯
new AirConditionerOffCommand(livingRoomAC), // 关闭客厅空调
new AirConditionerOnCommand(bedroomAC), // 开启卧室空调
new AirConditionerTempCommand(bedroomAC, 26) // 设置睡眠温度
};
sceneManager.createScene("睡眠模式", sleepCommands);
System.out.println("\n=== 创建回家场景 ===");
Command[] homeCommands = {
new LightOnCommand(livingRoomLight), // 开启客厅灯
new LightDimCommand(bedroomLight, 50), // 卧室灯调至50%
new AirConditionerOnCommand(livingRoomAC), // 开启客厅空调
new AirConditionerTempCommand(livingRoomAC, 23) // 设置舒适温度
};
sceneManager.createScene("回家模式", homeCommands);
sceneManager.listScenes();
System.out.println("\n=== 激活观影场景 ===");
sceneManager.activateScene("观影模式");
System.out.println("\n当前设备状态:");
System.out.println(livingRoomLight.getStatus());
System.out.println(bedroomLight.getStatus());
System.out.println(livingRoomAC.getStatus());
System.out.println(bedroomAC.getStatus());
System.out.println("\n=== 切换到睡眠场景 ===");
sceneManager.deactivateScene("观影模式"); // 先取消当前场景
sceneManager.activateScene("睡眠模式");
System.out.println("\n当前设备状态:");
System.out.println(livingRoomLight.getStatus());
System.out.println(bedroomLight.getStatus());
System.out.println(livingRoomAC.getStatus());
System.out.println(bedroomAC.getStatus());
System.out.println("\n=== 激活回家场景 ===");
sceneManager.deactivateScene("睡眠模式");
sceneManager.activateScene("回家模式");
System.out.println("\n最终设备状态:");
System.out.println(livingRoomLight.getStatus());
System.out.println(bedroomLight.getStatus());
System.out.println(livingRoomAC.getStatus());
System.out.println(bedroomAC.getStatus());
System.out.println("\n=== 宏命令的优势 ===");
System.out.println("✅ 一键执行多个操作");
System.out.println("✅ 场景化管理,用户体验好");
System.out.println("✅ 支持场景的整体撤销");
System.out.println("✅ 可以组合任意命令");
}
}
|