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.
Modeling a Design Pattern with a Class Diagram
- Create a new project named Design Patterns.
- Create a class diagram named Command.

- Select Class from the diagram toolbar. Click on the diagram to create a class and name it Invoker.

- Move the mouse cursor over the Invoker class and drag out Aggregation > Class to create an associated class named Command.

- Right-click on Command and select Model Element Properties > Abstract to set it as abstract.

- Right-click on the Command class and select Add > Operation from the popup menu.

- Name the operation Execute().

- Right-click on Execute and select Model Element Properties > Abstract to set it as abstract.

- Move the mouse cursor over the Command class and drag out Generalization > Class to create a subclass named ConcreteCommand.

- 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.

- Right-click on the ConcreteCommand class and select Add > Attribute from the popup menu. Enter state as the attribute name.

- Move the mouse cursor over the ConcreteCommand class and drag out Association > Class to create an associated class named Receiver.

- Right-click on the Receiver class and select Add > Operation from the popup menu. Enter Action as the operation name.

- Create a Client class near the Receiver class.

- Move the mouse cursor over the Client class and drag out Association > Class to create an associated class named Receiver.

- 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:

- 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.

- 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.

- 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:

Defining a Pattern
- Select all classes on the class diagram.

- Right-click on the selection and select Define Design Pattern... from the popup menu.

- In the Define Design Pattern dialog box, specify the pattern name as Command. Keep the file name as is and click OK to proceed.

Applying a Design Pattern to a Class Diagram
In this section, we will apply the command pattern to model a document editor.
- Create a new project named Document Editor.
- Create a class diagram named Domain Model.
- Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.

- In the Design Pattern dialog box, select Command from the list of patterns.

- Select Invoker in the overview.

- In the bottom pane, rename Invoker to ToolbarButton.

- Select Command in the overview. In the bottom pane, rename Command to DocumentCommand.

- Select ConcreteCommand in the overview. In the bottom pane, rename ConcreteCommand to OpenCommand.

- You need two more concrete commands for closing and saving a document. Click the + button and select Clone... from the popup menu.

- Enter 2 as the number of classes to clone.

- Rename ConcreteCommand2 to CloseCommand and ConcreteCommand3 to SaveCommand.

- Select Receiver in the overview. In the bottom pane, rename Receiver to Document and the Action operation to Load.

- Create more operations for closing and saving documents. Click the + button and select New Operation... from the popup menu.

- In the Operation Specification, enter Close as the name and click OK to confirm.

- Repeat steps 13 and 14 to create the Save operation.

- Click OK to apply the pattern to the diagram.
- Tidy up the diagram. The result should look like this:
