javascript - 桥+命令模式

标签 javascript design-patterns command bridge

我正在阅读命令模式,并且看到来自不同站点的示例,这些示例似乎使用桥+命令模式来展示命令模式。

首先,来自维基百科:https://en.wikipedia.org/wiki/Command_pattern 、命令模式的定义:

The command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

因此,根据这个定义,命令模式看起来非常简单,阅读位于此处的书:https://addyosmani.com/resources/essentialjsdesignpatterns/book/#commandpatternjavascript ,这个例子就是这样做的。

(function(){

  var carManager = {

    // request information
    requestInfo: function( model, id ){
      return "The information for " + model + " with ID " + id + " is foobar";
    },

    // purchase the car
    buyVehicle: function( model, id ){
      return "You have successfully purchased Item " + id + ", a " + model;
    },

    // arrange a viewing
    arrangeViewing: function( model, id ){
      return "You have successfully booked a viewing of " + model + " ( " + id + " ) ";
    }

  };

  carManager.execute = function ( name ) {
    return carManager[name] && carManager[name].apply( carManager, [].slice.call(arguments, 1) );
  };

  console.log(carManager.execute( "arrangeViewing", "Ferrari", "14523" ));
  console.log(carManager.execute( "requestInfo", "Ford Mondeo", "54323" ));
  console.log(carManager.execute( "requestInfo", "Ford Escort", "34232" ));
  console.log(carManager.execute( "buyVehicle", "Ford Escort", "34232" ));

})();

这个例子中没有多余的东西,我只看到了命令模式。然而,回到维基百科,他们使用以下示例来展示命令模式:

class Switch {
  constructor() {
    this._commands = [];
  }

  storeAndExecute(command) {
    this._commands.push(command);
    command.execute();
  }
}

class Light {
  turnOn() { console.log('turn on') }
  turnOff() { console.log('turn off') }
}

class FlipDownCommand {
  constructor(light) {
    this._light = light;
  }

  execute() {
    this._light.turnOff();
  }
}

class FlipUpCommand {
  constructor(light) {
    this._light = light;
  }

  execute() {
    this._light.turnOn();
  }
}

var light = new Light();
var switchUp = new FlipUpCommand(light);
var switchDown = new FlipDownCommand(light);
var s = new Switch();

s.storeAndExecute(switchUp);
s.storeAndExecute(switchDown);

当我看到上面的示例时,我立即看到桥接模式,然后看到命令模式,因为它们正在存储命令,然后立即调用命令。

我的问题是这样的;我认为维基百科示例使用桥+命令模式来展示命令模式是否正确?

编辑:

如果我采用第二个示例,并删除命令部分,这不是桥接模式吗?

class Light {
  turnOn() { console.log('turn on') }
  turnOff() { console.log('turn off') }
}

class FlipDownCommand {
  constructor(light) {
    this._light = light;
  }

  execute() {
    this._light.turnOff();
  }
}

class FlipUpCommand {
  constructor(light) {
    this._light = light;
  }

  execute() {
    this._light.turnOn();
  }
}

var light = new Light();
var switchUp = new FlipUpCommand(light);
var switchDown = new FlipDownCommand(light);

switchUp.execute();
switchDown.execute();

最佳答案

首先,我发现 js 示例中 Addy Osmani 的解释与 GoF 的原始解释(以及维基百科的定义)有点不同。

来自 GoF 命令模式页面:

The command pattern is a design pattern that enables all of the information for a request to be contained within a single object. The command can then be invoked as required, often as part of a batch of queued commands with rollback capabilities.

这意味着命令对象应包含一个无参数 Execute 方法(有时还包含一个 Undo)。命令的参数应该是 已经包含在其中。该命令可以传递到调用程序、排队并在以后随时执行。
维基百科的示例与原始 GoF 非常相似,并遵循该定义。
它不使用桥接模式。

桥接模式用于添加抽象级别并向消费者隐藏服务的技术具体实现。 桥接器可以具有由其接口(interface)定义的许多操作。

关于javascript - 桥+命令模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42429916/

相关文章:

javascript - 异步函数返回一个promise。如何使promise值全局化?

javascript - jquery如果一个元素没有被点击

c++ - 如何在不生成大量测试客户端应用程序的情况下切换元素和容器类型以进行基准测试?

command - Selenium IDE 在弹出窗口中的 clickAndWait 命令上卡住

javascript - 删除类更改列表大小

javascript - 烦人的 React 错误... "TypeError: Cannot read property ' My_Items' of null”

ios - 沿路径绘制图案

c# - 在责任链模式中的对象之间传递数据

linux - 用 n 步旋转文本文件中的行

c# - 如何将 Command 构造函数的execute 和canExecute 参数包含到Xamarin.Forms 中的单个方法中?