您的位置:首页 > 其它

设计模式之六:命令模式(简单实现(餐厅点餐模拟流程))

2012-10-01 11:27 696 查看
工程名称:

命令接口:CommandInSimple 下载目录:http://www.cnblogs.com/jrsmith/admin/Files.aspx ,CommandInSimple.zip



package com.jyu.command;

/**命令接口*/
public interface Command {

public void execute();
}


打开电灯的具体命令对象:

package com.jyu.command;

public class LightOnCommand implements Command {

Light light;

public LightOnCommand(Light light) {
this.light = light;
}

@Override
public void execute() {
light.on();
}

}


遥控器:

package com.jyu.command;

public class RemoteCOntrolTest {

/**
* @param args
*/
public static void main(String[] args) {

SimpleRemoteControl remote = new SimpleRemoteControl();
Light light = new Light();
LightOnCommand lightOn = new LightOnCommand(light);

remote.setCommand(lightOn);
remote.buttonWasPressed();
}

}


利用遥控器开灯的简单测试:

package com.jyu.command;

/**遥控器*/
public class SimpleRemoteControl {

Command slot;

public SimpleRemoteControl() {    }

public void setCommand(Command command) {
this.slot = command;
}

public void buttonWasPressed(){
slot.execute();
}

}


package com.jyu.command;

public class Light {

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

public void off(){
System.out.println("The Light is off...");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: