gpt4 book ai didi

javascript - 桥+命令模式

转载 作者:行者123 更新时间:2023-12-03 04:55:49 27 4
gpt4 key购买 nike

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

首先,来自维基百科: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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com