设计模式-命令模式
1.概述
命令模式将“请求”或者“命令”封装成对象,以便使用不同的请求,队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。
2.类图
3.角色
Command接口:所有命令或请求都得实现该接口。
Control对象:管理一组命令对象,通过队列或者集合管理命令。
Temperature对象: 具体的组件对象。
TempreatureLOWCommand对象:将Temperature对象温度调低的命令对象,内部组和了一个要操作的Temperature对象。
4.测试
1.接口Command
public interface Command {
void execute();
void undo();
}
2.Light
public class Light {
boolean isON = false;
public void on(){
isON = true;
System.out.println("打开电灯");
}
public void off(){
isON = false;
System.out.println("关闭电灯");
}
}
3.Temperature
public class Temperature {
public static int HIGH = 3;
public static int MEDIUM = 2;
public static int LOW = 1;
public static int OFF = 0;
int temperature;
public void high(){
temperature = HIGH;
System.out.println("temperature is HIGH");
}
public void medium(){
temperature = MEDIUM;
System.out.println("temperature is MEDIUM");
}
public void low(){
temperature = LOW;
System.out.println("temperature is LOW");
}
public void off(){
temperature = OFF;
System.out.println("temperature is OFF");
}
public int getTemperature() {
return temperature;
}
}
4.对Light的操作命令对象
public class LightOnComand implements Command{
Light light;
public LightOnComand(Light light) {
this.light = light;
}
public void execute() {
light.on();
}
public void undo(){
light.off();
}
}
public class LightOffCommand implements Command{
Light light;
public LightOffCommand(Light light) {
this.light = light;
}
public void execute() {
light.off();
}
public void undo() {
light.on();
}
}
5.对Temperature的操作命令对象
public class TemperatureHIGHCommand implements Command{
Temperature temperature;
int preTemperature; //记录执行之前的temperature
public TemperatureHIGHCommand(Temperature temperature) {
this.temperature = temperature;
}
public void execute() {
preTemperature = temperature.getTemperature();
temperature.high();
}
public void undo() {
if(preTemperature==Temperature.MEDIUM){
temperature.medium();
}else if(preTemperature==Temperature.LOW){
temperature.low();
}else if(preTemperature==Temperature.OFF){
temperature.off();
}else{
// 什么也不做,因为本来也是HIGH
}
}
}
public class TemperatureLOWCommand implements Command{
Temperature temperature;
int preTemperature;
public TemperatureLOWCommand(Temperature temperature) {
this.temperature = temperature;
}
public void execute() {
preTemperature = temperature.getTemperature();
temperature.low();
}
public void undo() {
if(preTemperature==Temperature.MEDIUM){
temperature.medium();
}else if(preTemperature==Temperature.HIGH){
temperature.high();
}else if(preTemperature==Temperature.OFF){
temperature.off();
}else{
// 什么也不做,因为本来也是LOW
}
}
}
public class TemperaTureMEDIUMCommand implements Command{
Temperature temperature;
int preTemperature; //记录执行之前的temperature
public TemperaTureMEDIUMCommand(Temperature temperature) {
this.temperature = temperature;
}
public void execute() {
preTemperature = temperature.getTemperature();
temperature.medium();
}
public void undo() {
if(preTemperature==Temperature.HIGH){
temperature.high();
}else if(preTemperature==Temperature.LOW){
temperature.low();
}else if(preTemperature==Temperature.OFF){
temperature.off();
}else{
// 什么也不做,因为本来也是MEDIUM
}
}
}
public class TemperatureOFFCommand implements Command{
Temperature temperature;
int preTemperature; //记录执行之前的temperature
public TemperatureOFFCommand(Temperature temperature) {
this.temperature = temperature;
}
public void execute() {
preTemperature = temperature.getTemperature();
temperature.off();
}
public void undo() {
if(preTemperature==Temperature.MEDIUM){
temperature.medium();
}else if(preTemperature==Temperature.LOW){
temperature.low();
}else if(preTemperature==Temperature.HIGH){
temperature.high();
}else{
// 什么也不做,因为本来也是OFF
}
}
}
6.noCommand表示无操作对象
public class noCommand implements Command{
public void execute() {
}
public void undo() {
}
}
7.Control管理Command
public class Control {
Command [] onCommands; //保存所有的打开命令集合
Command [] offCommands; //保存所有的关闭命令集合
Command undoCommand; //记录undo日志
public Control() {
this.onCommands = new Command[5];
this.offCommands = new Command[5];
this.undoCommand = new noCommand();
}
public void setCommand(int slot,Command onCommand,Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onCommandWasExecuted(int slot){
onCommands[slot].execute();
undoCommand = onCommands[slot];
}
public void offCommandWasExecuted(int slot){
offCommands[slot].execute();
undoCommand = offCommands[slot];
}
public void undoCommandWasExecuted(){
undoCommand.undo();
undoCommand = new noCommand();
}
}
8.测试main方法
public class Main {
public static void main(String[] args) {
Light light = new Light();
Temperature temperature = new Temperature();
LightOnComand lightOnComand = new LightOnComand(light);
LightOffCommand lightOffCommand = new LightOffCommand(light);
TemperatureHIGHCommand temperatureHIGHCommand = new TemperatureHIGHCommand(temperature);
TemperaTureMEDIUMCommand temperaTureMEDIUMCommand = new TemperaTureMEDIUMCommand(temperature);
TemperatureLOWCommand temperatureLOWCommand = new TemperatureLOWCommand(temperature);
TemperatureOFFCommand temperatureOFFCommand = new TemperatureOFFCommand(temperature);
Command noCommand = new noCommand();
Control myHomeControl = new Control();
myHomeControl.setCommand(0,lightOnComand,lightOffCommand);
myHomeControl.setCommand(1,temperatureHIGHCommand,temperatureOFFCommand);
myHomeControl.setCommand(2,temperaTureMEDIUMCommand,temperatureOFFCommand);
myHomeControl.setCommand(3,temperatureLOWCommand,temperatureOFFCommand);
myHomeControl.onCommandWasExecuted(0);
myHomeControl.onCommandWasExecuted(1);
myHomeControl.onCommandWasExecuted(3);
myHomeControl.undoCommandWasExecuted();
}
}