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();

            }
  • 相关阅读:
    Kafka Tuning Recommendations
    ​Installing the Ranger Kafka Plug-in
    Problem of Creating Topics in Kafka with Kerberos
    Step by Step Recipe for Securing Kafka with Kerberos
    ERROR:"org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for /config/topics/test" when creating or deleting Kafka operations authorized through the Ranger policies
    Scala操作Hbase空指针异常java.lang.NullPointerException处理
    使用ranger对kafka进行鉴权
    IBM developer:Setting up the Kafka plugin for Ranger
    IBM developer:Kafka ACLs
    Exception in thread "main" org.I0Itec.zkclient.exception.ZkAuthFailedException: Authentication failure is thrown while creating kafka topic
  • 原文地址:https://www.cnblogs.com/kavilee/p/2359337.html
Copyright © 2011-2022 走看看