Command Design Pattern

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Gang of Four, Design Patterns
What Is It?

Simply put, this pattern enables you to decouple objects calling an action from the objects which may use them.   As such, it provides you a means to separate the responsibilities of issuing commands from anything executing commands, delegating this responsibility to different objects instead.

THE SIMPLEST COMMAND
// THE COMMAND PATTERN: Simplest
var CarManager = {
    // request information
    requestInfo: function (model, id) {
        console.log('The information for ' + model + ' with ID ' + id + ' is foobar');
        return 'The information for ' + model + ' with ID ' + id + ' is foobar';
    },
    // purchase the car
    buyVehicle: function (model, id) {
        console.log('You have successfully purchased Item ' + id + ', a ' + model);
        return 'You have successfully purchased Item ' + id + ', a ' + model;
    },
    // arrange a viewing
    arrangeViewing: function (model, id) {
        console.log('You have successfully booked a viewing of ' + model + ' ( ' + id + ' ) ');
        return 'You have successfully booked a viewing of ' + model + ' ( ' + id + ' ) ';
    }
};
                            

THE SIMPLEST COMMAND: Expanded

Now, lets use it...  

Of course, a better implementation would be to define the CarManager as a provider and then pass that into a consumer class (which then calls 'execute').   This particular example is merely meant to demonstrate the simple idea.

// THE COMMAND PATTERN: Expanded
// Lets expand on the object above to make itr more useful
CarManager.execute = function (command) {
    return CarManager[command.request](command.model, command.carID);
};
// Now lets use it
CarManager.execute({ request: "arrangeViewing", model: 'Ferrari', carID: '145523' });
CarManager.execute({ request: "requestInfo", model: 'Ford Mondeo', carID: '543434' });
CarManager.execute({ request: "requestInfo", model: 'Ford Escort', carID: '543434' });
CarManager.execute({ request: "buyVehicle", model: 'Ford Escort', carID: '543434' });
                            

THE COMMAND PATTERN: Prototypal Example

Here is a great example I found written by Joe Zim.

Notice that the command objects each follow an interface.   In this example, the interface defines only one method and each of those methods only calls one function themselves.  

Of course, you "could" simply use a callback function instead of all this.   In which case, you are "still" using the command pattern.

// THE COMMAND PATTERN: Prototypal Example
var EnableAlarm = function(alarm) {
    this.alarm = alarm;
}
EnableAlarm.prototype.execute = function () {
    this.alarm.enable();
}
 
var DisableAlarm = function(alarm) {
    this.alarm = alarm;
}
DisableAlarm.prototype.execute = function () {
    this.alarm.disable();
}
 
var ResetAlarm = function(alarm) {
    this.alarm = alarm;
}
ResetAlarm.prototype.execute = function () {
    this.alarm.reset();
}
 
var SetAlarm = function(alarm) {
    this.alarm = alarm;
}
SetAlarm.prototype.execute = function () {
    this.alarm.set();
}
                            

Special Thanks To:
  • Joe Zim
Behavioral

Copyright © 2015 All Rights Reserved