Memento Design Pattern


Last week I tried to learn Memento Design Pattern, thought to share my knowledge with you all.

The Memento pattern is a design pattern that permits the current state of an object to be stored without breaking the rules of encapsulation. The originating object can be modified as required but can be restored to the saved state at any time.

  • Intent: The intent of Memento pattern is to capture the internal state of an object without violating encapsulation and thus providing a way to restore the object to it's initial state when needed.

    Motivation & Applicability: This is a behavioral pattern as it defines a manner for controlling communication between classes or entities. The Memento pattern is used to capture the current state of an object and store it in such a manner that it can be restored at a later time. Importantly, the pattern allows for the internal state of an object to be saved without breaking the rules of encapsulation. This design pattern is commonly used to provide an application with "Undo" functionality.

Participants of this design pattern are :
  • Memento (Memento)

    • Stores internal state of the Originator object. The memento may store as much or as little of the originator's internal state as necessary at its originator's discretion.
     
    • Protect against access by objects of other than the originator. Mementos have effectively two interfaces. Caretaker sees a narrow interface to the Memento -- it can only pass the memento to the other objects. Originator, in contrast, sees a wide interface, one that lets it access all the data necessary to restore itself to its previous state. Ideally, only the originator that produces the memento would be permitted to access the memento's internal state
     
  • Originator (SalesProspect)

    • Creates a memento containing a snapshot of its current internal state.
    • Uses the memento to restore its internal state
     
  • Caretaker (ProspectMemory)

    • Is responsible for the memento's safekeeping
    • Never operates on or examines the contents of a memento.

UML

/*Memento pattern which temporarily saves
 * and then restores the SalesProspect's internal state*/
class MainApp
    {
        static void Main()
        {
            SalesProspect s = new SalesProspect();
            s.Name = "Noel van Halen";
            s.Phone = "(412) 256-0990";
            s.Budget = 25000.0;
           
// Store internal state
            ProspectMemory m = new ProspectMemory();
            m.Memento = s.SaveMemento();
           
// Continue changing originator
            s.Name = "Leo Welch";
            s.Phone = "(310) 209-7111";
            s.Budget = 1000000.0;
           
// Restore saved state
            s.RestoreMemento(m.Memento);
           
// Wait for user
            Console.ReadKey();
        }
    }
// The 'Memento' class
    class Memento
    {
        private string _name;
        private string _phone;
        private double _budget;
       
// Constructor
        public Memento(string name, string phone, double budget)
        {
            this._name = name;
            this._phone = phone;
            this._budget = budget;
        }
       
// Gets or sets name
        public string Name
        {
            get { return _name; } set { _name = value; }
        }
       
// Gets or set phone
        public string Phone
        {
            get { return _phone; } set { _phone = value; }
        }
       
// Gets or sets budget
        public double Budget
        {
            get { return _budget; }set { _budget = value; }
        }
    }
// The 'Originator' class
    class SalesProspect
    {
        private string _name;
        private string _phone;
        private double _budget;
       
// Gets or sets name
        public string Name
        {
            get { return _name; }   set  { _name = value;
               Console.WriteLine("Name:  " + _name);
            }
        }
       
// Gets or sets phone
        public string Phone
        {
            get { return _phone; }  set  {   _phone = value;
                Console.WriteLine("Phone: " + _phone);
            }
        }
       
// Gets or sets budget
        public double Budget
        {
            get { return _budget; } set  { _budget = value;
                Console.WriteLine("Budget: " + _budget);
            }
        }
       
// Stores memento
        public Memento SaveMemento()
        {
            Console.WriteLine("\nSaving state --\n");
            return new Memento(_name, _phone, _budget);
        }
       
// Restores memento
        public void RestoreMemento(Memento memento)
        {
            Console.WriteLine("\nRestoring state --\n");
            this.Name = memento.Name;
            this.Phone = memento.Phone;
            this.Budget = memento.Budget;
        }
    }

// The 'Caretaker' class
    class ProspectMemory
    {
        private Memento _memento;
        // Property
        public Memento Memento
        {
            set { _memento = value; } get { return _memento; }
        }
    }

/*OUTPUT :
Name:   Noel van Halen
Phone:  (412) 256-0990
Budget: 25000
Saving state --
Name:   Leo Welch
Phone:  (310) 209-7111
Budget: 1000000

Restoring state --

Name:   Noel van Halen
Phone:  (412) 256-0990
Budget: 25000*/


Similar Articles