Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Mememto.
- Select Class from diagram toolbar. Click on diagram to create a class. Name it as Originator.
- Right-click on the Originator class, and select Add > Operation from the popup menu.
- Name the operation SetMemento(m : Memento).
- Repeat steps 4 and 5 to create operation CreateMemento() : Memento.
- Right-click on the Originator class, and select Add > Attribute from the popup menu.
- Name the attribute state.
- Move the mouse cursor over the Originator class, and drag out Dependency > Class to create a dependent class Memento.
- Create two operations in class Memento: GetState() and SetState().
- Create attribute state in class Memento.
- Create a class Caretaker near class Memento.
- Move the mouse cursor over the Caretaker class, and drag out Aggregation > Class to connect to Memento.
Defining 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 Memento. Keep the file name as is. Click OK to proceed.
Applying Design Pattern on Class Diagram
In this section, we will try to make use of the memento pattern to model a part of a sales ordering system, with regards to the state of sales order.
- Create a new project Order Processing System.
- Create a class diagram 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 Memento from the list of patterns.
- At the bottom of pane, rename Originator and Caretaker to Order and Transaction.
- Click OK to confirm editing and apply the pattern to diagram.
- Tidy up the diagram. It should become: