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

 

posted on 2009-08-01 18:57  Acor  阅读(187)  评论(0编辑  收藏  举报

导航