WEB开发网
开发学院软件开发C语言 无废话C#设计模式之十八:Command 阅读

无废话C#设计模式之十八:Command

 2009-04-02 08:23:06 来源:WEB开发网   
核心提示: 示例代码using System;using System.Collections.Generic;using System.Text;namespace CommandExample{ class Program { static void Main(string[] args) { M

示例代码

using System;

using System.Collections.Generic;

using System.Text;

 

namespace CommandExample

{

    class Program

    {

        static void Main(string[] args)

        {

            Man man = new Man();

            Server server = new Server();

            server.Execute(new MoveForward(man, 10));

            System.Threading.Thread.Sleep(50);

            server.Execute(new MoveRight(man, 10));

            server.Execute(new MoveBackward(man, 10));

            server.Execute(new MoveLeft(man, 10));

        }

    }

 

    class Man

    {

        private int x = 0;

        private int y = 0;

 

        public void MoveLeft(int i) { x -= i; }

 

        public void MoveRight(int i) { x += i; }

 

        public void MoveForward(int i) { y += i; }

 

        public void MoveBackward(int i) { y -= i; }

 

        public void GetLocation()

        {

            Console.WriteLine(string.Format("({0},{1})", x, y));

        }

    }

 

    abstract class GameCommand

    {

        private DateTime time;

 

        public DateTime Time

        {

            get { return time; }

            set { time = value; }

        }

 

        protected Man man;

 

        public Man Man

        {

            get { return man; }

            set { man = value; }

        }

 

        public GameCommand(Man man)

        {

            this.time = DateTime.Now;

            this.man = man;

        }

 

        public abstract void Execute();

 

        public abstract void UnExecute();

    }

 

    class MoveLeft : GameCommand

    {

        int step;

 

        public MoveLeft(Man man, int i) : base(man) { this.step = i; }

 

        public override void Execute()

        {

            man.MoveLeft(step);

        }

 

        public override void UnExecute()

        {

            man.MoveRight(step);

        }

    }

 

    class MoveRight : GameCommand

    {

        int step;

 

        public MoveRight(Man man, int i) : base(man) { this.step = i; }

 

        public override void Execute()

        {

            man.MoveRight(step);

        }

 

        public override void UnExecute()

        {

            man.MoveLeft(step);

        }

    }

 

    class MoveForward : GameCommand

    {

        int step;

 

        public MoveForward(Man man, int i) : base(man) { this.step = i; }

 

        public override void Execute()

        {

            man.MoveForward(step);

        }

 

        public override void UnExecute()

        {

            man.MoveBackward(step);

        }

    }

 

    class MoveBackward : GameCommand

    {

        int step;

 

        public MoveBackward(Man man, int i) : base(man) { this.step = i; }

 

        public override void Execute()

        {

            man.MoveBackward(step);

        }

 

        public override void UnExecute()

        {

            man.MoveForward(step);

        }

    }

 

    class Server

    {

        GameCommand lastCommand;

 

        public void Execute(GameCommand cmd)

        {

            Console.WriteLine(cmd.GetType().Name);

            if (lastCommand !=null && (TimeSpan)(cmd.Time - lastCommand.Time) < new TimeSpan(0, 0, 0, 0, 20))

            {

                Console.WriteLine("Invalid command");

                lastCommand.UnExecute();

                lastCommand = null;

            }

            else

            {

                cmd.Execute();

                lastCommand = cmd;

            }

            cmd.Man.GetLocation();

        }

    }

}

上一页  1 2 3 4 5  下一页

Tags:废话 设计模式 十八

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接