设计模式(命令模式)

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


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);
		 
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
命令模式是一种行为型设计模式,它将请求封装成一个对象,从而使得请求的发送者和接收者解耦。在命令模式中,请求以命令的形式包裹在对象中,并传递给调用对象。调用对象寻找可以处理该命令的合适的对象,并将命令传递给相应的对象,该对象执行命令。 在C语言中,可以使用函数指针来实现命令模式。具体步骤如下: 1. 定义一个命令接口,该接口包含一个执行命令的方法。 2. 创建具体的命令类,实现命令接口,并在执行方法中调用相应的函数。 3. 创建一个调用者类,该类包含一个命令对象,并提供一个执行命令的方法。 4. 在调用者类中,将命令对象传递给相应的对象,并调用命令对象的执行方法。 下面是一个简单的示例代码: ```c #include <stdio.h> // 定义命令接口 typedef struct { void (*execute)(void); } Command; // 创建具体的命令类 typedef struct { Command command; void (*function)(void); } ConcreteCommand; void concreteCommand_execute(void) { printf("执行具体的命令\n"); } // 创建调用者类 typedef struct { Command *command; void (*setCommand)(Command *command); void (*executeCommand)(void); } Invoker; void invoker_setCommand(Command *command) { Invoker *invoker = (Invoker *)command; invoker->command = command;} void invoker_executeCommand(void) { Invoker *invoker = (Invoker *)invoker->command; invoker->command->execute(); } int main() { // 创建具体的命令对象 ConcreteCommand concreteCommand; concreteCommand.command.execute = concreteCommand_execute; concreteCommand.function = concreteCommand_execute; // 创建调用者对象 Invoker invoker; invoker.setCommand((Command *)&concreteCommand); invoker.executeCommand(); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值