小蔡学Java

Java之设计模式 (十)

2023-09-20 17:56 1167 0 设计模式 设计模式命令模式

命令模式(Command Pattern)

  Java命令模式(Command Pattern)是一种行为型设计模式,它将请求封装成一个对象,从而使您可以将不同的请求参数化,将它们放入队列中,或者将请求记录在日志中,以及支持可撤销的操作。这种模式的主要组成部分包括命令接口、具体命令、调用者和接收者。

使用场景

  • 当需要将请求发送者和接收者解耦时,可以使用命令模式,因为命令对象充当请求发送者和接收者之间的媒介。

  • 当需要支持命令的撤销(undo)操作时,可以使用命令模式,因为每个命令对象都可以保存执行所需的状态。

  • 当需要在不同的时间指定、排队或记录请求时,可以使用命令模式,因为命令对象可以像其他对象一样传递、存储和操作。

  • 当需要实现日志记录功能时,可以使用命令模式,因为命令对象可以保存操作的历史记录,以便将来需要时进行检查和恢复。

  • 当需要将一组简单的操作组合成更复杂的操作时,可以使用命令模式,因为您可以将多个命令组合成一个复合命令。

代码实现

  假设有一个电视机,它具有开机、关机和切换频道等操作   定义命令接口和具体命令类

public interface Command {
    void execute();
}
public class TvOnCommand implements Command {
    private TV tv;
    
    public TvOnCommand(TV tv) {
        this.tv = tv;
    }
    
    public void execute() {
        tv.on();
    }
}
public class TvOffCommand implements Command {
    private TV tv;
    
    public TvOffCommand(TV tv) {
        this.tv = tv;
    }
    
    public void execute() {
        tv.off();
    }
}
public class TvChangeChannelCommand implements Command {
    private TV tv;
    private int channel;
    
    public TvChangeChannelCommand(TV tv, int channel) {
        this.tv = tv;
        this.channel = channel;
    }
    
    public void execute() {
        tv.changeChannel(channel);
    }
}

  定义接收者类

public class TV {
    public void on() {
        System.out.println("TV is on");
    }
    
    public void off() {
        System.out.println("TV is off");
    }
    
    public void changeChannel(int channel) {
        System.out.println("TV channel is changed to " + channel);
    }
}

  定义调用者类

public class RemoteController {
    private Command onCommand;
    private Command offCommand;
    private Command changeChannelCommand;
    
    public RemoteController(Command onCommand, Command offCommand, Command changeChannelCommand) {
        this.onCommand = onCommand;
        this.offCommand = offCommand;
        this.changeChannelCommand = changeChannelCommand;
    }
    
    public void turnOn() {
        onCommand.execute();
    }
    
    public void turnOff() {
        offCommand.execute();
    }
    
    public void changeChannel() {
        changeChannelCommand.execute();
    }
}

  使用命令模式

TV tv = new TV();
Command onCommand = new TvOnCommand(tv);
Command offCommand = new TvOffCommand(tv);
Command changeChannelCommand = new TvChangeChannelCommand(tv, 2);

RemoteController remoteController = new RemoteController(onCommand, offCommand, changeChannelCommand);
remoteController.turnOn(); // 输出 "TV is on"
remoteController.turnOff(); // 输出 "TV is off"
Command changeChannelCommand2 = new TvChangeChannelCommand(tv, 5);
RemoteController remoteController2 = new RemoteController(onCommand, offCommand, changeChannelCommand2);
remoteController2.changeChannel(); // 输出 "TV channel is changed to 5"

  上述代码中,RemoteController 充当了调用者角色,TvOnCommand、TvOffCommand 和 TvChangeChannelCommand 充当了具体命令角色,而 TV 充当了接收者角色。   当 RemoteController 调用某个命令的 execute() 方法时,具体命令将通过接收者TV来执行相应的操作。通过这种方式,调用者和接收者之间的耦合得以解除。

使用小结

  • 可以使用命令模式来实现撤销和恢复操作。例如,在编辑器中,可以使用命令模式来实现对文本的撤销和恢复操作。

  • 可以使用命令模式来实现日志记录。例如,在Web应用中,可以将每个请求封装成一个命令对象,并将命令对象记录到日志文件中。

  • 可以使用命令模式来实现消息队列。例如,在JMS(Java消息服务)中,可以将每个消息封装成一个命令对象,并将命令对象加入到消息队列中。

评论( 0 )

  • 博主 Mr Cai
  • 坐标 河南 信阳
  • 标签 Java、SpringBoot、消息中间件、Web、Code爱好者

文章目录