桥+命令模式

Bridge+command pattern

我正在阅读命令模式,我看到来自不同站点的示例似乎使用 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);

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

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

编辑:

如果我拿第2个例子,去掉命令部分,这不是桥接模式吗?

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 非常相似并遵循该定义。
它不使用桥接模式。

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