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
  • 相关阅读:
    Python基础-迭代器
    Python基础-生成器
    Python基础-装饰器
    Python基础-函数
    Python基础-文件操作
    Python基础-集合
    jfinal任务调度quartz(cron) 定时任务 QuartzPlugin
    ServletRequest.getRequestDispatcher
    QuartZ Cron表达式
    Jax-WS WebService实现
  • 原文地址:https://www.cnblogs.com/liurui/p/5537901.html
Copyright © 2011-2022 走看看