Step by step. NET Design Mode Study Notes 9. Command (Command mode)

Source: Internet
Author: User

Overview

In software systems, "behavior requestor" and "behavior implementers" are usually "tightly coupled ". However, in some cases, such as "record, undo/Redo, and transaction" operations, it is not suitable for tight coupling that cannot withstand changes. In this case, how can we decouple "behavior requestor" from "behavior implementer? Abstract A group of behaviors as objects to achieve loose coupling between them. This is the Command mode described in this article.

Intention

Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support unrecoverable operations. [GOF design patterns]

Structure chart

<Design Pattern> the Command mode structure is as follows:

Figure 1 Structure of Command mode

Example in Journey to the West: Emperor Yu Chuan Mei Monkey King heaven

The command mode is not a new invention. It was created before the Monkey King made a noise. At that time, the Yudi command too white Venus called the Monkey King heaven: "Venus diameter (Water Curtain Cave) in the middle, south of the line: I am the west too white Venus, with the Yudi Zhao an Decree, the lower bound please go big, worship xianlu. "Yu Di is the system client, Taibai Venus is the sender of the command, Monkey King is the receiver of the command, and the holy command is the command. This command of Yudi requires the Monkey King to report to the upper bound. The Jade Emperor only sends commands, no matter how the commands are delivered to the Monkey King. Venus in Taibai is responsible for transmitting the holy decree, but how the Monkey King executes the holy decree and when it will be the Monkey King's own affairs. Otherwise, the Monkey King of JIU Mei will make a big noise in tiangu.

The simulation system is designed as follows:

Examples in life

In Command mode, a request is encapsulated as an object, so that you can use different requests to parameterize the customer. The bill for dining is an example of the Command mode. The waiter accepts the customer's order and writes it into the bill for encapsulation. This ticket is queued for cooking. Note that the "bill" here is not dependent on the menu, it can be used by different customers, so it can be added to different order items.

Figure 2 use the Command mode object diagram of the dining example

Example use case structure:

The mobile phone operating system includes the functions of starting, shutting down, making phone calls, making phone calls, sending text messages, and deleting text messages. In fact, this is a command mode and its structure is as follows:

 

First create the interface ICommand. cs:

Public interface ICommand {// <summary> /// Execute the command // </summary> string Execute (); /// <summary> /// undo command /// </summary> string UnExecute ();}

 

Create SystemCommand. cs again:

Public class SystemCommand {public string StartUp () {return "";} public string ShutDown () {return "";} public string SendSMS () {return "text message";} public string RemoveSMS () {return "delete text message";} public string CallPhone () {return "call";} public string RingOff () {return "phone call ";}}

 

Create MobileSystem. cs again:

Public abstract class MobileSystem: ICommand {private SystemCommand mobileCommand; public SystemCommand MobileCommand {get {return mobileCommand;} set {mobileCommand = value;} public MobileSystem (SystemCommand command) {this. mobileCommand = command ;}# region ICommand member public abstract string Execute (); public abstract string UnExecute (); # endregion}

 

Create StartUp. cs again:

Public class StartUp: MobileSystem {# region MobileSystem member public override string Execute () {return MobileCommand. startUp ();} public override string UnExecute () {return MobileCommand. shutDown () ;}# endregion public StartUp (SystemCommand command): base (command ){}}

 

Create Call. cs again:

Public class Call: MobileSystem {# region MobileSystem member public override string Execute () {return MobileCommand. callPhone ();} public override string UnExecute () {return MobileCommand. ringOff () ;}# endregion public Call (SystemCommand command): base (command ){}}

 

Create SMS. cs again:

Public class SMS: MobileSystem {# region MobileSystem member public override string Execute () {return MobileCommand. sendSMS ();} public override string UnExecute () {return MobileCommand. removeSMS () ;}# endregion public SMS (SystemCommand command): base (command ){}}

 

 

Create MobileServer. cs again:

  public class MobileServer : ICommand    {       private MobileSystem mobileSystem;       public MobileServer(MobileSystem system)       {           this.mobileSystem = system;       }       public string Execute()       {         return  mobileSystem.Execute();       }       public string UnExecute()       {           return mobileSystem.UnExecute();       }    }

 

Finally, call:

 public partial class Run : Form    {        public Run()        {            InitializeComponent();        }        private void btnRun_Click(object sender, EventArgs e)        {            SystemCommand command = new SystemCommand();            MobileServer server = new MobileServer(new StartUp(command));            rtbResult.AppendText(server.Execute() + "");            rtbResult.AppendText(server.UnExecute() + "");            server = new MobileServer(new Call(command));            rtbResult.AppendText(server.Execute() + "");            rtbResult.AppendText(server.UnExecute() + "");            server = new MobileServer(new SMS(command));            rtbResult.AppendText(server.Execute() + "")

Related Article

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.