CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns
    Creational Patterns
    Structural Patterns
    Behavioral Patterns
        Chain of Responsibility
        Command
        Interpreter
        Iterator
        Mediator
        Memento
        Observer
        State
        Strategy
        Template Method
        Visitor


Memento Design Pattern


Memento design pattern is used to capture the state of an object without violating encapsulation, so that if required then it can be returned to its previous state.


Intent
  • Without violating encapsulation, capture and externalize an objectís internal state so that the object can be restored to this state later.
The intent is to capture the internal state of object without violating encapsulation, and restore to the same state if required.

Problem
  • Need to save state of an object to restore it later. Saving it externally is difficult as it violates encapsulation.
There may be scenarios like undo, rollback, handling exceptions where object has to be restored to previous state.

Solution
  • Store the state of the object in another object and make it accessible from the object whose state is saved.
The object state will be saved in another object which will be created and accessed by same object so it will not violate the encapsulation.

Where it is applicable?
  • An Exception has to be handled in a way where an object has to be restored to its previous state.
  • The object state has to be stored somewhere to restore it later but should not be accessed by anyone, other than the object whose state is saved.
  • The scenarios of undo and rollback operations.
Structure

Memento Design Pattern

Participant classes
  • Originator class has the state to be captured. The method CreateMemnto() is used to instantiate the Memento object which stores the state of the originator object; and Method SetMemnto() is used to get the previous state and restore the same.
  • Memento class keeps the internal state of the originator object and can be accessed by originator only.
  • Caretaker class keeps the Memento object safely, this is nothing but a mechanism for undo or rollback.
How they work together?
  • The client uses caretaker which requests originator to have a memento; and if undo or rollback is required then pass the memento to originator for restoring the object state. Originator accesses its previous state from memento and restores the same.
  • So the object state is stored externally; and if required then it can be accessed to restore the same without violating encapsulation. This may be expensive if object is large or many mementos have to be created. There may be possibility to store state with incremental changes too.
Example

Implementation Code

C++   Memento Pattern in C++   Example in C++
C#   Memento Pattern in C#   Example in C#
Java   Memento Pattern in Java   Example in Java
Python   Memento Pattern in Python   Example in Python
JavaScript   Memento Pattern in JavaScript   Example in JavaScript
PHP   Memento Pattern in PHP   Example in PHP
Ruby   Memento Pattern in Ruby   Example in Ruby
Swift   Memento Pattern in Swift   Example in Swift
Objective-C   Memento Pattern in Objective-C   Example in Objective-C



Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his courses on design patterns.







Yearly Subscription


Design Patterns in Objective-C


Data Structures in Java

Explore

Courses

Subscription

Knowledge Hub

Design Patterns

Tech Articles

About Us

Overview

Contact Us

CourseGalaxy

CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

© 2018 CourseGalaxy