重要思想:命令模式

本文介绍命令模式的设计理念及其在遥控器等应用场景中的实现细节。通过具体的Java代码示例,展示了如何利用命令模式封装请求,并实现请求的发送者与接收者的解耦。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一个命令对象
--实现命令接口
public interface Command {
    
public void execute();
}


--实现一个打开电灯的命令
public class LightOnCommand implemetns Command {
    Light light;

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

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

}


使用命令对象
public class SimpleRemoteControl {
    Command slot;

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

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

}


遥控器使用的简单测试
public class RemoteControlTest {
    
public static void main(String[] args) {
        SimpleRemoteContron remote 
= new SimpleRemoteControl();
        Light light 
= new Light();
        LightOnCommand lightOn 
= new LightOnCommand(light);

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

}


Sharpen your pencil
public class GarageDoorOpenComand implements Command {
    GarageDoor 
= garageDoor;

    
public GarageDoorOpenCommand(GarageDoor garageDoor) {
        
this.garageDoor = garageDoor;
    }

    
public void execute() {
        garageDoor.up();
    }

}


public class RemoteControlTest {
    
public static void main(String[] args) {
        SimpleRemoteContronl 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();
    }

}


定义命令模式
    命令模式:将“请求”封装成对象,以便使用不同的请求,队列或者日志来参数化
其他对象。命令模式也支持可撤销的操作。
命令模式的设计如何支持请求调用者和请求接收者之间的解耦?

实现遥控器
public 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 stringBuffer 
= new StringBuffer();
        stringBuffer.append(
" ----------Remote Control---------");
        
for (int i=0; i<onCommands.length; i++{
            stringBuffer.append(
"[slot" + i + "]" + onCommands[i].getClass().getName() 
                
+ "   " + offCommands[i].getClass().getName() + " ");
        }

        
return stringBuffer.toString();
    }

}


实现命令
public class LightOffCommand implements Command {
    Light light;

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


    
public void execute() {
        light.off();
    }

}


public class StereoOnWithCDCommand implements Command {
    Stereo stereo;

    
public StereoOnWithCDCommand(Stereo stereo) {
        
this.stereo = stereo;
    }


    
public void execute() {
        stereo.on();
        stereo.setCD();
        stereo.setVolume(
11);
    }

}


逐步测试遥控器
public 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);

        ……
    }

}


Undo的2种;Party模式“宏”;队列请求;日志请求……
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值