SIGN UP MEMBER LOGIN:    
ARTICLE

Memento Design Pattern

Posted by Shinuraj Articles | Design & Architecture February 11, 2011
The Memento pattern is a design pattern that permits the current state of an object to be stored without breaking the rules of encapsulation.
Reader Level:


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*/

Login to add your contents and source code to this article
share this article :
post comment
 

Tx :)

Posted by Raanan Nov 26, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor