software development, consultancy and web site design services

Programming - Memento Behavioral Design Pattern

Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.


The classes and/or objects participating in this pattern are:


  • Memento
    andand
    • 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
    and
    • creates a memento containing a snapshot of its current internal state.
    • uses the memento to restore its internal state
  • Caretaker
    andand
    • is responsible for the memento's safekeeping
    • never operates on or examines the contents of a memento.
// Memento pattern -- Structural example

using
System;

namespace
DoFactory.GangOfFour.Memento.Structural
{

andand// MainApp test application

andandclass MainApp
andand{
andandandandstatic void Main()
andandandand{
andandandandandandOriginator o = new Originator();
andandandandandando.State = "On";

andandandandandand// Store internal state
andandandandandandCaretaker c = new Caretaker();
andandandandandandc.Memento = o.CreateMemento();

andandandandandand// Continue changing originator
andandandandandando.State = "Off";

andandandandandand// Restore saved state
andandandandandando.SetMemento(c.Memento);

andandandandandand// Wait for user
andandandandandandConsole.Read();
andandandand}
andand}

andand// "Originator"

andandclass Originator
andand{
andandandandprivate string state;

andandandand// Property
andandandandpublic string State
andandandand{
andandandandandandget{ return state; }
andandandandandandset
andandandandandand{
andandandandandandandandstate = value;
andandandandandandandandConsole.WriteLine("State = " + state);
andandandandandand}
andandandand}

andandandandpublic Memento CreateMemento()
andandandand{
andandandandandandreturn (new Memento(state));
andandandand}

andandandandpublic void SetMemento(Memento memento)
andandandand{
andandandandandandConsole.WriteLine("Restoring state:");
andandandandandandState = memento.State;
andandandand}
andand}

andand// "Memento"

andandclass Memento
andand{
andandandandprivate string state;

andandandand// Constructor
andandandandpublic Memento(string state)
andandandand{
andandandandandandthis.state = state;
andandandand}

andandandand// Property
andandandandpublic string State
andandandand{
andandandandandandget{ return state; }
andandandand}
andand}

andand// "Caretaker"

andandclass Caretaker
andand{
andandandandprivate Memento memento;

andandandand// Property
andandandandpublic Memento Memento
andandandand{
andandandandandandset{ memento = value; }
andandandandandandget{ return memento; }
andandandand}
andand}
}
and
Output expected:
State = On
State = Off
Restoring state:
State = On



PDF Document: Programming - Memento Behavioral Design Pattern
Printer friendly PDF

2008 © DOTNUTSHELL TECHNOLOGIES Ltd.
Currently 91 active user(s) with 1904 hit(s) since 3.00 GMT 01/12/2008. RSS FEED
Home | Privacy Policy | Site Map | Links | Company | Contact Us | Arena

We provide cutting edge software development, consultancy and web design services.

Valid XHTML 1.0 Transitional Valid CSS!