Memento Pattern Tutorial
This tutorial aims to guide the definition and application of the Gang of Four (GoF) memento design pattern. By following this tutorial, you will learn how to create a UML class diagram for the memento pattern and save it as a design pattern file that can be reused in the future.
What is Memento Design Pattern?
Memento Design Pattern is a behavioral design pattern that enables the capture and externalization of an object’s internal state without violating encapsulation, allowing the object to be restored to its previous state later. The key idea of this pattern is to save the state of an object at different points in time and restore it to a previous state as required.
The Memento pattern consists of three main components: the originator, the memento, and the caretaker. The originator is the object whose state is being saved and restored. The memento is an object that stores the state of the originator. The caretaker is an object that knows how to store and retrieve mementos but does not modify them.
By using the Memento pattern, objects can be returned to their previous state, undoing any changes made since that point in time. This pattern is commonly used in applications that require an undo feature, such as in text editors, graphic editors, or any other application that allows the user to undo or redo an action.
Overall, the Memento pattern helps in maintaining the state of an object and provides a way to restore it when needed, without exposing its internal details to other objects.
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.
Saving the 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: