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

  • 相关阅读:
    iOS 整理面试题(上)
    2021年十大白马名单
    RabbitMQ:消息重复消费
    RabbitMQ:保证消息的顺序性
    RabbitMQ:保证消息的可靠性传输
    AWS S3 大文件分片上传
    rebase 用法小结
    Flask at scale
    MySQL分区
    动态规划示例题
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1536650.html
Copyright © 2011-2022 走看看