我正在尝试设计一个基于物联网的基本家庭自动化系统,例如 Alexa 或 Google Home。那么谁能解释一下 Alexa 或 Google Home 中用于动态注册新设备(如灯和风扇)并对其执行操作的基本设计模式是什么。
我已经使用固定设备集的命令模式实现了此功能,但需要对其进行扩展以负责注册动态运行时新对象,而无需更改 Alexa 等代码
public SmartHomeDevice getSmartHomeDevice(String deviceName) {
SmartHomeDevice smartHomeDevice = null;
if (SmartHomeDeviceCategoryConstants.DRAWING_ROOM_LIGHT.getName().equalsIgnoreCase(deviceName)) {
smartHomeDevice = new Light(deviceName);
} else if (SmartHomeDeviceCategoryConstants.LIVING_ROOM_FAN.getName().equalsIgnoreCase(deviceName)) {
smartHomeDevice = new Fan(deviceName);
} else if (SmartHomeDeviceCategoryConstants.SMART_CHARGER.getName().equalsIgnoreCase(deviceName)) {
smartHomeDevice = new ElectricalDevice(deviceName);
}
return smartHomeDevice;
}
}
最佳答案
对于家庭自动化,你必须使用命令设计模式引用https://www.geeksforgeeks.org/command-pattern/ .
关于java - 基于物联网的家庭自动化系统(如 Alexa)中使用的设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57848044/