Design Pattern Nine: Command Pattern

      Command Pattern (Command Pattern): It is a highly cohesive pattern.

      Definition: Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or long requests, and support undoable operations. Queue requests or record request logs, and can provide command undo and recovery functions.)

     General class diagram:

     

        Receive receiver role: The command passed here should be executed.

        Command role: All commands that need to be executed are declared here.

        Invoker caller role: receives commands and executes them.

        advantage:

        1. Inter-class decoupling: There is no dependency between the role of the caller and the role of the receiver. The caller only needs to call the execute method of the Command abstract class to realize the function, and does not need to know which receiver it is

        2. Scalability: The subclass of Command can be extended very easily, and the caller Invoker and the high-level module Client do not produce serious code coupling

        shortcoming:

        If there are N commands, there may be N subclasses of Command, causing the class to expand very large

       accomplish:

      

/// <summary>
    /// 通用Receive类
    /// </summary>
    public abstract class Reveiver
    {
        //抽象接收者,定义每个接收者都必须完成的业务
        public abstract void doSomething();
    }

    /// <summary>
    /// 具体的Receiver类
    /// </summary>
    public class ConcreteReciver1 : Reveiver
    {
        //每个接收者都必须处理一定的业务逻辑
        public void doSomething()
        { 
        }
    }
    public class ConcreteReciver2 : Reveiver
    {
        //每个接收者都必须处理一定的业务逻辑
        public void doSomething()
        { 
        }
    }
    /// <summary>
    /// 抽象的Command类
    /// </summary>
    public abstract class Command
    {
        //每个命令类都必须有一个执行命令的方法
        public abstract void execute();
    }
    /// <summary>
    /// 具体的Command类1
    /// </summary>
    public class ConcreteCommand1 : Command
    {
        private Receiver receiver;

        public ConcreteCommand1(Receiver _receiver)
        {
            receiver = _receiver;
        }

        public void execute()
        {
            receiver.doSomething();
        }
    }

    /// <summary>
    /// 具体的Command类2
    /// </summary>
    public class ConcreteCommand2 : Command
    {
        private Receiver receiver;

        public ConcreteCommand1(Receiver _receiver)
        {
            receiver = _receiver;
        }

        public void execute()
        {
            receiver.doSomething();
        }
    }
    /// <summary>
    /// 调用者Invoker类
    /// </summary>
    public class Invoker
    {
        private Command command;
        
        public void SetCommand(Command _command)
        {
            command = _command;
        }
        //执行命令
        public void  actiong()
        {
            command.execute();
        }
    }

    public class Client
    {
        public static void main(String [] args)
        {
            //首先声明调用者Invoker
            Invoker Invoker = new Invoker();

            //定义接收者
            Receiver receiver  = new ConcreteReciver1();

            //定义一个发送接收者的命令
            Command command  = new ConcreteCommand1(receiver);

            //把命令交给调用者去执行
            Invoker.SetCommand(command);
            invoker.action();
        }
    }
     Well, that's all for this chapter. Welcome everyone to join the QQ group: 280993838. Or pay attention to my official account:

Guess you like

Origin blog.csdn.net/zjw1349547081/article/details/54889720