Command Pattern Tutorial

This tutorial is aimed to guide the definition and application of Gang of Four (GoF) command design pattern. By reading this tutorial, you will know how to develop a model for the command pattern, and how to apply it in practice.

October 14, 2009
Views: 62,967
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Command.
    new diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class and name it Invoker.
    new invoker
  4. Move the mouse cursor over the Invoker class and drag out Aggregation > Class to create an associated class named Command.
    create command class
  5. Right-click on Command and select Model Element Properties > Abstract to set it as abstract.
    set command abstract
  6. Right-click on the Command class and select Add > Operation from the popup menu.
    new command oper
  7. Name the operation Execute().
    new oper execute
  8. Right-click on Execute and select Model Element Properties > Abstract to set it as abstract.
    set execute abstract
  9. Move the mouse cursor over the Command class and drag out Generalization > Class to create a subclass named ConcreteCommand.
    concrete command
  10. You need to make the concrete commands inherit operations from the command class. Right-click on ConcreteCommand and select Related Elements > Realize all Interfaces from the popup menu.
    realize interface
  11. Right-click on the ConcreteCommand class and select Add > Attribute from the popup menu. Enter state as the attribute name.
    add concrete cmd attribute
  12. Move the mouse cursor over the ConcreteCommand class and drag out Association > Class to create an associated class named Receiver.
    create receiver class
  13. Right-click on the Receiver class and select Add > Operation from the popup menu. Enter Action as the operation name.
    create action oper
  14. Create a Client class near the Receiver class.
    create client class
  15. Move the mouse cursor over the Client class and drag out Association > Class to create an associated class named Receiver.
    associate client and receiver
  16. Move the mouse cursor over the Client class and drag out Dependency > Class to create a dependency on the ConcreteCommand class. Up to now, the diagram should look like this:
    pattern basically modeled
  17. In practice, there may be multiple concrete commands. To represent this, stereotype the ConcreteCommand class as PTN Cloneable. Right-click on ConcreteCommand and select Stereotypes > Stereotypes... from the popup menu.
    stereotype concrete command
  18. In the Stereotypes tab of the Class Specification dialog box, select PTN Cloneable and click > to assign it to the ConcreteCommand class. Click OK to confirm.
    select ptn cloneable
  19. There may be multiple actions that the receiver can perform. To represent this, stereotype the Receiver class as PTN Members Creatable. The diagram should now look like this:
    pattern modeled

Defining a Pattern

  1. Select all classes on the class diagram.
    select all classes
  2. Right-click on the selection and select Define Design Pattern... from the popup menu.
    define design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name as Command. Keep the file name as is and click OK to proceed.
    name pattern

Applying a Design Pattern to a Class Diagram

In this section, we will apply the command pattern to model a document editor.

  1. Create a new project named Document Editor.
  2. Create a class diagram named Domain Model.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
    apply design pattern
  4. In the Design Pattern dialog box, select Command from the list of patterns.
    select command
  5. Select Invoker in the overview.
    select invoker
  6. In the bottom pane, rename Invoker to ToolbarButton.
    rename invoker
  7. Select Command in the overview. In the bottom pane, rename Command to DocumentCommand.
    rename command
  8. Select ConcreteCommand in the overview. In the bottom pane, rename ConcreteCommand to OpenCommand.
    rename concrete command
  9. You need two more concrete commands for closing and saving a document. Click the + button and select Clone... from the popup menu.
    clone
  10. Enter 2 as the number of classes to clone.
    clone count
  11. Rename ConcreteCommand2 to CloseCommand and ConcreteCommand3 to SaveCommand.
    create commands
  12. Select Receiver in the overview. In the bottom pane, rename Receiver to Document and the Action operation to Load.
    rename receiver
  13. Create more operations for closing and saving documents. Click the + button and select New Operation... from the popup menu.
    new oper
  14. In the Operation Specification, enter Close as the name and click OK to confirm.
    close doc
  15. Repeat steps 13 and 14 to create the Save operation.
    create save oper
  16. Click OK to apply the pattern to the diagram.
  17. Tidy up the diagram. The result should look like this:
    result