Command Design Pattern


GoF Statement: Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.
Category: Behavioral
UML Diagram:  

Description of the Demonstration:

In this demonstration there are a number of different appliances (lights, stereo, coffee pot, hot tub, security system, TV, and sprinkler system) each of which needs to be controlled from an instance of RemoteControl. Each appliance instantiates an "on" and an "off" Command which can handle all the required actions for these features. All "on" and "off" commands are subclasses of Command which defines the common interface with the function execute(). Commands for appliances are registered with RemoteControl which calls the Command::execute function on the appropriate Command instance. It is also then able to set the appropriate "undo" Command to counter the last executed Command.


Code:

Click here to download a zip file containing all the source files and an executable of the Demonstration. See the _ReadMe.txt file in the zip.