You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.8 KiB
128 lines
3.8 KiB
/**
|
|
* 动态设备管理系统
|
|
* 可以根据设备实现的接口自动识别和管理设备功能
|
|
*/
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class DeviceManager {
|
|
private String name;
|
|
private List<Device> devices;
|
|
|
|
public DeviceManager(String name) {
|
|
this.name = name;
|
|
this.devices = new ArrayList<>();
|
|
}
|
|
|
|
/**
|
|
* 添加设备到管理系统
|
|
* @param device 要添加的设备
|
|
*/
|
|
public void addDevice(Device device) {
|
|
devices.add(device);
|
|
System.out.println(name + " 已添加设备: " + device.getName());
|
|
}
|
|
|
|
/**
|
|
* 移除设备
|
|
* @param deviceName 设备名称
|
|
* @return 是否移除成功
|
|
*/
|
|
public boolean removeDevice(String deviceName) {
|
|
for (Device device : devices) {
|
|
if (device.getName().equals(deviceName)) {
|
|
devices.remove(device);
|
|
System.out.println(name + " 已移除设备: " + deviceName);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 打开所有设备
|
|
*/
|
|
public void turnOnAllDevices() {
|
|
for (Device device : devices) {
|
|
device.turnOn();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 关闭所有设备
|
|
*/
|
|
public void turnOffAllDevices() {
|
|
for (Device device : devices) {
|
|
device.turnOff();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 列出所有设备及其功能
|
|
*/
|
|
public void listAllDevices() {
|
|
System.out.println("===== " + name + " 设备列表 =====");
|
|
for (Device device : devices) {
|
|
System.out.println("设备: " + device.getName() + " (状态: " + (device.isOn() ? "开启" : "关闭") + ")");
|
|
System.out.print(" 支持的功能: ");
|
|
List<String> capabilities = new ArrayList<>();
|
|
|
|
if (device instanceof TemperatureAdjustable) {
|
|
capabilities.add("温度调节");
|
|
}
|
|
if (device instanceof BrightnessAdjustable) {
|
|
capabilities.add("亮度调节");
|
|
}
|
|
if (device instanceof MediaPlayable) {
|
|
capabilities.add("媒体播放");
|
|
}
|
|
if (device instanceof Timable) {
|
|
capabilities.add("定时功能");
|
|
}
|
|
|
|
System.out.println(String.join(", ", capabilities));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据接口类型查找设备
|
|
* @param interfaceType 接口类型
|
|
* @return 实现该接口的设备列表
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public <T> List<T> findDevicesByInterface(Class<T> interfaceType) {
|
|
List<T> result = new ArrayList<>();
|
|
for (Device device : devices) {
|
|
if (interfaceType.isInstance(device)) {
|
|
result.add((T) device);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 批量设置温度调节设备的温度
|
|
* @param temperature 目标温度
|
|
*/
|
|
public void setTemperatureForAll(int temperature) {
|
|
List<TemperatureAdjustable> tempDevices = findDevicesByInterface(TemperatureAdjustable.class);
|
|
for (TemperatureAdjustable device : tempDevices) {
|
|
if (device instanceof Device && ((Device)device).isOn()) {
|
|
device.setTemperature(temperature);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量设置亮度调节设备的亮度
|
|
* @param brightness 目标亮度
|
|
*/
|
|
public void setBrightnessForAll(int brightness) {
|
|
List<BrightnessAdjustable> brightnessDevices = findDevicesByInterface(BrightnessAdjustable.class);
|
|
for (BrightnessAdjustable device : brightnessDevices) {
|
|
if (device instanceof Device && ((Device)device).isOn()) {
|
|
device.setBrightness(brightness);
|
|
}
|
|
}
|
|
}
|
|
} |