首页 > 其他分享 >命令模式

命令模式

时间:2023-06-03 21:46:45浏览次数:33  
标签:object 模式 命令 Command Receiver command public receiver

The Command design pattern encapsulates a request as an object, thereby letting you paramize clients with different requests, queue or log requests, and support undoable operations.

命令模式封装请求作为一个对象,因此让你参数化客户端用不同的requests,队列或者日志requests,并且支持撤销操作。

UML Class Diagram

The client will create the command object, the command object involves three things. First, the command object has the request. sencond, it also has the Receiver Object Referece, the Receiver object is nothing but the object which will handle the request. Third,the command object alos has the excute method, the excute method will call the receiver object method and the receiver object method will handle the request.

As per the Command Design Pattern, the Command Object will be passed to the Invoker object. The Invoker Object class not know how to handle the request. What the Invoker will do is ,it will call the Execute method of the Command Object.The Execute method of the command will call the Receiver Object Method. The Receiver Object Method will perform the necessary action to handle the request.

 Receiver: This is a class that contains the actual implementation of the method that the client wants to call.

 Command: This is going to be an interface that specifies the excute operation.

 ConcreteCommand: There are going to be classes that implement the ICommand interface and provide implementations for the excute operation.As part of the Excute method, It is going to invoke operations on the Receiver object.

  Invoker: The Invoker is going to be a class and asks the command to carry out the action.

  Clilent: This is the class that creates and executes the command object.

Structure Code in C#

    public abstract class Command
    {
        protected Receiver receiver;
        public Command(Receiver receiver)
        {
            this.receiver = receiver;
        }
        public abstract void Execute();
    }

    public class ConcreteCommand : Command
    {
        public ConcreteCommand(Receiver receiver):base(receiver) { }

        public override void Execute()
        {
            receiver.Action();
        }
    }
Command
    public class Receiver
    {
        public void Action() 
        {
            Console.WriteLine($"Called Receiver.Action()");
        }
    }
Receiver
    public class Invoker
    {
        public Command command;
        public void SetCommand(Command command)
        {
            this.command = command;
        }
        public void ExecuteCommand()
        {
            command.Execute();
        }
    }
Invoker

 

When to use Command Design Pattern in Real-time Application?

  • When you need to parameterize objects according to the action performed.
  • When you need to create and execute requests at different times.
  • Sending requests to different receivers can handle in different ways.
  • When you need to support rollback, logging, or transaction functionality.
  • When you need to implement callback functionality
  • The source of the request should be decoupled from the object that actually handles the request.

标签:object,模式,命令,Command,Receiver,command,public,receiver
From: https://www.cnblogs.com/qindy/p/17415211.html

相关文章

  • 解释器模式
    Givealanguage,theInterpreterdesignpatterndefinesarepresentationforitsgrammaralongwithaninterpreterthatusestherepresentationtointerpretsentencesinthelanguage.TheInterpreterDesignPatternprovidesawaytoevaluatelanguagegram......
  • 组合模式
    TheCompositedesignpatterncomposesobjectsintotreestructurestorepresentpart-wholehierarchies.Thispatternletsclientstreatindividualobjectandcompositionsofobjectsuniformly.组合模式将对象组合成tree结构代表部分-整体层次结构,这种模式允许客户......
  • 装饰器模式
    TheDecoratorDesignPatternattachesadditionalresponsibilitiestoanobjectdynamically.Thispatternprovideaflexiblealternativetosubclassingforextendingfunctionality.装饰器模式动态的给Object添加额外的职责,这个模式为SubClassing提供灵活的扩展功能。......
  • 外观模式
    TheFacade designpattenprovidesaunifiedinterfacetoasetofinterfacesinasubsystem.Thispatterndefinesahigher-levelinterfacethatmakesthesubsystemeasiertouse.外观模式为子系统一组接口提供了统一的接口,这种模式定义了高级接口,便于子系统调用。......
  • 适配器模式
    TheAdpativedesignpatternconvertstheinterfaceofaclasstoanotherinterfaceclientsexpect.Thisdesignpatternletsclassesworktogetherthatcouldn'totherwisebecauseofincompatibleinterfaces.适配器模式将类的接口转换为客户期望的另外一个接口,这种......
  • 桥接模式
    TheBridgedesignpatterndecouplesanabstractionfromitsimplementationsothathetwocanvaryindependently.桥接模式将抽象和实现解耦,以便两者可以独立变化。UMLClassDiagram Abstraction:definestheabstraction'sinterface;matainsareferencetoanobj......
  • 模板方法模式
    TheTemplateMethoddesignpatterndefinestheskeletonofanalgorithminanoperation,deferingsomestepstosubclasses.Thispatternletssubclassesredefinecertainstepsofanalgorithmwihoutchangingthealgorithm'sstructure.模板方法设计模式在操作中......
  • 创建型设计模式
    TheCreationalDesignPatternareCategorizedintotwotypes. Object-CreationalPatterns:Object-CreationalPatternsdealwithobjectcreation.Here,itdeferspartofitsobjectcreationtoanotherobject.Class-CreationalPatterns:Class-CreationalPa......
  • 策略模式
    TheStrategydesignpatterndefinesafamiliyofalgorithms,encapsulateeachone,andmaketheminterchangeable.Thispatternletsthealgorithmvaryindependentlyfromclientthatuseit.策略模式定义一系列算法,封装它,使他们可以互换,这种设计模式使算法独立于客......
  • linux 中awk命令实现输出匹配字符的上下若干行
     001、[root@PC1test3]#lstest.txt[root@PC1test3]#cattest.txt##测试数据jjjjkkkgenejjjddddyyyiiiipppffff999genettteeeemmmaaaannn[root@PC1test3]#awk'BEGIN{idx=0}{ay1[NR]=$0;if($1=="......