命令模式是对命令的封装,目的就是要把发出命令和执行命令分割开来。

  以下的例子,用校长发送命令给学生来来说明命令发送的整个过程,其中老师作为命令的中转站必不可少。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// 命令接受者学生接口
        /// </summary>
        public abstract class Student
        {
            public abstract void Action();
        }

        /// <summary>
        /// 命令接受者学生A
        /// </summary>
        public class StudentA : Student
        {
            public override void Action()
            {
                Console.WriteLine("StudentA do the action");
            }
        }

        /// <summary>
        /// 命令接受者学生B
        /// </summary>
        public class StudentB : Student
        {
            public override void Action()
            {
                Console.WriteLine("StudentB do the action");
            }
        }

        /// <summary>
        /// 命令抽象类
        /// </summary>
        public abstract class Command
        {
            protected Student student;

            public Command(Student student)
            {
                this.student = student;
            }

            public abstract void Execute();
        }

        /// <summary>
        /// 具体命令
        /// </summary>
        public class ConcreteCommand : Command
        {
            public ConcreteCommand(Student student) :
                base(student)
            {
                
            }
            public override void Execute()
            {
                student.Action();
            }
        }

        /// <summary>
        /// 命令请求者
        /// </summary>
        public class Teacher
        {
            private Command command;

            public void SetCommand(Command command)
            {
                this.command = command;
            }

            public void ExecuteCommand()
            {
                command.Execute();
            }
        }

        /// <summary>
        /// 客户端 如校长
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            StudentA sa = new StudentA();
            StudentB sb = new StudentB();
            Command c1 = new ConcreteCommand(sa);
            Command c2 = new ConcreteCommand(sb);
            Teacher teacher = new Teacher();

            teacher.SetCommand(c1);
            teacher.ExecuteCommand();

            teacher.SetCommand(c2);
            teacher.ExecuteCommand();
        }
    }
}

  如果要执行一串命令可以用list数据结构进行封装。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// 命令接受者学生接口
        /// </summary>
        public abstract class Student
        {
            public abstract void Action();
        }

        /// <summary>
        /// 命令接受者学生A
        /// </summary>
        public class StudentA : Student
        {
            public override void Action()
            {
                Console.WriteLine("StudentA do the action");
            }
        }

        /// <summary>
        /// 命令接受者学生B
        /// </summary>
        public class StudentB : Student
        {
            public override void Action()
            {
                Console.WriteLine("StudentB do the action");
            }
        }

        /// <summary>
        /// 命令抽象类
        /// </summary>
        public abstract class Command
        {
            protected Student student;

            public Command(Student student)
            {
                this.student = student;
            }

            public abstract void Execute();
        }

        /// <summary>
        /// 具体命令
        /// </summary>
        public class ConcreteCommand : Command
        {
            public ConcreteCommand(Student student) :
                base(student)
            {
                
            }
            public override void Execute()
            {
                student.Action();
            }
        }

        /// <summary>
        /// 命令请求者
        /// </summary>
        public class Teacher
        {
            private List<Command> command;

            public void SetCommand(List<Command> command)
            {
                this.command = command;
            }

            public void ExecuteCommand()
            {
                foreach (var c in command)
                {
                    c.Execute();
                }
            }
        }

        /// <summary>
        /// 客户端 如校长
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            StudentA sa = new StudentA();
            StudentB sb = new StudentB();
            Command c1 = new ConcreteCommand(sa);
            Command c2 = new ConcreteCommand(sb);

            List<Command> command = new List<Command>();
            command.Add(c1);
            command.Add(c2);

            Teacher teacher = new Teacher();

            teacher.SetCommand(command);
            teacher.ExecuteCommand();
     }
    }
}