应用场景
使用组合命令模式实现多功能开关,让用户进行设置,要么控制教室的所有灯的开和关,要么控制教室所有风扇的开和关,要么控制教室所有空调的开和关,要么控制教室里教学多媒体设备的开和关。
模式结构视图
代码设计
控制器类
public class Oyy22Controller {
private Oyy22Command lightsOpenCommand, lightsCloseCommand, blowerOpenCommand, blowerCloseCommand, airOpenCommand,
airCloseCommand, multiOpenCommand, multiCloseCommand;
public Oyy22Controller(Oyy22Command lightsOpenCommand, Oyy22Command lightsCloseCommand,
Oyy22Command blowerOpenCommand, Oyy22Command blowerCloseCommand,
Oyy22Command airOpenCommand, Oyy22Command airCloseCommand,
Oyy22Command multiOpenCommand, Oyy22Command multiCloseCommand) {
this.lightsOpenCommand = lightsOpenCommand;
this.lightsCloseCommand = lightsCloseCommand;
this.blowerOpenCommand = blowerOpenCommand;
this.blowerCloseCommand = blowerCloseCommand;
this.airCloseCommand = airCloseCommand;
this.airOpenCommand = airOpenCommand;
this.multiCloseCommand = multiCloseCommand;
this.multiOpenCommand = multiOpenCommand;
}
public void openAllLights() {
lightsOpenCommand.execute();
}
public void closeAllLights() {
lightsCloseCommand.execute();
}
public void openBlower() {
blowerOpenCommand.execute();
}
public void closeBlower() {
blowerCloseCommand.execute();
}
public void openAirCleaner() {
airOpenCommand.execute();
}
public void closeAirCleaner