首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Bridge+command模式

Bridge+command模式
EN

Stack Overflow用户
提问于 2017-02-24 02:50:52
回答 1查看 814关注 0票数 0

我正在阅读命令模式,并看到来自不同站点的示例,它们似乎使用bridge+command模式来展示命令模式。

首先,来自维基百科:pattern,命令模式的定义:

命令模式是一种行为设计模式,在该模式中,对象用于封装执行操作或稍后触发事件所需的所有信息。此信息包括方法名称、拥有方法的对象和方法参数的值。

因此,使用这个定义,命令模式看起来非常简单,并且阅读了位于这里的书:https://addyosmani.com/resources/essentialjsdesignpatterns/book/#commandpatternjavascript,这个示例就是这样做的。

代码语言:javascript
运行
复制
(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" ));

})();

在这个例子中没有额外的东西,我只看到命令模式。然而,回到Wikipedia,他们使用下面的示例来展示命令模式:

代码语言:javascript
运行
复制
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);

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

我的问题是:我认为维基百科的例子是使用bridge+command模式来展示命令模式,对吗?

编辑:

如果我以第二个例子为例,删除命令部分,这不是桥接模式吗?

代码语言:javascript
运行
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-24 08:02:00

首先,我发现js示例中对Addy的解释与最初对GoF的解释(以及维基百科的定义)有些不同。

来自GoF命令模式页面:

命令模式是一种设计模式,它允许请求的所有信息都包含在单个对象中。然后,可以根据需要调用该命令,通常是作为一批具有回滚功能的排队命令的一部分。

这意味着命令对象应该包含一个无参数的Execute方法(有时还包含一个Undo)。命令的参数应该已经包含在其中了。该命令可以传递到调用方,排队,并在以后的任何时间执行。

维基百科的例子非常类似于最初的GoF,并遵循这个定义。

它不使用桥模式。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42429916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档