java 命令模式

java 命令模式

命令模式:将“请求“封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。

实例如下:

interface Command { // 命令
	public void execute(); // 执行
}
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" ); }
}
class Light { // 电灯
	public Light() {}	
	public void on() { System.out.println( "Light is on" ); }
	public void off() { System.out.println( "Light is off" ); }	
}
class GarageDoorOpenCommand implements Command { // 车库门打开命令
	GarageDoor garageDoor;
	public GarageDoorOpenCommand( GarageDoor garageDoor ) {
		this.garageDoor = garageDoor;
	}
	public void execute() { garageDoor.up(); }
}
class LightOffCommand implements Command { // 熄灯命令
	Light light; 
	public LightOffCommand( Light light ) { this.light = light; } 
	public void execute() { light.off(); }	
}	
class LightOnCommand implements Command { // 开灯命令
	Light light;  
	public LightOnCommand( Light light ) { this.light = light; }
	public void execute() { light.on(); }	
}
class SimpleRemoteControl { // 简单遥控器
	Command slot; // 命令插槽
	public SimpleRemoteControl() {} 
	public void setCommand( Command command ) { slot = command; }
	public void buttonWasPressed() { slot.execute(); }	
}
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();
    }	
}
interface Command { public void execute(); }	
class CeilingFan { // 吊扇
	String location = "";
	int level;
	public static final int HIGH = 2;
	public static final int MEDIUM = 1;
	public static final int LOW = 0; 
	public CeilingFan( String location ) { this.location = location; }
	public void high() { // 高档
		// turns the ceiling fan on to high
		level = HIGH;
		System.out.println( location + " ceiling fan is on high" ); 
	} 
	public void medium() { // 中档
		// turns the ceiling fan on to medium
		level = MEDIUM;
		System.out.println( location + " ceiling fan is on medium" );
	}
	public void low() { // 低档
		// turns the ceiling fan on to low
		level = LOW;
		System.out.println( location + " ceiling fan is on low" );
	} 
	public void off() { // 关掉
		// turns the ceiling fan off
		level = 0;
		System.out.println( location + " ceiling fan is off" );
	} 
	public int getSpeed() { return level; } // 获取速度
}
class GarageDoor { // 车库门
	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 Stopped" ); }
	public void lightOn() { System.out.println( location + " garage light is on" ); }
	public void lightOff() { System.out.println( location + " garage light is off" ); }	
}
class Hottub { // 热浴缸
	boolean on;
	int temperature; // 温度
	public Hottub() {}
	public void on() { on = true; }
	public void off() { on = false; }
	public void bubblesOn() {
		if ( on ) { System.out.println( "Hottub is bubbling!" ); } // 热水浴缸找开
	}	
	public void bubblesOff() {
		if ( on ) { System.out.println( "Hottub is not bubbling" ); }	// 热水浴缸关掉	
	}
	public void jetsOn() {
		if ( on ) { System.out.println( "Hottub jets are on" ); } // 热水浴缸喷头打开	
	}
	public void jetsOff() {
		if ( on ) { System.out.println( "Hottub jets are off" ); } // 热水浴缸喷头关闭		
	}
	public void setTemperature( int temperature ) {
		this.temperature = temperature;
	}
	public void heat() { // 加热
		temperature = 105;
		System.out.println( "Hottub is heating to a steaming 105 degrees" );
	}
	public void cool() { // 冷却
		temperature = 98;
		System.out.println( "Hottub is cooling to 98 degrees" );
	}
}
class Light {
	String location = "";
	public Light( String location ) { this.location = location; }
	public void on() { System.out.println( location + " light is on" ); }
	public void off() { System.out.println( location + " light is off" ); }
}
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 );
	}
}
class TV { // 电视
	String location;
	int channel; // 频道
	public TV( String location ) { this.location = location; }
	public void on() { System.out.println( "TV is on" ); }
	public void off() { System.out.println( "TV is off" ); }
	public void setInputChannel() { // 设置频道
		this.channel = 3;
		System.out.println( "Channel is set for VCR" ); // 录像机
	}
}
class CeilingFanOffCommand implements Command { // 吊扇关闭命令
	CeilingFan ceilingFan;
	public CeilingFanOffCommand( CeilingFan ceilingFan ) {
		this.ceilingFan = ceilingFan;
	}
	public void execute() { ceilingFan.off(); }
}
class CeilingFanOnCommand implements Command { // 吊扇打开命令
	CeilingFan ceilingFan;
	public CeilingFanOnCommand(CeilingFan ceilingFan ) {
		this.ceilingFan = ceilingFan;
	}
	public void execute() { ceilingFan.high(); }
}
class GarageDoorDownCommand implements Command { // 车库门关闭命令
	GarageDoor garageDoor;
	public GarageDoorDownCommand( GarageDoor garageDoor) {
		this.garageDoor = garageDoor;
	}
	public void execute() { garageDoor.down(); }
}
class GarageDoorUpCommand implements Command { // 车库门打开命令
	GarageDoor garageDoor;
	public GarageDoorUpCommand(GarageDoor garageDoor) {
		this.garageDoor = garageDoor;
	}
	public void execute() { garageDoor.up(); }	
}
class HottubOffCommand implements Command { // 热管关闭命令
	Hottub hottub;
	public HottubOffCommand( Hottub hottub ) { this.hottub = hottub; }
	public void execute() { hottub.cool(); hottub.off(); }
}
class HottubOnCommand implements Command { // 热管打开命令
	Hottub hottub;
	public HottubOnCommand( Hottub hottub ) { this.hottub = hottub; }
	public void execute() {
		hottub.on();
		hottub.heat();
		hottub.bubblesOn();
	}
}
class LightOffCommand implements Command {  // 关灯命令
	Light light; 
	public LightOffCommand( Light light ) { this.light = light; }
	public void execute() { light.off(); }
}
class LightOnCommand implements Command { // 开灯命令
	Light light;
	public LightOnCommand( Light light ) { this.light = light; }
	public void execute() { light.on(); }
}
class LivingroomLightOffCommand implements Command { // 客厅灯光关闭命令
	Light light;
	public LivingroomLightOffCommand( Light light ) { this.light = light; }
	public void execute() { light.off(); }
}
class LivingroomLightOnCommand implements Command { // // 客厅灯光打开命令
	Light light;
	public LivingroomLightOnCommand( Light light ) { this.light = light; }
	public void execute() { light.on(); }
}
class NoCommand implements Command { // 无命令
	public void execute() { }
}
class StereoOffCommand implements Command { // 音响关闭命令
	Stereo stereo; 
	public StereoOffCommand( Stereo stereo ) { this.stereo = stereo; }
	public void execute() { stereo.off(); }
}
class StereoOnWithCDCommand implements Command { // 音响打开命令
	Stereo stereo; 
	public StereoOnWithCDCommand( Stereo stereo ) { this.stereo = stereo; }
	public void execute() {
		stereo.on();
		stereo.setCD();
		stereo.setVolume( 11 );
	}
}
class RemoteControl { // 遥控
	Command[] onCommands;
	Command[] offCommands; 
	public RemoteControl() {
		onCommands = new Command[7];
		offCommands = new Command[7]; 
		Command noCommand = new NoCommand();
		for ( int i = 0; i < 7; i ++ ) {
			onCommands[i] = noCommand;
			offCommands[i] = noCommand;
		}
	}  
	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 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();
	}
}
class RemoteLoader { // 遥控加载程序
	public static void main( String[] args ) {
		RemoteControl remoteControl = new RemoteControl(); 
		Light livingRoomLight = new Light( "Living Room" );
		Light kitchenLight = new Light( "Kitchen" );
		CeilingFan ceilingFan= new CeilingFan( "Living Room" );
		GarageDoor garageDoor = new GarageDoor( "" );
		Stereo stereo = new Stereo( "Living Room" );  
		LightOnCommand livingRoomLightOn = new LightOnCommand( livingRoomLight );				
		LightOffCommand livingRoomLightOff = new LightOffCommand( livingRoomLight );				
		LightOnCommand kitchenLightOn = new LightOnCommand( kitchenLight );				
		LightOffCommand kitchenLightOff = new LightOffCommand( kitchenLight );
		CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand( ceilingFan );
		CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand( ceilingFan );
		GarageDoorUpCommand garageDoorUp = new GarageDoorUpCommand( garageDoor );
		GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand( garageDoor );
		StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand( stereo );
		StereoOffCommand  stereoOff = new StereoOffCommand( stereo );
		remoteControl.setCommand( 0, livingRoomLightOn, livingRoomLightOff );
		remoteControl.setCommand( 1, kitchenLightOn, kitchenLightOff );
		remoteControl.setCommand( 2, ceilingFanOn, ceilingFanOff );
		remoteControl.setCommand( 3, stereoOnWithCD, stereoOff );
		System.out.println( remoteControl );
		remoteControl.onButtonWasPushed( 0 );
		remoteControl.offButtonWasPushed( 0 );
		remoteControl.onButtonWasPushed( 1 );
		remoteControl.offButtonWasPushed( 1 );
		remoteControl.onButtonWasPushed( 2 );
		remoteControl.offButtonWasPushed( 2 );
		remoteControl.onButtonWasPushed( 3 );
		remoteControl.offButtonWasPushed( 3 );
	}
}
----------------------------------------------------------
E:\java>java Test0610

------ Remote Control -------
[slot 0] LightOnCommand    LightOffCommand
[slot 1] LightOnCommand    LightOffCommand
[slot 2] CeilingFanOnCommand    CeilingFanOffCommand
[slot 3] StereoOnWithCDCommand    StereoOffCommand
[slot 4] NoCommand    NoCommand
[slot 5] NoCommand    NoCommand
[slot 6] NoCommand    NoCommand

Living Room light is on
Living Room light is off
Kitchen light is on
Kitchen light is off
Living Room ceiling fan is on high
Living Room ceiling fan is off
Living Room stereo is on
Living Room stereo is set for CD input
Living Room Stereo volume set to 11
Living Room stereo is off
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值