命令模式
2011-12-26 19:28
204 查看
命令模式:
(1)把所要完成的所有类,设计出统一的接口及操作函数。
(2)各个操作类实现上面的接口。并且实现类本身的操作。
(3)定义一个类,把所有的实现的类通过统一的接口来实现本身的操作。
例子:
(1)统一接口
public interface Command {
public void execute();
}
(2)功能的实现
功能类:
public class Light {
private String name;
public Light(String name)
{
this.name=name;
}
public void on()
{
System.out.println(name+" Light On!");
}
public void off()
{
System.out.println(name+" Light Off!");
}
}
实现接口类:
public class LightOffCommand implements Command{
Light light;
public void execute() {
// TODO Auto-generated method stub
light.off();
}
public LightOffCommand(Light light)
{
this.light=light;
}
}
(3)
/**
* 命令模式就是提供一个共同的接口,对共同的接口进行不同的功能的操作
* @author Administrator
*
*/
public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
Command noCommad=new NoCommand();//自己去实现一个空的类
Command undoCommand;//加入这个参数是非常的刁的可以用于记录历史数据用于恢复过程中。
public RemoteControl()
{
onCommands=new Command[7];
offCommands=new Command[7];
for(int i=0;i<7;i++)
{
onCommands[i]=noCommad;
offCommands[i]=noCommad;
}
}
public void setCommand(int slot,Command onCommand,Command offCommand)
{
onCommands[slot]=onCommand;
offCommands[slot]=offCommand;
}
public void onButtonWasPushed(int slot)
{
onCommands[slot].execute();
}
public void offButtonWasPushed(int slot)
{
offCommands[slot].execute();
}
public String toString()//这个放很刁,可以覆盖toString方法进行输出
{
StringBuffer stringBuff=new StringBuffer();
stringBuff.append("\n----------Remote Control---------\n");
for(int i=0;i<onCommands.length;i++)
{
stringBuff.append("[slot "+i+"]"+onCommands[i].getClass().getName()+" "
+offCommands[i].getClass().getName()+"\n");
}
return stringBuff.toString();
}
}
测试
public class RemoteLoader {
public static void main(String [] args)
{
RemoteControl remoteControl=new RemoteControl();
Light livingRoomLight=new Light("Living Room");
LightOnCommand livingRoomLighOn=new LightOnCommand(livingRoomLight);//自己去实现这个类
LightOffCommand livingRoomLighOff=new LightOffCommand(livingRoomLight);
remoteControl.setCommand(0,livingRoomLighOn,livingRoomLighOff);
System.out.println(remoteControl);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.offButtonWasPushed(1);
}
}
(1)把所要完成的所有类,设计出统一的接口及操作函数。
(2)各个操作类实现上面的接口。并且实现类本身的操作。
(3)定义一个类,把所有的实现的类通过统一的接口来实现本身的操作。
例子:
(1)统一接口
public interface Command {
public void execute();
}
(2)功能的实现
功能类:
public class Light {
private String name;
public Light(String name)
{
this.name=name;
}
public void on()
{
System.out.println(name+" Light On!");
}
public void off()
{
System.out.println(name+" Light Off!");
}
}
实现接口类:
public class LightOffCommand implements Command{
Light light;
public void execute() {
// TODO Auto-generated method stub
light.off();
}
public LightOffCommand(Light light)
{
this.light=light;
}
}
(3)
/**
* 命令模式就是提供一个共同的接口,对共同的接口进行不同的功能的操作
* @author Administrator
*
*/
public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
Command noCommad=new NoCommand();//自己去实现一个空的类
Command undoCommand;//加入这个参数是非常的刁的可以用于记录历史数据用于恢复过程中。
public RemoteControl()
{
onCommands=new Command[7];
offCommands=new Command[7];
for(int i=0;i<7;i++)
{
onCommands[i]=noCommad;
offCommands[i]=noCommad;
}
}
public void setCommand(int slot,Command onCommand,Command offCommand)
{
onCommands[slot]=onCommand;
offCommands[slot]=offCommand;
}
public void onButtonWasPushed(int slot)
{
onCommands[slot].execute();
}
public void offButtonWasPushed(int slot)
{
offCommands[slot].execute();
}
public String toString()//这个放很刁,可以覆盖toString方法进行输出
{
StringBuffer stringBuff=new StringBuffer();
stringBuff.append("\n----------Remote Control---------\n");
for(int i=0;i<onCommands.length;i++)
{
stringBuff.append("[slot "+i+"]"+onCommands[i].getClass().getName()+" "
+offCommands[i].getClass().getName()+"\n");
}
return stringBuff.toString();
}
}
测试
public class RemoteLoader {
public static void main(String [] args)
{
RemoteControl remoteControl=new RemoteControl();
Light livingRoomLight=new Light("Living Room");
LightOnCommand livingRoomLighOn=new LightOnCommand(livingRoomLight);//自己去实现这个类
LightOffCommand livingRoomLighOff=new LightOffCommand(livingRoomLight);
remoteControl.setCommand(0,livingRoomLighOn,livingRoomLighOff);
System.out.println(remoteControl);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.offButtonWasPushed(1);
}
}