Command Object

CategoryPattern: This is CommandPattern in DesignPatterns, the GangOfFour book.


A computer is instructed to do things by way of commands. The usual intention is for these commands to be performed immediately. However, there are often occasions where one would like to issue instructions referring to the commands themselves.

Therefore: Make an object that represents a command. The most elemental responsibility of a command object is to perform the operations intended by an instruction. Additional responsibilities might include...

The simulation is handy when a command is difficult to undo. For example, an editor with a one level undo might simulate global text substitutions (which can be hard to undo) until a subsequent command replaces it in the undo buffer. For those whose programming language doesn't provide threading, running batched CommandObject can give you pseudo-threading.


Tell me, could an entry object returned [?] from a Collection.add() method allowing the caller to replace or delete the added object without a reference to the original Collection (i.e., would entry.replaceWith(anObject), entry.delete(), etc.) be considered a use of this pattern?

-- WilliamUnderwood

No/Yes. Why would you be returning anObject from a Collection.add()? Also, the 'without a reference to the original Collection' would be irrelevant but could break OO principles of visibility and scope and quite badly couple the CommandObject to the collection.

However the following would be a perfectly valid implementation

 CommandObject aCommandObject = new CommandObject() ;
 aCollection.add( aCommandObject ) ;
 ...
 aCommandObject.execute() ;
-- MartinSpamer

What's the name of the command? doSomethingWithSomeObjectInSomeCollection? Each problem in coming up with a name indicates a problem in your design. The command pattern applies to some command, say eatMe(), with methods like eatMe.do(), eatMe.undo(), eatMe.simulate().

-- JimBalter?

I should have been more specific... it was more of an 'Anybody else ever try this?' remark...

I actually use this all the time: in one project, a list of video clips is stored in a collection. It is possible that it could be moved to a different collection at some point, so it's useful if it doesn't know which collection it's in. The clip therefore receives a command object to delete itself from the collection which handles this detail. This object is exposed so that the user interface for a clip can handle deleting that particular clip without any knowledge of the collection, while still giving final responsibility for deleting the clip to the collection it's in.

-- WilliamUnderwood

This is one of my favorite patterns. I'm wondering if anyone has considered using this pattern as a mechanism of recording actions for later replay in unit testing scenarios. One of the things that I have been doing is building an adapter to the underlying model which executes the commands on the model providing the usual undo functionality. What I am hoping to do when I get the time is plug a "recorder" onto the side of the base adapter class which records the commands (and their activities) and records that into a file/database/whatever which can then be used for reply.

This would be good not only for unit testing, but potentially auditing as well. Does anyone else have CommandObjectModelViewControllerCrossBreedingProgramme??

-- MitchDenny

Yes, I designed and constructed a framework in java for developing Web Applications and using the CommandPattern to implement the Controller from MVC. The Controller was implemented such that the Commands where decoupled from HTTP and could also be used directly. Though was this intended so that the project had the option of replacing its ThinClient with a RichClient, a side-effect was that we could use a driver, which was actually used for load testing (rather than unit test which used Junit). Struts uses a very similar approach.

-- MartinSpamer


May be useful in... DatabaseDeadlockAvoidancePatterns


See also ThreadedCommandObject


The MacApp version of CommandObject was one of many pieces of architecture contributed to BruceAnderson's first ArchitectureHandbookWorkshop.


It seems to me that the PageController? pattern in Fowler's Patterns of Enterprise Application Architecture could be implemented with a specialized CommandObject. This makes me wonder if you could consider a PageControllerObject? pattern as a "subclass" of the CommandObject pattern, and that makes me wonder if design patterns could be logically arranged in an inheritance hierarchy. Has anyone else considered this?

-- RyanSpringer


EditText of this page (last edited May 31, 2014) or FindPage with title or text search