《Head First设计形式》阅读笔记.第六章

《Head First设计模式》阅读笔记.第六章
1.命令(Command)模式部分

引用
----连连看解答----
女招待->Invoker
快餐厨师->Receiver
orderUp()->execute()
订单->Command
顾客->Client
takeOrder()->setCommand
------------


----GarageDoorOpenCommand类----
public class GarageDoorOpenCommand {
    GarageDoor door;
    
    public GarageDoorOpenCommand(GarageDoor door){
        this.door = door;
    }
    
    public void execute() {
        door.up();
    }
}
------------


命令(Command)模式:将“请求”封装成对象,以便使用请求、队列或日志来参数化其它对象。命令模式也支持可撤销的操作。

*命令对象通过在一个特定接受者上绑定一组动作来封装请求。为实现这一点,命令对象将动作和接受者包进对象中。

*空对象(null object)可以用于返回无意义的对象时,它可以承担处理null的责任。有时候空对象也被视为一种设计模式。

*将命令执行之前的状态记录在命令对象中,这样就可以实现更复杂的撤销操作。

----CeilingFanLowCommand类----
public class CeilingFanLowCommand implements Command {
    private CeilingFan fan;
    private int prevSpeed;
    
    public CeilingFanLowCommand(CeilingFan fan) {
        this.fan = fan;
    }
    
    public void execute() {
        prevSpeed = fan.getSpeed();
        fan.low();
    }
    
    public void undo() {
        if (prevSpeed == CeilingFan.HIGH) {
            fan.high();
        } else if (prevSpeed == CeilingFan.MEDIUM) {
            fan.medium();
        } else if (prevSpeed == CeilingFan.LOW) {
            fan.low();
        } else if (prevSpeed == CeilingFan.OFF) {
            fan.off();
        }
    }
}
------------

----CeilingFanMediumCommand类----
public class CeilingFanMediumCommand implements Command {
    private CeilingFan fan;
    private int prevSpeed;
    
    public CeilingFanMediumCommand(CeilingFan fan) {
        this.fan = fan;
    }
    
    public void execute() {
        prevSpeed = fan.getSpeed();
        fan.medium();
    }
    
    public void undo() {
        if (prevSpeed == CeilingFan.HIGH) {
            fan.high();
        } else if (prevSpeed == CeilingFan.MEDIUM) {
            fan.medium();
        } else if (prevSpeed == CeilingFan.LOW) {
            fan.low();
        } else if (prevSpeed == CeilingFan.OFF) {
            fan.off();
        }
    }
}
------------

----CeilingFanOffCommand类----
public class CeilingFanOffCommand implements Command {
    private CeilingFan fan;
    private int prevSpeed;
    
    public CeilingFanOffCommand(CeilingFan fan) {
        this.fan = fan;
    }
    
    public void execute() {
        prevSpeed = fan.getSpeed();
        fan.off();
    }
    
    public void undo() {
        if (prevSpeed == CeilingFan.HIGH) {
            fan.high();
        } else if (prevSpeed == CeilingFan.MEDIUM) {
            fan.medium();
        } else if (prevSpeed == CeilingFan.LOW) {
            fan.low();
        } else if (prevSpeed == CeilingFan.OFF) {
            fan.off();
        }
    }
}
------------


*宏命令(Macro Command)是一个命令队列,它包含了一组实现了同一个命令接口的类。

----Sharpen Your Pencil解答----
Light light = new Light("Living Room");
TV tv = new TV("Living Room");
Stereo stereo = new Stereo("Living Room");
Hottub hottub = new Hottub();

LightOffCommand lightoff = new LightOffCommand(light);
TVOffCommand tvoff = new TVOffCommand(tv);
StereoOffCommand stereooff = new StereoOffCommand(stereo);
HottubOffCommand hottuboff = new HottubOffCommand(hottub);
------------

----undo()方法----
public void undo() {
    // 我觉得根据命令执行的顺序反向地撤销更为合理
    for(int i = commands.length() - 1; i >= 0; i--) {
        commands[i].undo();
    }
}
------------


*命令(Command)模式通过把接受者当作参数来传递,并且让所有的命令对象都实现一个命令接口的方式,实现了针对接口编程,从而在调用者和接受者之间实现了解耦。

*在调用者中用一个堆栈记录连续执行的命令,这样就可以实现每按一次按钮就执行一次撤销操作的连续撤销功能。

*命令(Command)模式将发出请求的对象和执行请求的对象解耦。

*被解耦的对象之间通过命令对象进行沟通,命令对象封装了接受者和一个或一组动作。

*宏命令是命令的一种简单延伸,执行调用多个命令。宏方法也可以支持撤销。

*在实际操作时,很常见地用“聪明”命令对象,也就是直接实现了请求,而不是将工作委托(Delegate)给接收者。

*命令可以被用来实现日志和事物系统。

2.命令(Command)模式实例
// mp3播放器
public class Mp3Player {
	private String name;

	public Mp3Player(String n) {
		this.name = n;
	}

	public void playMusic() {
		System.out.println("Music is playing.");
	}

	public void stopMusic() {
		System.out.println("Music is stopped.");
	}

	public String getName() {
		return name;
	}
}

// 收音机
public class Radio {
	private float frequency = 100;// 频率

	private String name;

	public Radio(String n) {
		this.name = n;
	}

	public void on() {
		System.out.println("Radio is on.");
	}

	public void off() {
		System.out.println("Radio is off.");
	}

	public void setFrequency(float f) {
		this.frequency = f;
		System.out.println("Frequency is set to " + f);
	}

	public float getFrequency() {
		return this.frequency;
	}

	public String getName() {
		return name;
	}
}

// 命令接口
public interface Command {
	public void execute();

	public void undo();
}

// 播放音乐命令
public class PlayMusicCommand implements Command {
	private Mp3Player player;

	public PlayMusicCommand(Mp3Player p) {
		this.player = p;
	}

	public void execute() {
		this.player.playMusic();
	}

	public void undo() {
		this.player.stopMusic();
	}
}

// 停止音乐命令
public class StopMusicCommand implements Command {
	private Mp3Player player;

	public StopMusicCommand(Mp3Player p) {
		this.player = p;
	}

	public void execute() {
		this.player.stopMusic();
	}

	public void undo() {
		this.player.playMusic();
	}
}

// 打开收音机命令
public class RadioOnCommand implements Command {
	private Radio radio;

	public RadioOnCommand(Radio p) {
		this.radio = p;
	}

	public void execute() {
		this.radio.on();
	}

	public void undo() {
		this.radio.off();
	}
}

// 关闭收音机命令
public class RadioOffCommand implements Command {
	private Radio radio;

	public RadioOffCommand(Radio p) {
		this.radio = p;
	}

	public void execute() {
		this.radio.off();
	}

	public void undo() {
		this.radio.on();
	}
}

// 频率调高命令
public class FrequencyHigherCommand implements Command {
	private Radio radio;

	private float prevFre;

	public FrequencyHigherCommand(Radio p) {
		this.radio = p;
	}

	public void execute() {
		this.prevFre = radio.getFrequency();
		radio.setFrequency(this.prevFre + 0.1f);
	}

	public void undo() {
		this.radio.setFrequency(prevFre);
	}
}

// 频率调低命令
public class FrequencylowerCommand implements Command {
	private Radio radio;

	private float prevFre;

	public FrequencylowerCommand(Radio p) {
		this.radio = p;
	}

	public void execute() {
		this.prevFre = radio.getFrequency();
		if (this.prevFre >= 0.1)
			radio.setFrequency(this.prevFre - 0.1f);
	}

	public void undo() {
		this.radio.setFrequency(prevFre);
	}
}

// 遥控器
public class RemoteControl {
	private Command[] leftCommands;

	private Command[] rightCommands;

	private Command prevCommand;

	public RemoteControl() {
		leftCommands = new Command[3];
		rightCommands = new Command[3];

		for (int i = 0; i < leftCommands.length; i++) {
			leftCommands[i] = new NoCommand();
		}
		for (int i = 0; i < rightCommands.length; i++) {
			rightCommands[i] = new NoCommand();
		}

		prevCommand = new NoCommand();
	}

	public void setCommand(int index, Command left, Command right) {
		leftCommands[index] = left;
		rightCommands[index] = right;
	}

	public void leftButtonPressed(int index) {
		System.out.println("Left button [" + index + "] is pressed.");
		leftCommands[index].execute();
		prevCommand = leftCommands[index];
	}

	public void rightButtonPressed(int index) {
		System.out.println("Right button [" + index + "] is pressed.");
		rightCommands[index].execute();
		prevCommand = rightCommands[index];
	}

	public void cancelButtonPressed() {
		System.out.println("Cancel button is pressed.");
		prevCommand.undo();
	}
}

// 空命令,处理空对象(null object)
public class NoCommand implements Command {
	public void execute() {
	}

	public void undo() {
	}
}

----命令(Command)模式测试程序----
// 测试程序
public class TestRemoteControl {
	public static void main(String[] args) {
		RemoteControl rc = new RemoteControl();

		Mp3Player player = new Mp3Player("Sony Ericsson");
		PlayMusicCommand pm = new PlayMusicCommand(player);
		StopMusicCommand sm = new StopMusicCommand(player);
		rc.setCommand(0, pm, sm);

		Radio radio = new Radio("Tecsun");
		RadioOnCommand ron = new RadioOnCommand(radio);
		RadioOffCommand roff = new RadioOffCommand(radio);
		FrequencyHigherCommand fh = new FrequencyHigherCommand(radio);
		FrequencylowerCommand fl = new FrequencylowerCommand(radio);
		rc.setCommand(1, ron, roff);
		rc.setCommand(2, fh, fl);
		
		rc.leftButtonPressed(0);
		rc.cancelButtonPressed();
		rc.leftButtonPressed(0);
		rc.rightButtonPressed(0);
		rc.cancelButtonPressed();
		
		rc.leftButtonPressed(1);
		rc.cancelButtonPressed();
		rc.leftButtonPressed(1);
		rc.rightButtonPressed(1);
		rc.cancelButtonPressed();
		
		rc.leftButtonPressed(2);
		rc.cancelButtonPressed();
		rc.leftButtonPressed(2);
		rc.rightButtonPressed(2);
		rc.cancelButtonPressed();
	}
}


引用
----测试程序运行结果----
Left button [0] is pressed.
Music is playing.
Cancel button is pressed.
Music is stopped.
Left button [0] is pressed.
Music is playing.
Right button [0] is pressed.
Music is stopped.
Cancel button is pressed.
Music is playing.
Left button [1] is pressed.
Radio is on.
Cancel button is pressed.
Radio is off.
Left button [1] is pressed.
Radio is on.
Right button [1] is pressed.
Radio is off.
Cancel button is pressed.
Radio is on.
Left button [2] is pressed.
Frequency is set to 100.1
Cancel button is pressed.
Frequency is set to 100.0
Left button [2] is pressed.
Frequency is set to 100.1
Right button [2] is pressed.
Frequency is set to 100.0
Cancel button is pressed.
Frequency is set to 100.1

------------


3.宏命令(Macro Command)实例

在以上代码基础上完成:
// 宏命令
public class MacroCommand implements Command {
	private Command[] commands;

	public MacroCommand(Command[] commands) {
		this.commands = commands;
	}

	public void execute() {
		for (int i = 0; i < commands.length; i++) {
			commands[i].execute();
		}
	}

	public void undo() {
		for (int i = commands.length - 1; i >= 0; i--) {
			commands[i].undo();
		}
	}
}

----宏命令(Macro Command)测试程序----
// 测试宏命令
public class TestMacroCommand {
	public static void main(String[] args) {
		Mp3Player player = new Mp3Player("Sony Ericsson");
		PlayMusicCommand pm = new PlayMusicCommand(player);
		StopMusicCommand sm = new StopMusicCommand(player);

		Radio radio = new Radio("Tecsun");
		RadioOnCommand ron = new RadioOnCommand(radio);
		RadioOffCommand roff = new RadioOffCommand(radio);
		FrequencyHigherCommand fh = new FrequencyHigherCommand(radio);
		FrequencylowerCommand fl = new FrequencylowerCommand(radio);
		FrequencyHigherCommand fh2 = new FrequencyHigherCommand(radio);
		FrequencylowerCommand fl2 = new FrequencylowerCommand(radio);

		Command[] commands = new Command[] { pm, sm, ron, fh, fh2, fl, fl2, roff };
		MacroCommand mc = new MacroCommand(commands);

		RemoteControl rc = new RemoteControl();
		rc.setCommand(0, mc, mc);
		rc.leftButtonPressed(0);
		rc.cancelButtonPressed();
	}
}


引用
----测试程序运行结果----
Left button [0] is pressed.
Music is playing.
Music is stopped.
Radio is on.
Frequency is set to 100.1
Frequency is set to 100.2
Frequency is set to 100.1
Frequency is set to 100.0
Radio is off.
Cancel button is pressed.
Radio is on.
Frequency is set to 100.1
Frequency is set to 100.2
Frequency is set to 100.1
Frequency is set to 100.0
Radio is off.
Music is playing.
Music is stopped.

--------


4.聪明命令(Smart Command)实例

在以上代码基础上完成:
// 聪明(Smart)命令实例
public class PrintLogCommand implements Command {
	public void execute() {
		// 自己实现,而不是委托其它类
		System.out.println("Log at " + new Date());
	}

	public void undo() {
		// 命令可以支持撤销,也可以不支持
	}
}

----聪明命令(Smart Command)测试程序----
// 测试聪明命令
public class TestSmartCommand {
	public static void main(String[] args) {
		PrintLogCommand pl = new PrintLogCommand();
		RemoteControl rc = new RemoteControl();
		rc.setCommand(0, pl, pl);
		rc.leftButtonPressed(0);
	}
}


引用
----测试程序运行结果----
Left button [0] is pressed.
Log at Tue Jan 19 14:44:02 CST 2010

------------


--END--