结构:
意图:
在在不破坏封装性的前提下,获取一个对象的内部状态,并在对象之外保存该状态,以后可将该对象恢复到原先保存的状态。
适用性:
l 必须保存一个对象某时刻的(部分)状态,这样以后需要它时才能恢复到原状态。
l 如果用一个接口来让其他对象获得这些状态,会暴露对象的实现细节,破坏封装性。
示例:
- // Memento
- // Intent: "Without violating encapsulation, capture and externalize an
- // object's internal state so that an object can be restored to this
- // state later."
- //在不破坏对象封装性的前提下,获取一个对象的内部状态,并在对象之外将其保存,
- //以便以后将该对象恢复到原先保存的状态。
- // For further information, read "Design Patterns", p283, Gamma et al.,
- // Addison-Wesley, ISBN:0-201-63361-2
- /* Notes:
- * We often have client code that wishes to record the current state of an
- * object, without being interested in the actual data values (this is
- * needed for undo and checkpointing). To support this behavior, we can have
- * the object record its internal data in a helper class called a memento, and
- * the client code can treat this as an opaque store of the object's state.
- * At some later point, the client can pass the memento back into the object,
- * to restore it to the previous state.
- * 我们的客户代码希望在无需知道实际数据值的情况下能够记录一个对象某时刻的状态。
- * 为了支持此行为,我们可以将对象的内部数据保存在一个称为备忘录的辅助类里,
- * 客户代码可以将之视为对象状态的不透明仓库。此后,客户可以将备忘录传给对象,
- * 以恢复到原来的状态。
- */
- namespace Memento_DesignPattern
- {
- using System;
- class Originator
- {
- private double manufacturer = 0;
- private double distributor = 0;
- private double retailer = 0;
- public void MakeSale(double purchasePrice)
- {
- // We assume sales are divided equally amount the three
- manufacturer += purchasePrice * .40;
- distributor += purchasePrice * .3;
- retailer += purchasePrice * .3;
- // Note: to avoid rounding errors for real money handling
- // apps, we should be using decimal integers
- // (but hey, this is just a demo!)
- }
- public Memento CreateMemento()
- {
- return (new Memento(manufacturer, distributor, retailer));
- }
- public void SetMemento(Memento m)
- {
- manufacturer = m.A;
- distributor = m.B;
- retailer = m.C;
- }
- public void printVar()
- {
- Console.WriteLine(this.manufacturer);
- Console.WriteLine(this.distributor);
- Console.WriteLine(this.retailer);
- }
- }
- class Memento
- {
- private double iA;
- private double iB;
- private double iC;
- public Memento(double a, double b, double c)
- {
- iA = a;
- iB = b;
- iC = c;
- }
- public double A
- {
- get
- {
- return iA;
- }
- }
- public double B
- {
- get
- {
- return iB;
- }
- }
- public double C
- {
- get
- {
- return iC;
- }
- }
- }
- class caretaker
- {
- }
- ///
- /// Summary description for Client.
- ///
- public class Client
- {
- public static int Main(string[] args)
- {
- Originator o = new Originator();
- // Assume that during the course of running an application
- // we we set various data in the originator
- o.MakeSale(45.0);
- o.MakeSale(60.0);
- o.printVar();
- // Now we wish to record the state of the object
- Memento m = o.CreateMemento();
- o.printVar();
- // We make further changes to the object
- o.MakeSale(60.0);
- o.MakeSale(10.0);
- o.MakeSale(320.0);
- // Then we decide ot change our minds, and revert to the saved state (and lose the changes since then)
- o.SetMemento(m);
- o.printVar();
- return 0;
- }
- }
- }
阅读(751) | 评论(0) | 转发(0) |