Memento Pattern Tutorial
This tutorial is aimed to guide the definition and application of Gang of Four (GoF) memento design pattern. By reading this tutorial, you will know how to develop a model for the memento 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 Memento.

- Select Class from the diagram toolbar. Click on the diagram to create a class. Name it 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 the 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 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 Memento. Keep the file name as is. Click OK to proceed.

Applying a Design Pattern on a 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 a 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 the pane, rename Originator and Caretaker to Order and Transaction.

- Click OK to confirm editing and apply the pattern to the diagram.
- Tidy up the diagram. It should become:
