Welcome to this tutorial on the Gang of Four (GoF) command design pattern. Throughout this guide, we will help you understand the definition and application of the command pattern in your software projects. By following our step-by-step instructions, you will learn how to create a UML class diagram for the command pattern and save it as a design pattern file that can be reused in the future. Whether you are an experienced developer or just starting, this tutorial will provide you with valuable insights into how the command pattern can be used to improve your code.

What is Command Design Pattern?

The Command Design Pattern is a software design pattern that encapsulates a request as an object, thereby allowing for the parameterization of clients with different requests, and for the support of undoable operations. In this pattern, a command object is created that contains all the information necessary to perform a specific action, including the method to call, the arguments to pass, and the receiver that will perform the action.

The purpose of the Command pattern is to decouple the object that invokes the operation from the one that knows how to perform it. This allows the invoker object to be easily parameterized with different commands, and allows for the separation of concerns between the invoker and the receiver of the command.

The Command pattern is commonly used in object-oriented programming languages such as Java and C++, where it is used to implement menu systems, undo/redo functionality, and other software applications that require the encapsulation of requests as objects.

The Command pattern is an important concept for any programmer to understand, as it provides a simple and flexible way to implement complex functionality, and allows for the easy extension of the system by adding new commands.

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

Saving the 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 CloseCommandConcreteCommand3 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

Resources

  1. Command.pat
  2. Design Patterns.vpp

Related Links

Leave a Comment

Your email address will not be published.