Memento Pattern

 Rate It (1)

The memento design pattern is a pattern that helps to save the object internal
state in an external place enabling us to restore the state later when needed.
The memento pattern doesn’t violate encapsulation of the internal state.
The pattern is rarely used but it’s very helpful in scientific computing or in
computer games (saving of check points in the game for example).
 
Use Cases for the Memento Pattern

You should use the pattern in the following cases:

  • You need to save object’s state and use the saved state later in order to
    restore the saved state.
  • You don’t want to expose the internal state of your object.

UML Diagram  
Memento Pattern UML   

Example in C#
The following code is an example of how to implement the pattern:

    #region Originator

    public class Originator<T>

    {

        #region Properties

        public T State { get; set; }

        #endregion

        #region Methods

        /// <summary>

        /// Creates a new memento to hold the current

        /// state

        /// </summary>

        /// <returns>The created memento</returns>

        public Memento<T> SaveMemento()

        {

            return (new Memento<T>(State));

        }

        /// <summary>

        /// Restores the state which is saved in the given memento

        /// </summary>

        /// <param name="memento">The given memento</param>

        public void RestoreMemento(Memento<T> memento)

        {           

            State = memento.State;

        }

        #endregion

    }

    #endregion

    #region Memento

    public class Memento<T>

    {

        #region Properties

        public T State { get; private set; }

        #endregion

        #region Ctor

        /// <summary>

        /// Construct a new memento object with the

        /// given state

        /// </summary>

        /// <param name="state">The given state</param>

        public Memento(T state)

        {

            State = state;

        }

        #endregion

    }

    #endregion

    #region Caretaker

    public class Caretaker<T>

    {

        #region Properties

        public Memento<T> Memento { get; set; }

        #endregion

    }

    #endregion

The given example shows the three parts of the pattern: the Originator, the Memento
and the Caretaker. The Caretaker is the repository for the Memento. You can also see 
that once the Memento object is created you can’t change the saved state and in order
to save a new Memento you need to create it again.
Lets look at an example of how to use the pattern in code:

   Originator<string> org = new Originator<string>();

   org.State = "Old State";

   // Store internal state in the caretaker object

   Caretaker<string> caretaker = new Caretaker<string>();

   caretaker.Memento = org.SaveMemento();

   Console.WriteLine("This is the old state: {0}", org.State);

   org.State = "New state";

   Console.WriteLine("This is the new state: {0}", org.State);

   // Restore saved state from the caretaker

   org.RestoreMemento(caretaker.Memento);

   Console.WriteLine("Old state was restored: {0}", org.State);

   // Wait for user

   Console.Read();

As you can see the saved state is inserted to the Caretaker object and than
you can change the state of the Originator to whatever you need. In order to
restore the Originator state you use the restore method and pass it the Caretaker’s
saved Memento.

Summary
To sum up the post, the memento pattern is is used for saving encapsulated object state
in an external object. The state can then be restored by demand.
As I wrote the places that you’ll want to use the pattern are in scientific
computing or in computer games but it can be helpful also in other places.

Revision number 2, Friday, October 03, 2008 8:26:05 PM by random0xff

Comments

Related Articles

Design Patterns

Design Patterns Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups

Prototype Pattern

The prototype is built upon the use of object cloning. The prototype creates new objects by cloning one of its concrete classes. The prototype is used in the following situations: You need to hide the concrete product classes from the client. You want to reduce

Threat Modeling

It's absolutely necessary if you're serious about security. Whitepapers/Books/Blogs Threat Modeling for ASP.NET (PDF) - an excellent white paper from Rüdiger Grimm and Henrik Eichstädt from the University of Kent Threat Modeling book from MSPress

IDisposable Pattern

The IDisposable pattern isn't one of the a classic patterns. It's a pattern suggested in MSDN to implement the IDisposable interface. You should be familiar with the pattern or with the interface because it's a basic thing to know about the .Net

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (5)
  2. SGWellens (4)
  3. maartenba (2)
  4. rami_nassar (2)
  5. stiansol (2)
  6. MisterFantastic (2)
  7. satish1.v (1)
  8. raklos (1)
  9. mosessaur (1)
  10. Jos Branders (1)

Advertise Here

Microsoft Communities
Page view counter