Design Mode (10) --- command mode

Source: Internet
Author: User

I. INTRODUCTION:

There is a barbecue stall on the side of the road, and a boss is responsible for barbecue food for the guests. If there are few guests, the boss can accurately remember everyone's requirements. As the number of guests increases, the number of requirements increases, making it difficult for the boss to meet everyone's requirements. What should the boss do?

First of all, I would like to explain why there are too many guests and the boss cannot meet everyone's requirements, because "in the words of programmers, it is due to tight coupling ". Loose coupling can solve this problem, that is, the command mode described in this article. The tightly coupled code is provided first:

Class Program {static void Main (string [] args) {extends er = new extends er (); extends er. bakeChickenWing (); Console. read () ;}} public class extends er // boss {public void BakeMutton () {Console. writeLine ("bake mutton");} public void BakeChickenWing () {Console. writeLine ("bake chickenwing ");}}
View Code

 

II. Definition:

Command mode: 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.

Explanation: The request command is encapsulated as an object and recorded in logs. It supports command revocation.

 

III. UML diagram and basic code:

Basic code:

Class Receicer {public void Action () {Console. writeLine ("execution request") ;}} abstract class Command {protected Receicer extends ER; public Command (Receicer extends er) {this. extends er = extends ER;} abstract public void Execute ();} class ConcreteCommand: Command {public ConcreteCommand (Receicer extends er): base (extends er) {} public override void Execute () {extends er. action () ;}} class Invoker {private Command command; public void SetCommand (Command command) {this. command = command;} public void ExecuteCommand () {command. execute ();}}

Call:

Receicer receiver er = new Receicer (); Command command = new ConcreteCommand (receiver er); Invoker invoker = new Invoker ();
Invoker. SetCommand (command );

Invoker. ExecuteCommand ();

Explanation: maps guests to the hotel for meals. The Receiver is the Receiver of the Command, which is equivalent to the chef in the hotel; the Command abstract class. The ConcreteCommand is a specific Command, which is equivalent to a la carte request. The Command should contain the Receiver of the Command. Invoke is a courier of commands. It is equivalent to a hotel attendant who records the order request and sends it to the kitchen.

 

4. Examples:

The school's senior one students undergo military training. The principal issued a 10000-meter command for the students. Throughout the process: the principal issues a command to the military training instructors, the instructors send the command to the students, and the students receive the command and execute the corresponding operations. In the following example, the runner is a student and runs a 10000-meter command. Command is a Command, and the receiver of the Command must be known. The Drillmaster is an instructor and the instructor must know what the command is and send the command.

// The principal issues a 1000-meter command for the student. The instructor sends the command to the student. The student is the receiver of the command. // The client is the principal, the sender of the command must know the specific command, Receiver, and sender class Program {static void Main (string [] args) {receiver er extends er = new Receiver er (); command command = new ConcreteCommand (reset er); Drillmaster drillmaster = new Drillmaster (command); drillmaster. executeCommand (); Console. read () ;}// command Receiver public class extends er {public void Run1000Meters () {Console. writeLine ("running 10000 meters") ;}// abstract Command public abstract class Command {protected extends er extends ER; public Command (extends er) {this. extends er = extends ER;} public abstract void Action () ;}// for specific commands, you must know the receiver public class ConcreteCommand: Command {public ConcreteCommand (extends er): base (extends er) {} public override void Action () {extends er. run1000Meters () ;}}// Instructor: the passer of the Command, responsible for calling the method of the command object to ensure that the Command is executed by the public class Drillmaster {public command Command command; public Drillmaster (Command command Command) {this. command = command;} public void ExecuteCommand () {command. action ();}}
View Code

 

V. Solve the problems in the introduction

The recipient of the order, the boss (chef ):

Public class extends er {public void BakeMutton () {Console. WriteLine ("bake mutton");} public void BakeChickenWing () {Console. WriteLine ("bake chickenwing ");}}

 

Ordering command:

Public abstract class Command {protected extends er extends ER; public Command (extends er) {this. extends er = extends ER;} abstract public void Execute ();} class ConcreteCommand1: Command {public ConcreteCommand1 (extends er): base (extends er) {} public override void Execute () {extends er. bakeMutton () ;}} class ConcreteCommand2: Command {public ConcreteCommand2 (extends er): base (extends er) {} public override void Execute () {extends er. bakeChickenWing ();}}

 

Add a waiter to record the order of the guests and send them to the chefs. The ordering command can be added or canceled.

Public class Invoker {private IList <Command> commands = new List <Command> (); public void AddCommand (Command command) {commands. add (command); Console. writeLine ("add order" + command. toString ();} public void CancelCommand (Command command) {commands. remove (command); Console. writeLine ("Cancel order" + command. toString ();} public void ExecuteCommand () {foreach (Command command in commands) {command. execute ();}}}

 

Client call:

Extends er = new extends er (); Command command1 = new ConcreteCommand1 (extends er); Command command2 = new ConcreteCommand2 (extends er); Invoker invoker = new Invoker (); invoker. addCommand (command1); invoker. addCommand (command2); invoker. cancelCommand (command1); invoker. executeCommand ();

 

VI. Advantages and disadvantages and applicable scenarios

Advantages:

It is easier to design a command queue. When necessary, it is easier to include the command into the log. The party that allows receiving the request decides whether to reject the request; it is easier to undo and redo commands. Because adding a new command class does not affect other classes, it is easier to add a new command class.

Disadvantages:

The system may have too many specific command classes.

 

Applicable scenarios:

Personal Understanding: The command mode can be used to show its advantages.

Design Mode (10) --- 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.