Last active
August 29, 2015 14:23
-
-
Save OriginUnknown/27715bbfc6d90495deda to your computer and use it in GitHub Desktop.
JavaScript OOP Design Pattern Command Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>Command Pattern</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var _msg = ""; | |
//Receiver[s] -> classes that will carry out the action | |
function Light () { | |
var turnOnLight = function () { | |
_msg = "The light is on"; | |
return _msg; | |
}; | |
var turnOffLight = function () { | |
_msg = "The light is on"; | |
return _msg; | |
}; | |
//module pattern used to return the API for the Light method | |
return { | |
"turnOn" : turnOnLight, | |
"turnOff" : turnOffLight | |
} | |
} | |
//ConcreteCommand #1 -> implements the command interface by using the signature methods (execute and undo) to wrap the receiver's methods in | |
var TurnLightsOnCommand = function () { | |
var theLight = Light(); | |
var execute = function () { | |
return theLight.turnOn(); | |
}; | |
var undo = function () { | |
return theLight.turnOff(); | |
}; | |
//for the logging purposes | |
var cmdName = function () { | |
var fnName = "TurnLightsOnCommand"; | |
return fnName; | |
}; | |
return { | |
"execute" : execute, | |
"undo" : undo, | |
"getCmdName" : cmdName | |
}; | |
}; | |
//ConcreteCommand #2 | |
var TurnLightsOffCommand = function () { | |
var theLight = Light(); | |
var execute = function () { | |
return theLight.turnOff(); | |
}; | |
var undo = function () { | |
return theLight.turnOn(); | |
}; | |
//for the logging purposes | |
var cmdName = function () { | |
var fnName = "TurnLightsOffCommand"; | |
return fnName; | |
}; | |
return { | |
"execute" : execute, | |
"undo" : undo, | |
"getCmdName" : cmdName | |
}; | |
}; | |
/* | |
* * Invoker - triggers the execution of the command via the command object passed in. | |
* * It also keeps a log of the commands issued and stores them into an array | |
*/ | |
var RemoteControl = function () { | |
var commands = [], logger = StatusLogger();//private variables | |
/* | |
* * Like the ConcreteCommands, the Invoker uses the Command interface and therefore, expects the objects passed to | |
* * it to have identical signature methods to the ones it's invoking | |
*/ | |
var onPress = function ( cmd ) { | |
commands.push( cmd ); | |
cmd.execute(); | |
logger.add( cmd.getCmdName() + " has been executed." ); | |
}; | |
var onUndoPress = function ( cmd ) { | |
commands.pop(); | |
cmd.undo(); | |
logger.add( cmd.getCmdName() + " has been undone." ); | |
}; | |
return { | |
"press" : onPress, | |
"undoPress" : onUndoPress, | |
"logs" : logger.show | |
}; | |
}; | |
//Helper function to show a list of commands that have been executed | |
var StatusLogger = function () { | |
var msg = ""; | |
var add = function ( m ) { | |
msg += m + "\n"; | |
}; | |
var showMsg = function () { | |
return msg; | |
}; | |
return { | |
"add" : add, | |
"show" : showMsg | |
}; | |
}; | |
//client | |
//invokes the required command objects | |
var switchOnLight = TurnLightsOnCommand(); | |
var switchOffLight = TurnLightsOffCommand(); | |
//gets the invoker class that will trigger the command requests | |
var remoteControlDevice = RemoteControl(); | |
//pass the command objects to the invoker for execution | |
remoteControlDevice.press( switchOnLight ); | |
remoteControlDevice.press( switchOffLight ); | |
remoteControlDevice.undoPress( switchOffLight ); | |
//get a list of the three commands that have been executed | |
console.log( remoteControlDevice.logs() ); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment