zoukankan      html  css  js  c++  java
  • Memento 模式

    第三版:

    namespace DemoGame
    {
        class GameRole
        {
            private int shenmingli;
            public int sml
            {
                get { return shenmingli; }
                set { shenmingli = value; }
            }
            private int gongjili;
            public int gjl
            {
                get { return gongjili; }
                set { gongjili = value; }
            }
            private int fangyuli;
            public int fyl
            {
                get { return fangyuli; }
                set { fangyuli = value; }
            }
            public void GetInitialState()
            {
                this.sml = 100;
                this.gjl = 100;
                this.fyl = 100;
            }
            public void DisPlayState()
            {
                Console.WriteLine("生命力:{0}", this.sml);
                Console.WriteLine("攻击力:{0}", this.gjl);
                Console.WriteLine("防御力:{0}", this.fyl);           
            }
            public void Fight()
            {
                this.sml = 0;
                this.gjl = 0;
                this.fyl = 0;
            }

            public RoleStateDemo SaveState()
            {
                return new RoleStateDemo(this.sml,this.gjl,this.fyl);
            }
            public void Restore(RoleStateDemo r)
            {
                this.sml = r.sml;
                this.gjl = r.gjl;
                this.fyl = r.fyl;
            }

        }
        class RoleStateDemo
        {
            private int shengmingli;
            public int sml
            {
                get { return shengmingli; }
                set { shengmingli = value; }
            }
            private int gongjili;
            public int gjl
            {
                get { return gongjili; }
                set { gongjili = value; }
            }
            private int fangyuli;
            public int fyl
            {
                get { return fangyuli; }
                set { fangyuli = value; }
            }
            public RoleStateDemo(int a, int b, int c)
            {
                this.sml = a;
                this.gjl = b;
                this.fyl = c;
            }
        }
        class RoleStateMgr
        {
            private RoleStateDemo rolestatedemo;
            public RoleStateDemo rsd
            {
                get { return rolestatedemo; }
                set { rolestatedemo = value; }
            }

        }
        class Program
        {
            static void Main()
            {
                GameRole Lixiaoyao = new GameRole();
                Lixiaoyao.GetInitialState();
                Console.WriteLine("*****Before Fight*****");
                Lixiaoyao.DisPlayState();

                RoleStateMgr rsm = new RoleStateMgr();

    //            RoleStateDemo rsd = new RoleStateDemo(Lixiaoyao.sml, Lixiaoyao.gjl, Lixiaoyao.fyl);//ugly code!
    //            Lixiaoyao.SaveState();
                rsm.rsd = Lixiaoyao.SaveState();
                Lixiaoyao.Fight();
                Console.WriteLine("*****After Fight*****");
                Lixiaoyao.DisPlayState();

    //            Lixiaoyao.Restore(rsd);
                Lixiaoyao.Restore(rsm.rsd);
                Console.WriteLine("*****After Restore*****");
                Lixiaoyao.DisPlayState();

                Console.Read();

            }

        }
    }

    第二版:

    namespace DemoGame
    {
        class GameRole
        {
            private int shenmingli;
            public int sml
            {
                get { return shenmingli; }
                set { shenmingli = value; }
            }
            private int gongjili;
            public int gjl
            {
                get { return gongjili; }
                set { gongjili = value; }
            }
            private int fangyuli;
            public int fyl
            {
                get { return fangyuli; }
                set { fangyuli = value; }
            }
            public void GetInitialState()
            {
                this.sml = 100;
                this.gjl = 100;
                this.fyl = 100;
            }
            public void DisPlayState()
            {
                Console.WriteLine("生命力:{0}", this.sml);
                Console.WriteLine("攻击力:{0}", this.gjl);
                Console.WriteLine("防御力:{0}", this.fyl);           
            }
            public void Fight()
            {
                this.sml = 0;
                this.gjl = 0;
                this.fyl = 0;
            }

            public RoleStateDemo SaveState()
            {
                return new RoleStateDemo(this.sml,this.gjl,this.fyl);
            }
            public void Restore(RoleStateDemo r)
            {
                this.sml = r.sml;
                this.gjl = r.gjl;
                this.fyl = r.fyl;
            }

        }
        class RoleStateDemo
        {
            private int shengmingli;
            public int sml
            {
                get { return shengmingli; }
                set { shengmingli = value; }
            }
            private int gongjili;
            public int gjl
            {
                get { return gongjili; }
                set { gongjili = value; }
            }
            private int fangyuli;
            public int fyl
            {
                get { return fangyuli; }
                set { fangyuli = value; }
            }
            public RoleStateDemo(int a, int b, int c)
            {
                this.sml = a;
                this.gjl = b;
                this.fyl = c;
            }
        }
        class Program
        {
            static void Main()
            {
                GameRole Lixiaoyao = new GameRole();
                Lixiaoyao.GetInitialState();
                Console.WriteLine("*****Before Fight*****");
                Lixiaoyao.DisPlayState();

     //           RoleStateDemo rsd = new RoleStateDemo(Lixiaoyao.sml, Lixiaoyao.gjl, Lixiaoyao.fyl);//ugly code!
                RoleStateDemo rsd = Lixiaoyao.SaveState();

                Lixiaoyao.Fight();
                Console.WriteLine("*****After Fight*****");
                Lixiaoyao.DisPlayState();

                Lixiaoyao.Restore(rsd);

                Console.WriteLine("*****After Restore*****");
                Lixiaoyao.DisPlayState();

                Console.Read();

            }

        }
    }


    第一版:

    namespace DemoGame
    {
        class GameRole
        {
            private  int shengmingli;
            public int sml
            {
                get { return shengmingli; }
                set { shengmingli = value; }
            }
            private int gongjili;
            public int gjl
            {
                get { return gongjili; }
                set { gongjili = value; }
            }
            private int fangyuli;
            public int fyl
            {
                get { return fangyuli; }
                set { fangyuli = value; }
            }
            public void GetInitialState()
            {
                this.sml = 100;
                this.gjl = 100;
                this.fyl = 100;
            }
            public void Fight()
            {
                this.sml = 0;
                this.gjl = 0;
                this.fyl = 0;
            }
            public void DisPlayState()
            {
                Console.WriteLine("生命力:{0}", this.sml);
                Console.WriteLine("攻击力:{0}", this.gjl);
                Console.WriteLine("防御力:{0}", this.fyl);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                GameRole Lixiaoyao = new GameRole();
                Lixiaoyao.GetInitialState();
                Console.WriteLine("*****Before Fight*****");
                Lixiaoyao.DisPlayState();
               

                GameRole backup = new GameRole();//客户端需要知道所有细节。
                backup.sml = Lixiaoyao.sml;             //通过新建一个对象,来保存所有细节。
                backup.gjl = Lixiaoyao.gjl;
                backup.fyl = Lixiaoyao.fyl;

                Lixiaoyao.Fight();
                Console.WriteLine("*****After Fight*****");
                Lixiaoyao.DisPlayState();

                Lixiaoyao.sml = backup.sml;
                Lixiaoyao.gjl = backup.gjl;
                Lixiaoyao.fyl = backup.fyl;
                Console.WriteLine("*****After Restore*****");
                Lixiaoyao.DisPlayState();

                Console.Read();
            }
        }
    }

  • 相关阅读:
    Windows上安装配置SSH教程(3)——在Windows系统上安装与配置WinSCP
    Windows上安装配置SSH教程(2)——在Windows XP和Windows 10上安装并配置OpenSSH for Windows
    Windows上安装配置SSH教程(1)——知识点汇总
    Windows上安装配置SSH教程(5)——win10下使用Cygwin+Expect自动登陆ssh
    Win10安装cygwin并添加apt-cyg
    Windows上安装配置SSH教程(4)——WinSCP+OpenSSH 使用公钥自动登陆
    地线干扰与抑制(转)
    AMBA总线协议AHB、APB
    springcloud(六):配置中心git示例
    springcloud(四):熔断器Hystrix
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1536650.html
Copyright © 2011-2022 走看看