zoukankan      html  css  js  c++  java
  • 设计模式8-备忘录模式

    备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。

     1 namespace DesignModel.备忘录模式
     2 {
     3     class Order
     4     {
     5         public string State { get; set; }
     6 
     7         public Memento CreateMemento()
     8         {
     9             return new Memento(State);
    10         }
    11 
    12         public void SetMemento(Memento memento)
    13         {
    14             this.State = memento.State;
    15         }
    16 
    17     }
    18 
    19 
    20     class Memento
    21     {
    22        public Memento(string state)
    23         {
    24             this.state = state;
    25         }
    26         private string state;
    27         public string State
    28         {
    29             get { return state; }
    30             //set { }
    31         }
    32     }
    33 
    34     class MementoHelper
    35     {
    36         public Memento memento { get; set; }
    37 
    38     }
    39 }
    40 
    41       static void 备忘录模式()
    42         {
    43             Order o = new Order();
    44             o.State = "new";
    45 
    46             MementoHelper h = new MementoHelper();
    47             //备份管理类,里面可以做其它的事情,比如保存一个备份列表,
    48             //或者一些额外信息,否则本列用以下一句话就行了。
    49             //var m = o.CreateMemento();
    50 
    51             h.memento = o.CreateMemento();
    52             o.State = "old";
    53             //还原
    54             o.SetMemento(h.memento);
    55 
    56         }
    View Code
  • 相关阅读:
    台州 OJ 3847 Mowing the Lawn 线性DP 单调队列
    洛谷 OJ P1417 烹调方案 01背包
    快速幂取模
    台州 OJ 2649 More is better 并查集
    UVa 1640
    UVa 11971
    UVa 10900
    UVa 11346
    UVa 10288
    UVa 1639
  • 原文地址:https://www.cnblogs.com/liurui/p/5537901.html
Copyright © 2011-2022 走看看