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
*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
*uses the memento to restore its internal state
- Caretaker
*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 MementoSaveMemento()
{
return (new Memento(State));
}
///
/// Restores the state which is saved in the given memento
///
/// The given memento
public void RestoreMemento(Mementomemento)
{
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 MementoMemento { 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.
-
-
阅读(1213)
-
固定链接