⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 GOF23种设计模式详细例子!附有详细的代码噢!
💻 CS
字号:
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();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -