Design Mode---command mode

Source: Internet
Author: User

Objective

Long time no Bo, the recent life has undergone a small change, my dear baby came to the world, I am also a happy father. After being a father, we should do some things to do the father, take care of the baby, housework that is indispensable, learning time is naturally reduced. Of course, they did not stop the pace of learning, this time also have to see Pluralsight class, revisit the ASP. NET Security, Enterprise Architecture design, EF and WCF-related things, but all is fragmented, after all, life is busy, leaving the study time is generally more fragmented. Blogging itself needs a chunk of time to think about it, so the amount of blogging goes down. However, habits always have to persist, even if busy, one months at least one article

Business

Back to the point, this article on the design mode of the command mode, we look at the official explanation:

abstract a set of behaviors into objects to achieve loose coupling between them. This is the command pattern .

Let's take a look at the explanation of the foreign language community:

Encapsulate a request as an object, thereby letting-parameterize clients with different requests, queue or log request s, and support undoable operations

Encapsulates a request as an object, allowing you to parameterize a client call based on a different request, queue, or log request, and to support undo operations

Personal preference for the latter explanation

UML Class Diagram

Participants
    • commands, Command interface
    • Concretecommand, the concrete command implements the object, the virtual implementation, usually holds the receiver, and invokes the receiver's function to complete the command to perform the operation
      • Binding recipient and Action behavior
      • Implement the command interface by invoking the corresponding operation on receiver
    • Client, create a specific command object, and set the recipient of the Command object, note that this is not the client in our regular sense, but the assembly of the Command object and the receiver, perhaps, this client is known as the assembler will understand better, Because the client that actually uses the command is from invoker to trigger execution
    • Invoker, which requires the command object to execute the request, usually holds the Command object and can hold a lot of command objects. This is where the client actually triggers the command and asks the command to perform the appropriate action, which is equivalent to using the Command object's entry
    • Receiver, receiver, the object that actually executes the command. Any class can be a recipient, as long as it implements the appropriate functionality required by the command
Code implementation
    • Command interface
Namespace command{    abstract class Command    {        protected receiver receiver;        Constructor Public        Command (receiver receiver)        {            this.receiver = receiver;        }        public abstract void Execute ();}    }
    • Receiver
namespace command{//<summary>//The    ' receiver ' class///    </summary>    class receiver    {public        void Action ()        {            Console.WriteLine ("Called Receiver.action ()");    }}}
    • Specific commands
Namespace command{    class Concretecommand:command    {        //Constructor public        Concretecommand ( Receiver receiver):            base (receiver)        {        } public        override void Execute ()        {            receiver. Action ();}}}    
    • Invoker Command Call entry
namespace command{//<summary>//The    ' Invoker ' class///    </summary>    class Invoker    {        private Command _command;        public void SetCommand (Command command)        {            this._command = command;        }        public void ExecuteCommand ()        {            _command. Execute ();}}}    
    • Client assembly execution
Namespace command{    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            invoker. SetCommand (command);            Invoker. ExecuteCommand ();            Wait for user            Console.readkey ();}}    }
Conclusion

To tell the truth, so far in the actual work did not use this mode, it is to see others used. Decoupling the request from the implementation, in some large distributed systems, does have its advantages. For example, a one-way asynchronous request message encapsulated as a command, using distributed messaging components for communication and processing, such as some DB cud operation, etc., can be unified programming model, easy to maintain and manage

Design Mode---command mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.