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
  • 相关阅读:
    二分图最大匹配的König定理及其证明
    HDOJ 2389 Rain on your Parade
    HDOJ 1083 Courses
    HDOJ 2063 过山车
    POJ 1469 COURSES
    UESTC 1817 Complete Building the Houses
    POJ 3464 ACM Computer Factory
    POJ 1459 Power Network
    HDOJ 1532 Drainage Ditches
    HDU 1017 A Mathematical Curiosity
  • 原文地址:https://www.cnblogs.com/liurui/p/5537901.html
Copyright © 2011-2022 走看看