zoukankan      html  css  js  c++  java
  • C# 备忘录模式(Memento)

    获取目标对象的当前内部状态,并将状态保存在对象外部,以后可以从将目标对象恢复到保存时的状态。代码如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;


    namespace DesignMode.Memento
    {
        public class MyAim
        {
            private string _state1;
            private int _state2;
            private decimal _state3;

            public decimal State3
            {
                get { return _state3; }
                set { _state3 = value; }
            }

            public int State2
            {
                get { return _state2; }
                set { _state2 = value; }
            }
            public string State1
            {
                get { return _state1; }
                set { _state1 = value; }
            }

            public MyAim()
            {
                this._state1 = "1";
                this._state2 = 1;
                this._state3 = 1.0M;
            }

            public void SetState()
            {
                this._state1 = "2";
                this._state2 = 2;
                this._state3 = 2.0M;
            }

            public void DisplayState()
            {
                MessageBox.Show(" state1:" + State1 + "\n state2:" + State2 + "\n state3:" + State3);
            }

            //保存当前状态
            public MyMemento SaveState()
            {
                return new MyMemento(_state1, _state2, _state3);
            }

            //恢复状态
            public void RecoveryState(MyMemento myMemento)
            {
                this._state1 = myMemento.State1;
                this._state2 = myMemento.State2;
                this._state3 = myMemento.State3;
            }
        }

        //备忘录类,用于保存当前状态
        public class MyMemento
        {
            private string _state1;
            private int _state2;
            private decimal _state3;

            public MyMemento(string state1,int state2,decimal state3)
            {
                this._state1 = state1;
                this._state2 = state2;
                this._state3 = state3;
            }

            public MyMemento()
            { 
            }

            public decimal State3
            {
                get { return _state3; }
                set { _state3 = value; }
            }

            public int State2
            {
                get { return _state2; }
                set { _state2 = value; }
            }
            public string State1
            {
                get { return _state1; }
                set { _state1 = value; }
            }
        }


        //将备忘录对象保存到外部类
        public class ExternalSave
        {
            private MyMemento myMemento;

            public MyMemento MyMemento
            {
                get { return myMemento; }
                set { myMemento = value; }
            }
        }


    }

    客户端代码:

            private void btn_Memento_Click(object sender, EventArgs e)
            {
                
                MyAim aim = new MyAim();
                aim.DisplayState();

                ExternalSave external = new ExternalSave();
                external.MyMemento = aim.SaveState();

                aim.SetState();
                aim.DisplayState();

                aim.RecoveryState(external.MyMemento);
                aim.DisplayState();

            }
  • 相关阅读:
    使用MSXML2::IXMLDOMDocument2Ptr每次都要CreateInstance和load(xmlfile)吗?
    .Net程序安装打包的一些经验贡献
    感慨SQL2005中的数据挖掘算法
    COM客户端没法激活托管代码生成的COM Server的原因
    预感~=命中注定
    创业经理10大必备素质
    全局缓存管理工具
    XML DOM的结构概念图解哪里是Element,哪里是Attribute,哪里是Text
    用GetVolumeInformation得到的不是硬盘的序列号,不要再抄这样的错误好吗?
    站在生活的背后
  • 原文地址:https://www.cnblogs.com/kavilee/p/2359337.html
Copyright © 2011-2022 走看看