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

  1. Create a new project Design Patterns.
  2. Create a class diagram Mememto.
    new diagram
  3. Select Class from diagram toolbar. Click on diagram to create a class. Name it as Originator.
    create colleague
  4. Right-click on the Originator class, and select Add > Operation from the popup menu.
    add oper to originator
  5. Name the operation SetMemento(m : Memento).
  6. Repeat steps 4 and 5 to create operation CreateMemento() : Memento.
    oper added to originator
  7. Right-click on the Originator class, and select Add > Attribute from the popup menu.
    add attr
  8. Name the attribute state.
    state added
  9. Move the mouse cursor over the Originator class, and drag out Dependency > Class to create a dependent class Memento.
  10. Create two operations in class MementoGetState() and SetState().
  11. Create attribute state in class Memento.
    oper added to memento
  12. Create a class Caretaker near class Memento.
    caretaker
  13. Move the mouse cursor over the Caretaker class, and drag out Aggregation > Class to connect to Memento.
    caretaker connect to memento

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 pattern
  3. In the Define Design Pattern dialog box, specify the pattern name Memento. Keep the file name as is. Click OK to proceed.
    name pattern

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.

  1. Create a new project Order Processing System.
  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 Memento from the list of patterns.
    select memento
  5. At the bottom of pane, rename Originator and Caretaker to Order and Transaction.
    rename classes
  6. Click OK to confirm editing and apply the pattern to diagram.
  7. Tidy up the diagram. It should become:
    result

Resources

  1. Design Patterns.vpp
  2. Memento.pat

Related Links

Leave a Comment

Your email address will not be published.