head first-----------浅谈命令设计模式
Command design pattern:命令模式------将请求封装成对象,这可以让你使用不同的请求、队列,或者日志请求来参数化其他对象。命令模式也可以支持撤销操作。
日常中,当我们需要将发出请求的对象和执行请求的对象解耦的时候,使用命令模式。
以下是一个命令模式的代码实现例子
package com.clark.commandpattern.abstractclass;
/**
* command interface
* @author Administrator
*
*/
public interface Command {
public void execute();
}
/**
* command interface
* @author Administrator
*
*/
public interface Command {
public void execute();
}
package com.clark.commandpattern.abstractclass;
import com.clark.commandpattern.NoCommand;
/**
* 实现遥控器
* @author Administrator
*
*/
public class RemoteControl {
public Command[] onCommands;//开命令
public Command[] offCommands;//关命令
public RemoteControl(){
//java中接口不能实例化,但是现在是属于数组类型了,故可以new
onCommands=new Command[7];
offCommands=new Command[7];
//初始化处于off status
Command noCommand=new NoCommand();
for (int i = 0; i <7; i++) {
onCommands[i]=noCommand;
offCommands[i]=noCommand;
}
}
//setCommand()方法必须有3个参数,指定插槽的位置、开得命令、关的命令
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(){
StringBuffer sb=new StringBuffer();
sb.append("\n--------- Remote Control -------\n");
for (int i = 0; i < onCommands.length; i++) {
sb.append("[slot"+i+"]"+onCommands[i].getClass().getName()
+" "+offCommands[i].getClass().getName()+"\n");
}
return sb.toString();
}
}
import com.clark.commandpattern.NoCommand;
/**
* 实现遥控器
* @author Administrator
*
*/
public class RemoteControl {
public Command[] onCommands;//开命令
public Command[] offCommands;//关命令
public RemoteControl(){
//java中接口不能实例化,但是现在是属于数组类型了,故可以new
onCommands=new Command[7];
offCommands=new Command[7];
//初始化处于off status
Command noCommand=new NoCommand();
for (int i = 0; i <7; i++) {
onCommands[i]=noCommand;
offCommands[i]=noCommand;
}
}
//setCommand()方法必须有3个参数,指定插槽的位置、开得命令、关的命令
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(){
StringBuffer sb=new StringBuffer();
sb.append("\n--------- Remote Control -------\n");
for (int i = 0; i < onCommands.length; i++) {
sb.append("[slot"+i+"]"+onCommands[i].getClass().getName()
+" "+offCommands[i].getClass().getName()+"\n");
}
return sb.toString();
}
}
package com.clark.commandpattern;
/**
* light class
* @author Administrator
*
*/
public class Light {
public String type;//Light type
public Light(String type){
this.type=type;
}
public void on(){
System.out.println(type+"Light is on");
}
public void off(){
System.out.println(type+"close light");
}
}
/**
* light class
* @author Administrator
*
*/
public class Light {
public String type;//Light type
public Light(String type){
this.type=type;
}
public void on(){
System.out.println(type+"Light is on");
}
public void off(){
System.out.println(type+"close light");
}
}
package com.clark.commandpattern;
import com.clark.commandpattern.abstractclass.Command;
/**
* light on command
* @author Administrator
*
*/
public class LightOffCommand implements Command {
public Light light;
@Override
public void execute() {
this.light.off();
}
public LightOffCommand(Light light){
this.light=light;
}
}
import com.clark.commandpattern.abstractclass.Command;
/**
* light on command
* @author Administrator
*
*/
public class LightOffCommand implements Command {
public Light light;
@Override
public void execute() {
this.light.off();
}
public LightOffCommand(Light light){
this.light=light;
}
}
package com.clark.commandpattern;
import com.clark.commandpattern.abstractclass.Command;
/**
* light on command
* @author Administrator
*
*/
public class LightOnCommand implements Command {
public Light light;
@Override
public void execute() {
this.light.on();
}
public LightOnCommand(Light light){
this.light=light;
}
}
import com.clark.commandpattern.abstractclass.Command;
/**
* light on command
* @author Administrator
*
*/
public class LightOnCommand implements Command {
public Light light;
@Override
public void execute() {
this.light.on();
}
public LightOnCommand(Light light){
this.light=light;
}
}
package com.clark.commandpattern;
public class Stereo {
String location;
public Stereo(String location) {
this.location = location;
}
public void on() {
System.out.println(location + " stereo is on");
}
public void off() {
System.out.println(location + " stereo is off");
}
public void setCD() {
System.out.println(location + " stereo is set for CD input");
}
public void setDVD() {
System.out.println(location + " stereo is set for DVD input");
}
public void setRadio() {
System.out.println(location + " stereo is set for Radio");
}
public void setVolume(int volume) {
// code to set the volume
// valid range: 1-11 (after all 11 is better than 10, right?)
System.out.println(location + " Stereo volume set to " + volume);
}
}
public class Stereo {
String location;
public Stereo(String location) {
this.location = location;
}
public void on() {
System.out.println(location + " stereo is on");
}
public void off() {
System.out.println(location + " stereo is off");
}
public void setCD() {
System.out.println(location + " stereo is set for CD input");
}
public void setDVD() {
System.out.println(location + " stereo is set for DVD input");
}
public void setRadio() {
System.out.println(location + " stereo is set for Radio");
}
public void setVolume(int volume) {
// code to set the volume
// valid range: 1-11 (after all 11 is better than 10, right?)
System.out.println(location + " Stereo volume set to " + volume);
}
}
package com.clark.commandpattern;
import com.clark.commandpattern.abstractclass.Command;
public class StereoOffCommand implements Command {
Stereo stereo;
public StereoOffCommand(Stereo stereo) {
this.stereo = stereo;
}
public void execute() {
stereo.off();
}
}
import com.clark.commandpattern.abstractclass.Command;
public class StereoOffCommand implements Command {
Stereo stereo;
public StereoOffCommand(Stereo stereo) {
this.stereo = stereo;
}
public void execute() {
stereo.off();
}
}
package com.clark.commandpattern;
import com.clark.commandpattern.abstractclass.Command;
public class StereoOnWithCDCommand implements Command {
Stereo stereo;
public StereoOnWithCDCommand(Stereo stereo) {
this.stereo = stereo;
}
public void execute() {
stereo.on();
stereo.setCD();
stereo.setVolume(11);
}
}
import com.clark.commandpattern.abstractclass.Command;
public class StereoOnWithCDCommand implements Command {
Stereo stereo;
public StereoOnWithCDCommand(Stereo stereo) {
this.stereo = stereo;
}
public void execute() {
stereo.on();
stereo.setCD();
stereo.setVolume(11);
}
}
package com.clark.commandpattern;
/**
* 汽车库门
* @author Administrator
*
*/
public class GarageDoor {
public String location;
public GarageDoor(String location){
this.location=location;
}
public void up(){
System.out.println(location+" garage door is up");
}
public void down(){
System.out.println(location+" garage door is down");
}
public void stop(){
System.out.println(location+" garage door is stop");
}
}
/**
* 汽车库门
* @author Administrator
*
*/
public class GarageDoor {
public String location;
public GarageDoor(String location){
this.location=location;
}
public void up(){
System.out.println(location+" garage door is up");
}
public void down(){
System.out.println(location+" garage door is down");
}
public void stop(){
System.out.println(location+" garage door is stop");
}
}
package com.clark.commandpattern;
import com.clark.commandpattern.abstractclass.Command;
public class GarageDownCommand implements Command{
public GarageDoor garageDoor;
public void execute() {
this.garageDoor.stop();
this.garageDoor.down();
}
public GarageDownCommand(GarageDoor garageDoor){
this.garageDoor=garageDoor;
}
}
import com.clark.commandpattern.abstractclass.Command;
public class GarageDownCommand implements Command{
public GarageDoor garageDoor;
public void execute() {
this.garageDoor.stop();
this.garageDoor.down();
}
public GarageDownCommand(GarageDoor garageDoor){
this.garageDoor=garageDoor;
}
}
package com.clark.commandpattern;
import com.clark.commandpattern.abstractclass.Command;
public class GarageUpCommand implements Command{
public GarageDoor garageDoor;
public void execute() {
this.garageDoor.up();
}
public GarageUpCommand(GarageDoor garageDoor){
this.garageDoor=garageDoor;
}
}
import com.clark.commandpattern.abstractclass.Command;
public class GarageUpCommand implements Command{
public GarageDoor garageDoor;
public void execute() {
this.garageDoor.up();
}
public GarageUpCommand(GarageDoor garageDoor){
this.garageDoor=garageDoor;
}
}
package com.clark.commandpattern;
import com.clark.commandpattern.abstractclass.Command;
public class NoCommand implements Command {
@Override
public void execute() {
System.out.println("now status is off");
}
}
import com.clark.commandpattern.abstractclass.Command;
public class NoCommand implements Command {
@Override
public void execute() {
System.out.println("now status is off");
}
}
package com.clark.commandpattern;
import com.clark.commandpattern.abstractclass.RemoteControl;
/**
* 测试遥控器类
* @author Administrator
*
*/
public class RemoteLoader {
public static void main(String[] args) {
//product a remote control
RemoteControl remoteControl=new RemoteControl();
//将所有的命令对象放置在合适的位置
Light livingRoomLight=new Light("living Room");
Light kitchenLight=new Light("Kitchen");
Stereo stereo=new Stereo("Living Room");
GarageDoor garageDoor=new GarageDoor("");
//set Command
LightOnCommand livingRoomLightOn=new LightOnCommand(livingRoomLight);
LightOffCommand livingRoomLightOff=new LightOffCommand(livingRoomLight);
LightOnCommand kitchenLightOn=new LightOnCommand(kitchenLight);
LightOffCommand kitchenLightOff=new LightOffCommand(kitchenLight);
StereoOnWithCDCommand stereoOnWithCd=new StereoOnWithCDCommand(stereo);
StereoOffCommand stereoOffWithCd=new StereoOffCommand(stereo);
GarageUpCommand garageDoorUp=new GarageUpCommand(garageDoor);
GarageDownCommand garageDoorDown=new GarageDownCommand(garageDoor);
//loader to remote control
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
remoteControl.setCommand(2, stereoOnWithCd, stereoOffWithCd);
remoteControl.setCommand(3, garageDoorUp, garageDoorDown);
System.out.println(remoteControl.toString());
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(2);
remoteControl.onButtonWasPushed(3);
remoteControl.offButtonWasPushed(3);
}
}
import com.clark.commandpattern.abstractclass.RemoteControl;
/**
* 测试遥控器类
* @author Administrator
*
*/
public class RemoteLoader {
public static void main(String[] args) {
//product a remote control
RemoteControl remoteControl=new RemoteControl();
//将所有的命令对象放置在合适的位置
Light livingRoomLight=new Light("living Room");
Light kitchenLight=new Light("Kitchen");
Stereo stereo=new Stereo("Living Room");
GarageDoor garageDoor=new GarageDoor("");
//set Command
LightOnCommand livingRoomLightOn=new LightOnCommand(livingRoomLight);
LightOffCommand livingRoomLightOff=new LightOffCommand(livingRoomLight);
LightOnCommand kitchenLightOn=new LightOnCommand(kitchenLight);
LightOffCommand kitchenLightOff=new LightOffCommand(kitchenLight);
StereoOnWithCDCommand stereoOnWithCd=new StereoOnWithCDCommand(stereo);
StereoOffCommand stereoOffWithCd=new StereoOffCommand(stereo);
GarageUpCommand garageDoorUp=new GarageUpCommand(garageDoor);
GarageDownCommand garageDoorDown=new GarageDownCommand(garageDoor);
//loader to remote control
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
remoteControl.setCommand(2, stereoOnWithCd, stereoOffWithCd);
remoteControl.setCommand(3, garageDoorUp, garageDoorDown);
System.out.println(remoteControl.toString());
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(2);
remoteControl.onButtonWasPushed(3);
remoteControl.offButtonWasPushed(3);
}
}