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

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

    namespace Memento
    {
        public class Originator
        {
            private string state;
            public string State
            {
                get { return state; }
                set { state = value; }
            }
            public Memento CreateMemento()
            {
                return new Memento(state);
            }
            public void SetMemento(Memento memento)
            {
                state = memento.State;
            }
            public void Show()
            {
                Console.WriteLine("State=" + state);
            }
        }
        public class Memento
        {
            private string state;
            public Memento(string state)
            {
                this.state = state;
            }
            public string State
            {
                get { return state; }
            }
        }
        public class Caretaker
        {
            private Memento memento;
            public Memento Memento
            {
                get { return memento; }
                set { memento = value; }
            }
        }
    }
    View Code

    测试代码:

                Originator o = new Originator();
                o.State = "On";
                o.Show();
                Caretaker c = new Caretaker();
                c.Memento = o.CreateMemento();
                o.State = "Off";
                o.Show();
                o.SetMemento(c.Memento);
                o.Show();
    View Code
  • 相关阅读:
    22:django 配置详解
    21:序列化django对象
    20:django中的安全问题
    19:django 分页
    HTML 标签(一)
    流程图学习绘制
    HTTP原理
    终端的颜色代码
    Python 进程 线程总结
    Python Select模型
  • 原文地址:https://www.cnblogs.com/uptothesky/p/5278386.html
Copyright © 2011-2022 走看看