• 中文(中华人民共和国)
    • English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(香港特別行政區)
  • 登录
  • 注册
DotNetAge - Mvc & jQuery CMS
隐藏边栏

Memento


Definition



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

Summary



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 class diagram





Participants



  • 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
*creates a memento containing a snapshot of its current internal state.

*uses the memento to restore its internal state
  • Caretaker
*is responsible for the memento's safekeeping

*never operates on or examines the contents of a memento.


Sample code in C#




#region Originator
public class Originator
{
#region Properties
public T State { get; set; }
#endregion

#region Methods
///
/// Creates a new memento to hold the current
/// state
///
/// The created memento
public Memento SaveMemento()
{
return (new Memento(State));
}

///
/// Restores the state which is saved in the given memento
///
/// The given memento
public void RestoreMemento(Memento memento)
{
State = memento.State;
}

#endregion

}

#endregion

#region Memento

public class Memento
{
#region Properties

public T State { get; private set; }

#endregion

#region Ctor

///
/// Construct a new memento object with the
/// given state
///
/// The given state
public Memento(T state)
{
State = state;
}

#endregion

}

#endregion

#region Caretaker

public class Caretaker
{

#region Properties

public Memento 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.

 


    Average:
  • 阅读
    (1213)
  • 固定链接
分享至:

Tag cloud

Anything in here will be replaced on browsers that support the canvas element