Chinaunix首页 | 论坛 | 博客
  • 博客访问: 617152
  • 博文数量: 87
  • 博客积分: 3399
  • 博客等级: 中校
  • 技术积分: 1422
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 21:20
文章分类

全部博文(87)

文章存档

2013年(1)

2012年(51)

2011年(33)

2010年(2)

分类: 嵌入式

2011-08-19 19:33:38

备忘录模式 Memento
结构: 
意图:

在在不破坏封装性的前提下,获取一个对象的内部状态,并在对象之外保存该状态,以后可将该对象恢复到原先保存的状态。

适用性:

l  必须保存一个对象某时刻的(部分)状态,这样以后需要它时才能恢复到原状态。

l  如果用一个接口来让其他对象获得这些状态,会暴露对象的实现细节,破坏封装性。

示例:
  1. // Memento


  2. // Intent: "Without violating encapsulation, capture and externalize an

  3. // object's internal state so that an object can be restored to this

  4. // state later."

  5. //在不破坏对象封装性的前提下,获取一个对象的内部状态,并在对象之外将其保存,

  6. //以便以后将该对象恢复到原先保存的状态。


  7. // For further information, read "Design Patterns", p283, Gamma et al.,

  8. // Addison-Wesley, ISBN:0-201-63361-2


  9. /* Notes:
  10.  * We often have client code that wishes to record the current state of an
  11.  * object, without being interested in the actual data values (this is
  12.  * needed for undo and checkpointing). To support this behavior, we can have
  13.  * the object record its internal data in a helper class called a memento, and
  14.  * the client code can treat this as an opaque store of the object's state.
  15.  * At some later point, the client can pass the memento back into the object,
  16.  * to restore it to the previous state.
  17.  * 我们的客户代码希望在无需知道实际数据值的情况下能够记录一个对象某时刻的状态。
  18.  * 为了支持此行为,我们可以将对象的内部数据保存在一个称为备忘录的辅助类里,
  19.  * 客户代码可以将之视为对象状态的不透明仓库。此后,客户可以将备忘录传给对象,
  20.  * 以恢复到原来的状态。
  21.  */

  22. namespace Memento_DesignPattern
  23. {
  24.     using System;

  25.     class Originator
  26.     {
  27.         private double manufacturer = 0;
  28.         private double distributor = 0;
  29.         private double retailer = 0;

  30.         public void MakeSale(double purchasePrice)
  31.         {
  32.             // We assume sales are divided equally amount the three

  33.             manufacturer += purchasePrice * .40;
  34.             distributor += purchasePrice * .3;
  35.             retailer += purchasePrice * .3;
  36.             // Note: to avoid rounding errors for real money handling

  37.             // apps, we should be using decimal integers

  38.             // (but hey, this is just a demo!)

  39.         }

  40.         public Memento CreateMemento()
  41.         {
  42.             return (new Memento(manufacturer, distributor, retailer));
  43.         }

  44.         public void SetMemento(Memento m)
  45.         {
  46.             manufacturer = m.A;
  47.             distributor = m.B;
  48.             retailer = m.C;
  49.         }
  50.         public void printVar()
  51.         {
  52.             Console.WriteLine(this.manufacturer);
  53.             Console.WriteLine(this.distributor);
  54.             Console.WriteLine(this.retailer);
  55.         }
  56.     }

  57.     class Memento
  58.     {
  59.         private double iA;
  60.         private double iB;
  61.         private double iC;

  62.         public Memento(double a, double b, double c)
  63.         {
  64.             iA = a;
  65.             iB = b;
  66.             iC = c;
  67.         }

  68.         public double A
  69.         {
  70.             get
  71.             {
  72.                 return iA;
  73.             }
  74.         }

  75.         public double B
  76.         {
  77.             get
  78.             {
  79.                 return iB;
  80.             }
  81.         }

  82.         public double C
  83.         {
  84.             get
  85.             {
  86.                 return iC;
  87.             }
  88.         }
  89.     }

  90.     class caretaker
  91.     {

  92.     }

  93.     ///

  94.     /// Summary description for Client.

  95.     ///

  96.     public class Client
  97.     {
  98.         public static int Main(string[] args)
  99.         {
  100.             Originator o = new Originator();

  101.             // Assume that during the course of running an application

  102.             // we we set various data in the originator

  103.             o.MakeSale(45.0);
  104.             o.MakeSale(60.0);
  105.             o.printVar();

  106.             // Now we wish to record the state of the object

  107.             Memento m = o.CreateMemento();
  108.             o.printVar();


  109.             // We make further changes to the object

  110.             o.MakeSale(60.0);
  111.             o.MakeSale(10.0);
  112.             o.MakeSale(320.0);

  113.             // Then we decide ot change our minds, and revert to the saved state (and lose the changes since then)

  114.             o.SetMemento(m);
  115.             o.printVar();

  116.             return 0;
  117.         }
  118.     }
  119. }
 
阅读(674) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~