Author: Yi Yu Tian (http://blog.csdn.net/dylgsy ). You are welcome to post this article, and please keep this information
In reality, there is an example that can describe the command mode very closely. This is the relationship between the emperor and the soldiers. The Emperor serves as the caller and the soldier is the receiver. If the Emperor wants to give a command to the soldier, does he directly go to the front of the soldier to explain the command details to him, do soldiers execute the command? For the emperor, this is impossible, because for the emperor, he only wants to complete a task, and it is irrelevant to who will help him complete the task. Therefore, the emperor has something called the Holy power, he only needs the next decree, and whoever receives it has nothing to do with him.
In command mode, the Emperor is invoker, the command is Decree, and the soldier is the receiver. As follows:
The following describes the mode.
The command mode includes several key classes: invoker, command, and receiver)
First, let's take a look at how this mode completes collaboration. The caller and receiver are decoupled through the command object. The command has a receiver object, while the invoker calls the unified function execute of the command object, so that the Execute function actually calls the function in the receiver. Here, let's look at why the invoker does not directly call the function in the handler?
Because:
1. In some cases, the caller does not know what the receiver actually is (for example, the emperor does not know who is responsible), so there is a possibility that our receiver will change.
2. In some cases, the receiver's interface is messy, and the caller wants to have a unified interface for him to call. For example, execute and unexecute can be implemented to implement undo operations.
3. For the time being, I can only think of the two reasons for using command. If you know other applications, please let us know!
Next, let's take a look at the code to simulate the emperor and learn how the command code is organized:
# Include <stdio. h>
# Include <iostream>
Using namespace STD;
// Command class)
Class command
...{
Public:
Virtual ~ Command ()
...{
}
// Unified function Interfaces
Virtual void execute () = 0;
};
// Soldier (recipient of the holy message)
Class Cycler
...{
Public:
Receiver (char * pszname)
...{
Strcpy (szname, pszname );
}
Void action ()
...{
Cout <szname <"command! "<Endl;
}
PRIVATE:
// Each soldier has a name
Char szname [20];
};
// Specific purpose 1: capture thieves
Class zhuazeicommand: Public command
...{
Public:
Zhuazeicommand (Explorer * r): _ recver (r)
...{
}
Virtual void execute ()
...{
Cout <"who gets the purpose of stealing? "<Endl;
_ Recver-> action ();
}
PRIVATE:
Receiver * _ recver;
};
// Specific purpose 2-send money
// Where is the purpose of sending money? My dizzy
Class songqiancommand: Public command
...{
Public:
Songqiancommand (receiver * r): _ recver (r)
...{
}
Virtual void execute ()
...{
Cout <"who receives the money? "<Endl;
_ Recver-> action ();
}
PRIVATE:
Receiver * _ recver;
};
// Emperor class
Class invoker
...{
Public:
Invoker (command * C): _ cmd (c)
...{
}
Void invoke ()
...{
_ Cmd-> execute ();
}
PRIVATE:
Command * _ cmd;
};
// This main function is an application. I generally regard the main function as a script of God.
// Isn't it? You can create everything you want.
Void main ()
...{
// There are two soldiers
Extends er r1 ("Yi Yu Tian ");
Extends er R2 ("Deng Yilei ");
// Two holy things, one for the thief and the other for the money
// Send a thief to Yutian
Zhuazeicommand ZC (& R1 );
// Send money to Deng Yilei
Songqiancommand SC (& R2 );
// There is an emperor who wants to subscribe to the decree
// The first line is the purpose of capturing thieves
Invoker I1 (& ZC );
// The second method is to send the money
Invoker I2 (& SC );
// The execution of the decree
I1.invoke ();
I2.invoke ();
}