最近在看《headfirst设计模式》一书,正开始学习设计模式不久。哎,就大四了,感觉落后了别人很多,伤感。现在也只能一步一个脚印,慢慢进步吧。
其实,前面已经学过工厂模式(抽象工厂和工厂方法)、观察者模式、单件模式、策略模式。今天把命令模式看完了,看懂并不是很困难,我想困难的是如何在实际中运用它们。因为它们的确会给我们的程序带来好处,比如说解耦。
恩,看完书,模仿了书本的例子写了下面这些,都是自己写,没看书了。例子几乎一样,本来想写空调遥控的,因为宿舍就有个,但还是觉得不一样吗?只是Light换成了空调。居于思维的局限,就没去再想其他的例子了。
命令模式中的对象主要包括:命令对象,调用者(命令请求者),命令接受者。[color=green]而命令模式就是就是通过命令对象,来实现命令请求者和命令接收者解耦。[/color]
1.类图(见底)
[img]file:///C:/Documents%20and%20Settings/rudy/桌面/CommandFactory.jpg[/img]
2.代码
我分了四个包,分别是:command,receiver,requester和test
a. receiver包----接收者包,在这里,light就是接收者,由它执行最后的动作。
b.command包----命令对象包。命令对象的execute方法执行了接受者(Light)的一个或者几个action,在由调用者control确定什么时候执行execute。
c.requester---指遥控器。通过setCommand()方法来获取ICommand,再由子类“决定”需要的哪个Command。通过几个Button方法,来决定什么时候执行相应的事件。接受者作出相应地反映(灯亮,灯灭)。[color=red]这里即可以体会到,遥控器和灯是解耦的。[/color]
d.测试类
e.控制台输出:
The light is open!
The light is close!
The light is open!
其实,前面已经学过工厂模式(抽象工厂和工厂方法)、观察者模式、单件模式、策略模式。今天把命令模式看完了,看懂并不是很困难,我想困难的是如何在实际中运用它们。因为它们的确会给我们的程序带来好处,比如说解耦。
恩,看完书,模仿了书本的例子写了下面这些,都是自己写,没看书了。例子几乎一样,本来想写空调遥控的,因为宿舍就有个,但还是觉得不一样吗?只是Light换成了空调。居于思维的局限,就没去再想其他的例子了。
命令模式中的对象主要包括:命令对象,调用者(命令请求者),命令接受者。[color=green]而命令模式就是就是通过命令对象,来实现命令请求者和命令接收者解耦。[/color]
1.类图(见底)
[img]file:///C:/Documents%20and%20Settings/rudy/桌面/CommandFactory.jpg[/img]
2.代码
我分了四个包,分别是:command,receiver,requester和test
a. receiver包----接收者包,在这里,light就是接收者,由它执行最后的动作。
package reciever;
public class Light {
//private static final int open = 0 ;
//private static final int close = 1 ;
public Light(){}
public void onlight(){
System.out.println("The light is open!") ;
}
public void offlight(){
System.out.println("The light is close!") ;
}
}
b.command包----命令对象包。命令对象的execute方法执行了接受者(Light)的一个或者几个action,在由调用者control确定什么时候执行execute。
package command;
/*
* Interface
*/
public interface ICommand {
void execute() ;
void undo() ;
}
package command;
import reciever.Light;
/*
* 灯关的命令对象
*/
public class LightOffCommand implements ICommand {
private Light light ;
public LightOffCommand(Light light){
this.light = light ;
}
public void execute() {
this.light.offlight() ;
}
public void undo() {
this.light.onlight() ;
}
}
package command;
import reciever.Light;
/*
* 灯开的命令对象
*/
public class LightOnCommand implements ICommand {
private Light light ;
public LightOnCommand(Light light){
this.light = light ;
}
public void execute() {
this.light.onlight() ;
}
public void undo() {
this.light.offlight() ;
}
}
package command;
/*
* 空对象。null object
*/
public class NoCommand implements ICommand {
//do nothing
public void execute() {
}
public void undo() {
}
}
c.requester---指遥控器。通过setCommand()方法来获取ICommand,再由子类“决定”需要的哪个Command。通过几个Button方法,来决定什么时候执行相应的事件。接受者作出相应地反映(灯亮,灯灭)。[color=red]这里即可以体会到,遥控器和灯是解耦的。[/color]
package requester;
import command.ICommand;
import command.NoCommand;
/*
* 调用者
*/
public class RemoteControl {
//undocommand变量用来村粗light的最后一次的状态
private ICommand oncommand ,offcommand ,undocommand ;
public RemoteControl(){
this.oncommand = new NoCommand() ;
this.offcommand = new NoCommand() ;
this.undocommand = new NoCommand() ;
}
public void setCommand(ICommand oncommand,ICommand offcommand){
this.oncommand = oncommand ;
this.offcommand = offcommand ;
}
public void onButtonWasPressed(){
oncommand.execute() ;
this.undocommand = this.oncommand ;
}
public void offButtonWasPressed(){
offcommand.execute() ;
this.undocommand = this.offcommand ;
}
public void undo(){
this.undocommand.undo() ;//取消操作
}
}
d.测试类
package test;
import command.ICommand;
import command.LightOffCommand;
import command.LightOnCommand;
import reciever.Light;
import requester.RemoteControl;
/*
* Test Class
*/
public class ControlTest {
public static void main(String[] args){
//receiver
Light light = new Light() ;
//command
ICommand lightOff = new LightOffCommand(light) ;
ICommand lightOn = new LightOnCommand(light) ;
//control
RemoteControl control = new RemoteControl() ;
control.setCommand(lightOn , lightOff) ;
control.onButtonWasPressed() ;
control.offButtonWasPressed() ;
control.undo() ;
//control.setCommand(lightOff) ;
//control.offButtonWasPressed() ;
}
}
e.控制台输出:
The light is open!
The light is close!
The light is open!