Command mode (c + + commands mode)

Source: Internet
Author: User

Re-review GOF23 to the 19th command mode, write it down to facilitate your understanding

It's better to use the manager, the Secretary, the post Office to understand the pattern.

Manager: Hello, Xiao Li, here is a letter, please help me to send it ASAP ...
Clerk: OK, manager, I'll do it right away!
The Secretary took the letter and sent the letter to a post office. The post office eventually delivered the letter to the addressee.
This is a very common life work scene, but in this: manager, Secretary and Post office, three unconsciously, the interpretation of an object-oriented design mode: Command mode. Command mode is the first behavioral design pattern I'm going to talk about.
The contents of the command pattern are roughly the same:
Encapsulates a request into an object (Command object) and assigns an execution interface to the object. The command object does not necessarily do this on its own, but instead forwards the request to another object that really does it (receiver object), and the receiver object eventually finishes the request operation. The command pattern enables decoupling between the request initiator and the request recipient.
object to object to, more abstract, think of the secretary Xiao Li is much better: the manager needs to initiate a request (send a letter), he found Xiao Li (Command object), to her to do this thing. Xiao Li will request to the Post office (Receiver object) to finalize the letter delivery task. Throughout the process, the manager believes that the execution of his orders is Xiao Li, he does not care about or do not know which post Office final delivery of this letter, even if it is Xiao Li himself errands or the letter to the recipient, the manager does not care.

The command mode is a bit:

1. Be able to design a command queue easily;

2. If required, it is easier to log commands into the logs.

3. The revocation and redo of the request can be implemented easily.

4. Since the addition of the new specific command class does not affect other classes, it is easy to add new specific command classes.

Wrote a small demo, the code is as follows:

Command.h

Author:jeson yang//file:command.h// date:2014.11.6//////////////////////////////////////////////////////////////////////////#include <iostream > #include <vector>using namespace Std;class reciever{public:void Action () {cout << "do Action!!" << Endl;}}; Class Icommand{public:virtual ~icommand () {}virtual void Excute () = 0;protected:icommand () {}};class Read_command: Public Icommand{public:read_command (reciever *rev): M_rev (rev) {}virtual void Excute () {cout << "Read Command ..." << endl;m_rev->action ();} ~read_command () {}private:reciever *m_rev;}; Class Write_command:public Icommand{public:write_command (reciever *rev): M_rev (rev) {}virtual void Excute () {cout < < "Read Command:" << endl;m_rev->action ();} ~write_command () {}private:reciever *m_rev;}; Class Invoker{public:invoker (icommand* cmd): m_cmd (cmd) {}invoker () {}~invoker () {delete m_cmd;} void Notify () {Std::vector<icoMmand*>::iterator it = Cmdlist.begin (); for (It;it! = Cmdlist.end (); ++it) {m_cmd = *it;m_cmd->excute ()}} void AddCmd (icommand* pcmd) {cmdlist.push_back (pcmd);} void Delcmd (icommand* pcmd) {//cmdlist.pop_back ();} private:icommand* m_cmd;std::vector<icommand*> cmdlist;};


Main.cpp

#include <iostream> #include <vector> #include "command.h" using namespace std;/////////////////////////// Author:jeson yang//file:main.cpp//date:2014.11.6////////////// int main () {reciever* rev = new Reciever (); icommand* cmd1 = new Read_command (rev); icommand* cmd2 = new Write_command (rev); Invoker Inv;inv. AddCmd (CMD1); Inv. AddCmd (CMD2); Inv. Notify (); System ("pause"); return 0;}


08 on test Pass.

Command mode (c + + commands 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.