<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="id12487412">
  <name>Command Design Pattern</name>
  <metadata>
  <md:version>1.2</md:version>
  <md:created>2008/07/03 23:52:00 GMT-5</md:created>
  <md:revised>2008/07/25 15:45:10.704 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="swong">
      <md:firstname>Stephen</md:firstname>
      
      <md:surname>Wong</md:surname>
      <md:email>swong@rice.edu</md:email>
    </md:author>
      <md:author id="dxnguyen">
      <md:firstname>Dung</md:firstname>
      <md:othername>X.</md:othername>
      <md:surname>Nguyen</md:surname>
      <md:email>dxnguyen@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="swong">
      <md:firstname>Stephen</md:firstname>
      
      <md:surname>Wong</md:surname>
      <md:email>swong@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="dxnguyen">
      <md:firstname>Dung</md:firstname>
      <md:othername>X.</md:othername>
      <md:surname>Nguyen</md:surname>
      <md:email>dxnguyen@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="prat">
      <md:firstname>Alex</md:firstname>
      
      <md:surname>Tribble</md:surname>
      <md:email>prat@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>command</md:keyword>
    <md:keyword>command design pattern</md:keyword>
    <md:keyword>design pattern</md:keyword>
  </md:keywordlist>

  <md:abstract>Explains and illustrates the command design pattern.</md:abstract>
</metadata>
  <content>
    <section id="id-532680938967">
      <name>Command Design Pattern</name>
      <para id="id12982435">When two objects communicate, often one object is sending a command to the other object to perform a particular function. The most common way to accomplish this is for the first object (the <term>issuer</term>) to hold a reference to the second (the <term>recipient</term>). The issuer executes a specific method on the recipient to send the command.</para>
      <para id="id11092445">But what if the issuer is not aware of, or does not care who the recipient is? That is, the issuer simply wants to abstractly issue the command?</para>
      <para id="id11061544">The <term>Command design pattern</term> encapsulates the concept of the command into an object. The issuer holds a reference to the command object rather than to the recipient. The issuer sends the command to the command object by executing a specific method on it. The command object is then responsible for dispatching the command to a specific recipient to get the job done.</para>
      <para id="id12330947">Note the similarities between the Command design pattern and the <cnxn document="m17037">Strategy design pattern</cnxn>.</para>
      <figure id="id12814010">
        <media type="image/png" src="command.png">
          <param name="height" value="360"/>
          <param name="width" value="710"/>
        </media>
      </figure>
      <para id="id11718974">In the above diagram, the invoker holds an abstract command and issues a command by calling the abstract <code>execute()</code> method. This command is translated into a specific action on a specific receiver by the various concrete command objects. It is also possible for a command to be a collection of commands, called a <term>macro</term> command. Calling the <code>execute</code> method of a macro command will invoke a collection of commands.</para>
    </section>
  </content>
</document>
