Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step
https://juejin.cn/column/7244788137410560055
1.91k stars 232 forks source link

命令模式 #460

Open LittlebearHat opened 1 year ago

LittlebearHat commented 1 year ago
class Receiver {
  //我是接收者
  execute() {
    console.log("接收者执行请求");
  }
}
class Command {
  //我是命令对象
  constructor(receiver) {
    this.receiver = receiver;
  }
  execute() {
    // 调用接收者对应接口执行
    console.log("命令对象处理发布者请求");
    this.receiver.execute();
  }
}
class Invoker {
  //我是发布者
  constructor(command) {
    this.command = command;
  }
  invoke() {
    // 调用命令
    console.log("发布者发布请求");
    this.command.execute();
  }
}
const receiver = new Receiver();
const command = new Command(receiver);
const invoker = new Invoker(command);
invoker.invoke();