Design Pattern Command Design mode, designpattern

Source: Internet
Author: User

Design Pattern Command Design mode, designpattern

This design mode is to use different classes to package different commands, to achieve what operation to achieve.

You can also use map to connect to your favorite command words.

An execution class actually contains all required operations, such:

class SuperMaker{public:string makeCar(){return "Car";}string makePlane(){return "Plane";}};

There are two commands. The following is to use different classes to include the required commands.

class Command{protected:SuperMaker *sm;public:virtual void exeCmd() = 0;};class MakeCarCmd : public Command{public:MakeCarCmd(SuperMaker *s){sm = s;}void exeCmd(){cout<<sm->makeCar()<<std::endl;}};class MakePlaneCmd : public Command{public:MakePlaneCmd(SuperMaker *s){sm = s;}void exeCmd(){cout<<sm->makePlane()<<std::endl;}};

Here, the MakeCarCmd and MakePlaneCmd classes implement different commands respectively. Different operations can be implemented using different classes.

How to implement these classes (commands) can be defined by yourself. For example, map can be used to correspond to meaningful strings, you can also use a class package to include these commands again.


All code:

#include <iostream>#include <string>using std::string;using std::cout;class SuperMaker{public:string makeCar(){return "Car";}string makePlane(){return "Plane";}};class Command{protected:SuperMaker *sm;public:virtual void exeCmd() = 0;};class MakeCarCmd : public Command{public:MakeCarCmd(SuperMaker *s){sm = s;}void exeCmd(){cout<<sm->makeCar()<<std::endl;}};class MakePlaneCmd : public Command{public:MakePlaneCmd(SuperMaker *s){sm = s;}void exeCmd(){cout<<sm->makePlane()<<std::endl;}};int main(){SuperMaker suMa;MakeCarCmd mcc(&suMa);Command *cmdCar = &mcc;MakePlaneCmd mpc(&suMa);Command *cmdPlane = &mpc;cmdCar->exeCmd();cmdPlane->exeCmd();return 0;}

Run:





Simple Example of command Design Mode

Command mode: command mode
Specifically, a request is encapsulated as an object so that you can parameterize the customer with different requests, queue the request or record the request log, and support unrecoverable operations.
For example, we can still draw a picture board. We will generate an object for each operation and store the operation in a data structure if it is not executed once. If necessary, we can cancel and restore the operation, this is a typical example of the command mode.

Public interface ICommand {
Bool Execute ();
Void Rollback ();
}

Public class CommandBase: ICommand
{
Public virtual bool Execute ()
{
Return true;
}
Public virtual void Rollback ()
{
}
}

Public class CommandA: CommandBase
{
Public override bool Execute (){
Console. Write ("CommandA Execute ");
Return base. Execute ();
}
}

Public class CommandB: CommandBase
{
Public override bool Execute (){
Console. Write ("CommandB Execute ");
Return base. Execute ();
}
}

Public class CommandC: CommandBase
{
Public override bool Execute (){
Console. Write ("CommandC Execute ");
Return base. Execute ();
}
}

Public class CommandList: CommandBase {
Public List <ICommand> _ commandList = new List <ICommand> ();
Public virtual List <ICommand> CreateCommand (){
Return new List <ICommand> ();
}
Public CommandList (){
CreateCommand ();
}
Public override bool Execute (){
Bool result = true;
Foreach (ICommand command in _ commandList ){
Result = command. Execute ();
If (! Result ){
Command. Rollback ();
Break;
}
}
Return result;
}
}

Public class CommandListA: CommandList {
Public virtual List <ICommand> CreateCommand (){
List <ICommand> tmpList = new List <ICommand> ();
TmpList. Add (new CommandA ());
TmpList. Add (new CommandB ());
}
}

Public class Comm... the remaining full text>
 
Java design mode (command mode)

Let's talk about the command mode first. For example, the command was created by the Emperor Yudi, the Monkey King of the United States, which is the sacred purpose. Then it was pointed out that the recipient of the Holy Spirit, Monkey King of the United States, was the only one who conveyed the command. This process is an application of the command mode.
The advantage is that the Jade Emperor will not directly deal with the Monkey King. He only needs to encapsulate the commands in the holy decree and send them to the white Venus to communicate other things.
Next, let's explain the observer model. In the travel to the West as an example, the Tang sanzang was trapped by a red child. Sun Wukong asked him to pray for the Bodhisattva, while the Bodhisattva listened and sent his pearl bottle to the sea, I saw a turtle carrying a bottle up .... The tortoise is the observer, and the object to be observed is the bodhisattva. Here we can see the difference between the command mode. The tortoise is waiting to observe the action of the Bodhisattva, And the Jade Emperor sends his own command. Sometimes it is messy. The observer is looking at how the turtles are implemented, and the command is about how the people who issue the command arrange it ....
Sorry!

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.