设计模式(命令模式)

命令模式,简单一句话概括就是:把调用的方法封装起来。既然是“命令”,那么就有发送命令者(请求对象)和执行命令者(执行对象),两者均不在乎是拥有的是什么对象,只要实现了命令接口就可以了;遵循了 针对接口编程,不针对实现编程这一原则。


Command.java

public interface Command{
    public void execute();
}

LightOnCommand.java

public class LightOnCommand implements Command{
    Light light;
   
    public LightOnCommand(Light light){
         this.light=light;
	}
	
	public void execute(){
	     light.on();
	}
}


Light.java
public class Light{
    public void on(){
	     System.out.println("light is on");
	}
	
	public void off(){
	     System.out.println("light is off");
	}

} 


SimpleRemoteControl.java
public class SimpleRemoteControl{
    Command slot;
	
	public SimpleRemoteControl(){}
	
	public void setCommand(Command command){
	     slot=command;
	}
	
	public void buttonWasPressed(){
	     slot.execute();
	}
}


GarageDoorOpen.java

public class GarageDoorOpen{
    public void on(){
	   System.out.println("the garage door is open!");
	}

}



GarageDoorOpenCommand.java

public class GarageDoorOpenCommand implements Command{
    GarageDoorOpen gdp;
	
	public GarageDoorOpenCommand(GarageDoorOpen gdp){
	     this.gdp=gdp;
	}
	
	public void execute(){
	     gdp.on();
	}
 
}



RemoteControlTest.java
public class RemoteControlTest{
    public static void main(String[] args){
	     SimpleRemoteControl remote = new SimpleRemoteControl();
		 Light light = new Light();
		 LightOnCommand lightOn = new LightOnCommand(light);
		 
		 GarageDoorOpen gdp = new  GarageDoorOpen();
		 GarageDoorOpenCommand gdpc= new GarageDoorOpenCommand(gdp);
		 
		 remote.setCommand(gdpc);
		 remote.buttonWasPressed();
	}
}


修改下:


Light.java

public class Light{
   
    public  Light(String s){
	    
	}
  
    public void on(){
	     System.out.println("light is on");
	}
	
	public void off(){
	     System.out.println("light is off");
	}

} 

LightOffCommand.java

public class LightOffCommand implements Command{
    Light light;
   
    public LightOffCommand(Light light){
         this.light=light;
	}
	
	public void execute(){
	     light.off();
	}
}

StereoOnWithCDCommand.java

public class StereoOnWithCDCommand implements Command{
    Stereo stereo;
	
	public StereoOnWithCDCommand(Stereo stereo){
	     this.stereo=stereo;
	}
	
	public void execute(){
	    stereo.on();
		stereo.setCD("CD");
		stereo.setVolume(11);
	}
}


Stereo.java

public class Stereo{
     String cd;
	 int volume;
     
	 public Stereo(String s){

	 }
	 
     public void on(){
	    System.out.print("Stereo is on");
	 }
	 
	 public void off(){
	     System.out.println("Stereo is off");
	 }
	 public void setCD(String cd){
	     this.cd=cd;
	 }
	 
	 public void setVolume(int volume){
	     this.volume=volume;
	 }
}


StereoOffCommand.java

public class StereoOffCommand implements Command{
    Stereo stereo;
	
	public StereoOffCommand(Stereo stereo){
	     this.stereo=stereo;
	}
	
	public void execute(){
	    stereo.off();
		stereo.setCD("");
		stereo.setVolume(0);
	}
}


NoCommand.java

public class NoCommand implements Command{
    public void execute(){}
}



GarageDoorOpen.java

public class GarageDoorOpen{
    public GarageDoorOpen(String s){

	}
    public void on(){
	   System.out.println("the garage door is open!");
	}

}


SimpleRemoteControl.java

public class SimpleRemoteControl{
    Command[] onCommands;
	Command[] offCommands;
	
	public SimpleRemoteControl(){
	    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 onButtonWasPressed(int slot){
	     onCommands[slot].execute();
	}
	
	public void offButtonWasPressed(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();
	}
}


RemoteControlTest.java

public class RemoteControlTest{
    public static void main(String[] args){
	     SimpleRemoteControl remote = new SimpleRemoteControl();
		 Light livingRoomLight = new Light("living Room");
		 Light kitchenLight = new Light("kitchen");
		 
		
		 GarageDoorOpen gdp = new  GarageDoorOpen("");
		 Stereo stereo = new Stereo("Living Room");
		 
		  LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
		 LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);

		 
		 GarageDoorOpenCommand gdpc= new GarageDoorOpenCommand(gdp);
		 
		 StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);
		 StereoOffCommand stereoOff = new StereoOffCommand(stereo);
		 
		 
		 remote.setCommand(0,livingRoomLightOn,livingRoomLightOff);
		 remote.setCommand(1,stereoOnWithCD,stereoOff);
         
		 System.out.println(remote);
		 
		 remote.onButtonWasPressed(0);
		 remote.offButtonWasPressed(0);
		 remote.onButtonWasPressed(1);
		 remote.offButtonWasPressed(1);
		 
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值