1. 定义
在不破环封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。2. UML 类图
3. 结构代码
// Memento pattern -- Structural example
|
using System;
namespace DoFactory.GangOfFour.Memento.Structural { ///<summary> /// MainApp startup class for Structural /// Memento Design Pattern. ///</summary> classMainApp { ///<summary> /// Entry point into console application. ///</summary> staticvoid Main() { Originator o =new Originator(); o.State = "On";
// Store internal state Caretaker c =new Caretaker(); c.Memento = o.CreateMemento();
// Continue changing originator o.State = "Off";
// Restore saved state o.SetMemento(c.Memento);
// Wait for user Console.ReadKey(); } }
///<summary> /// The 'Originator' class ///</summary> classOriginator { privatestring _state;
// Property publicstring State { get {return _state; } set { _state = value; Console.WriteLine("State = " + _state); } }
// Creates memento publicMemento CreateMemento() { return (newMemento(_state)); }
// Restores original state publicvoid SetMemento(Memento memento) { Console.WriteLine("Restoring state..."); State = memento.State; } }
///<summary> /// The 'Memento' class ///</summary> classMemento { privatestring _state;
// Constructor public Memento(string state) { this._state = state; }
// Gets or sets state publicstring State { get {return _state; } } }
///<summary> /// The 'Caretaker' class ///</summary> classCaretaker { privateMemento _memento;
// Gets or sets memento publicMemento Memento { set { _memento =value; } get {return _memento; } } } } |
Output
State = On
State = Off Restoring state: State = On |
4. 实例代码
// Memento pattern -- Real World example
|
using System;
namespace DoFactory.GangOfFour.Memento.RealWorld { ///<summary> /// MainApp startup class for Real-World /// Memento Design Pattern. ///</summary> classMainApp { ///<summary> /// Entry point into console application. ///</summary> staticvoid 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(); } }
///<summary> /// The 'Originator' class ///</summary> classSalesProspect { privatestring _name; privatestring _phone; privatedouble _budget;
// Gets or sets name publicstring Name { get {return _name; } set { _name = value; Console.WriteLine("Name: " + _name); } }
// Gets or sets phone publicstring Phone { get {return _phone; } set { _phone = value; Console.WriteLine("Phone: " + _phone); } }
// Gets or sets budget publicdouble Budget { get {return _budget; } set { _budget = value; Console.WriteLine("Budget: " + _budget); } }
// Stores memento publicMemento SaveMemento() { Console.WriteLine("\nSaving state --\n"); returnnew Memento(_name, _phone, _budget); }
// Restores memento publicvoid RestoreMemento(Memento memento) { Console.WriteLine("\nRestoring state --\n"); this.Name = memento.Name; this.Phone = memento.Phone; this.Budget = memento.Budget; } }
///<summary> /// The 'Memento' class ///</summary> classMemento { privatestring _name; privatestring _phone; privatedouble _budget;
// Constructor public Memento(string name,string phone, double budget) { this._name = name; this._phone = phone; this._budget = budget; }
// Gets or sets name publicstring Name { get {return _name; } set { _name =value; } }
// Gets or set phone publicstring Phone { get {return _phone; } set { _phone =value; } }
// Gets or sets budget publicdouble Budget { get {return _budget; } set { _budget =value; } } }
///<summary> /// The 'Caretaker' class ///</summary> classProspectMemory { privateMemento _memento;
// Property publicMemento 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 |
该文章来自:http://www.dofactory.com/Patterns/PatternMemento.aspx