Jump to Menu

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.

Compatible edition(s): Enterprise, Professional, Standard

  • October 14, 2009
  • Views: 50,233
  • PDF

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Command.
    new diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Invoker.
    new invoker
  4. Move the mouse cursor over the Invoker class, and drag out Aggregation > Class to create an associated class 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 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 subclasses ConcreteCommand.
    concrete command
  10. We need 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 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 Receiver.
    create receiver class
  13. Right-click on the Receiver class, and select Add > Operation from the popup menu. Enter Action as 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 Receiver.
    associate client and receiver
  16. Move the mouse cursor over the Client class, and drag out Dependency > Class to create an associated class ConcreteCommand. Up to now, the diagram becomes:
    pattern basically modeled
  17. In practice, there may be multiple concrete handlers. To represent this, stereotypes the class ConcreteCommand 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 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 class Receiver as PTN Members Creatable. Up to now, the diagram becomes:
    pattern modeled

Defining 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 Command. Keep the file name as is. Click OK to proceed.
    name pattern

Applying Design Pattern on Class Diagram

In this section, we are going to apply the command pattern in modeling a document editor.

  1. Create a new project Document Editor.
  2. Create a class diagram 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 overview.
    select invoker
  6. At the bottom pane, rename Invoker to ToolbarButton.
    rename invoker
  7. Select Command in overview. At the bottom pane, rename Command to DocumentCommand.
    rename command
  8. Select ConcreteCommand in overview. At the bottom pane, rename ConcreteCommand to OpenCommand.
    rename concrete command
  9. We need 2 more concrete commands for closing and saving a document. Press on the + button and select Clone... from the popup menu.
    clone
  10. Enter 2 to be the number of classes to clone.
    clone count
  11. Rename ConcreteCommand2 to CloseCommand, ConcreteCommand3 to SaveCommand.
    create commands
  12. Select Receiver in overview. At the bottom pane, rename Receiver to Document, and operation Action to Load.
    rename receiver
  13. Create more operations for closing and saving documents. Click on the + button and select New Operation... from the popup menu.
    new oper
  14. In the Operation Specification, enter Close as name. Click OK to confirm.
    close doc
  15. Repeat steps 13 and 14 to create operation Save.
    create save oper
  16. Click OK to apply the pattern to diagram.
  17. Tidy up the diagram. Here is the result:
    result



创造美好 共同成长

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.

OK