设计模式----命令模式
?
好久没过来写blog了,今天写一下命令模式。
案例:设计一个家电自动化的API,这个遥控器具有几个可编程的插槽,每一个插槽都有对应的开关按钮,还有一些java类,这些类由多家厂商提供,控制家电自动化装置,
例如:电灯,风扇等。好了,此时就要创建一个控制遥控器的API出来,让每一个插槽都能够控制一个装置。
每个类都有on()/off()方法,除外可能还有其他方法(如:stop()方法等),而且厂商可能还会有更多。
首先,遥控器应该知道如何解读按钮被按下的动作,即命令,然后发出正确的请求,但是遥控器不需要知道这些家电自动化细节,与厂商解耦。
其次,可能有些人会想到用if判断语句:if command==Light then Light.on , else if command==GarageDoor then?GarageDoor.stop 等增加判断来解决
但有个模式可以解决上面两种情况,OK,那就是命令模式,代码如下:
?
?
/** * 命令接口 */public interface Command { /** * 只需要一个方法 */public void execute();}
?
?
?
?
/** * 厂商提供电灯 */public class Light {public Light() {}public void on() {System.out.println("Light is on");}public void off() {System.out.println("Light is off");}}
?
?
?
/** * 电灯打开命令,这是一个命令所以需要实现Command接口 */public class LightOnCommand implements Command {Light light; /** * 构造器被传入了某个电灯,以便让这个命令控制,然后记录 * 在实例变量中。一旦调用execute/undo就由这个电灯对象 * 成为接受者,负责接受请求 */public LightOnCommand(Light light) {this.light = light;} /** * 调用接受者的on()方法 */public void execute() {light.on();}}
?
?
?
?
/** * 电灯关闭命令 */public class LightOffCommand implements Command {Light light; public LightOffCommand(Light light) {this.light = light;} public void execute() {light.off();}}?
?
?
/** * 车库门 */public class GarageDoor {public GarageDoor() {}public void up() {System.out.println("Garage Door is Open");}public void down() {System.out.println("Garage Door is Closed");}public void stop() {System.out.println("Garage Door is Stopped");}public void lightOn() {System.out.println("Garage light is on");}public void lightOff() {System.out.println("Garage light is off");}}?
?
?
?
/** * 车库门命令 */public class GarageDoorOpenCommand implements Command {GarageDoor garageDoor;public GarageDoorOpenCommand(GarageDoor garageDoor) {this.garageDoor = garageDoor;}public void execute() {garageDoor.up();}}?
?
?
?
/** * 简单遥控器 */public class SimpleRemoteControl { /** * 一个插槽持有命令,而这个命令控制着一个装置 */Command slot; public SimpleRemoteControl() {} /** * 这个方法用来设置插槽控制的命令,如果 * 客户想要改变按钮行为,可以调用此方法 */public void setCommand(Command command) {slot = command;} /** * 当按下按钮时,这个方法就会被调用,使得当前命令衔接插槽 */public void buttonWasPressed() {slot.execute();}}?
?
?
?
/** * 遥控器测试 */public class RemoteControlTest {public static void main(String[] args) {SimpleRemoteControl remote = new SimpleRemoteControl();Light light = new Light();GarageDoor garageDoor = new GarageDoor();LightOnCommand lightOn = new LightOnCommand(light);GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor); remote.setCommand(lightOn);remote.buttonWasPressed();remote.setCommand(garageOpen);remote.buttonWasPressed(); }}?
?
OK,可以看出命令模式可将“动作的请求者”从“动作的执行者”对象中解耦出来,此例中,请求这是遥控器,而执行者对象就是厂商类其中之一的实例。
命令模式定义:将“请求”封装称对象,以便使用不同的请求,队列或者日志来参数化其他对象,命令模式也支持可撤销的操作。可以知道,一个对象通过在特定的接受者上绑定一组动作来封装一个请求,
即命令对象将动作和接受者包进对象中,而这个对象只暴露一个execute方法。
命令模式类图如下:
?
?
?
OK,简单介绍了一下命令模式,这里只介绍了一下单个请求,对于一组请求(可以用一个让接受者包含一个数组命令,即让SimpleRemoteControl含有一个Command[]数组实例),
队列与一组请求类似(用Queue完成),日志等这里就不再介绍了,其实都是相似的
好了,今天课程结束,下课,呵。。。。。。