设计模式----Command Pattern

  • Command Pattern
    将命令的请求者(invoker)与命令的执行者(receiver)解耦。
  • 类图
    在这里插入图片描述
  • 代码示例
/*
 *command Pattern
 * 
 */
using System;

namespace Pattern03
{
    class Program
    {

        static void Main(string[] args)
        {
            // Create receiver, command, and invoker

            Receiver receiver = new Receiver();
            Command command = new ConcreteCommand(receiver);//
            Invoker invoker = new Invoker();
            // Set and execute command

            //请求者 设置command
            invoker.SetCommand(command);
            //执行命令,使用command中的抽象方法
            invoker.ExecuteCommand();
            Console.ReadKey();
        }
    }

    /// <summary>
    /// Command :
    /// declares an interface for executing an operation
    /// </summary>
    abstract class Command
    {
        protected Receiver receiver;//命令接收者
        public Command(Receiver receiver)
        {
            this.receiver = receiver;
        }
        public abstract void Execute();
    }
    /// <summary>
    /// ConcreteCommand:
    /// ##defines a binding between a Receiver object and an action
    /// ##implements Execute by invoking the corresponding operation(s) on Receiver
    /// </summary>
    class ConcreteCommand : Command
    {
        //绑定receiver对象
        public ConcreteCommand(Receiver receiver) : base(receiver) { }
        public override void Execute()
        {
            receiver.Action();///绑定receiver中的方法
        }
    }
    /// <summary>
    /// Receiver:
    /// knows how to perform the operations associated with carrying out the request.
    /// 执行与请求相关的操作,它具体实现对请求的业务处理。
    /// </summary>
    class Receiver
    {
        public void Action()
        {
            Console.WriteLine("Called Receiver.Action()");
        }
    }
    /// <summary>
    /// Invoker :
    /// asks the command to carry out the request
    /// 请求者,它通过命令对象来执行请求
    /// </summary>
    class Invoker
    {
        private Command _command;//带有receiver的command
       
        public void SetCommand(Command command)
        {
            this._command = command;
        }
        public void ExecuteCommand()
        {
            _command.Execute();//通过命令对象来执行请求
        }
    }
}

  • 总结
  1. 解耦命令请求(invoker)与命令执行(receiver)
    ps:
    You can implement undo/redo.
    You can implement deferred execution of operations.
    You can assemble a set of simple commands into a complex
    one.
    The code may become more complicated since you’re introducing
    a whole new layer between senders and receivers.

The client should initialize objects in the following order:
◦ Create receivers.
◦ Create commands, and associate them with receivers if
needed.
◦ Create senders(as Invoker), and associate them with specific commands.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值