Design Mode commands
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 the Undo operations.
Have you ever written a letter? Have you sent a mail to the post office? Who sent the first token you wrote? Post offices in daily life may have become a landscape in the eyes of people. Currently, few people use letters to communicate and express their feelings. However, as a transit agency for the sender and receiver, the post office can not be underestimated, saving the direct communication between the sender and receiver and sending messages to the sender and receiver. Today, let's repeat the text and post office times...
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Command {// mail Receiver interface public interface IReceiver {// The recipient receives the mail and reads void readMail (String message);} // The Mail Receiver public class Referer: IReceiver {public void readMail (String message) {Console. writeLine ("recipient reads mail:" + message) ;}// Post Office interface public interface IPost {// Post Office sends mail void sendMail (Stri Ng message);} public class Post: IPost {private IReceiver receiver; public Post (IReceiver receiver) {this. extends ER = extends er;} public void sendMail (string message) {Console. writeLine ("the post office sends a letter to the recipient... "); this. aggreger. readMail (message) ;}}// sender public class Invoker {private IPost post; public void setPost (IPost post) {this. post = post;} // The sender sends the public void postMail (string message) {Con Sole. writeLine ("the sender delivers the mail to the post office... "); this. post. sendMail (message) ;}} class Program {static void Main (string [] args) {er Receiver = new receiver (); IPost post = new Post (Receiver ); invoker invoker = new Invoker (); invoker. setPost (post); invoker. postMail ("hello, I haven't seen you for a long time. Are you busy working recently? ");}}}
Class diagram: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vade + partition = "http://www.2cto.com/uploadfile/Collfiles/20141006/20141006090618155.png" alt = "[k? Rough Argon? F? Too many? Why? ^ Why jw rough Argon? 'Audio-extract failed? Too many? K? Why? Why? U blow ky "http://www.bkjia.com/ OS/" target = "_ blank" class = "keylink"> system coupling.
2. You can easily design a command queue or macro command (combined command ).
3. It provides a design and implementation solution for Undo and Redo operations.
Main disadvantages:Using the command mode may cause some systems to have too many specific command classes. Because a specific command class is designed for each call operation to the request receiver, a large number of specific command classes may be required in some systems, which will affect the use of the command mode.
Usage:1. abstract the action to be executed to parameterize an object. Similar to the callback mechanism in process design, the command mode is an object-oriented alternative to the callback mechanism.
2. Specify, sort, and execute requests at different times.
3. Supports undo operations.
4. You need to support the log modification function. When the system crashes, these modifications can be redone.
Related mode:1. Combination: The combination mode is used when macro commands are used.
2. Memorandum: it can be used to store command effect status information for command revocation and restoration.