Design Mode C ++ study note 12 (Command mode)

Source: Internet
Author: User

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. It should be a relatively simple mode.

 

12. 1. Explanation

Main (), customer

CInvoker, command receiver, such as Project Manager

IGroup, executor Interface

CRequirementGroup, one of the actual executors

CPageGroup, the actual executor 2

CCodePage, Operator 3

ICommand, command INTERFACE

CAddRequirementCommand, Execute function, will call multiple commands of CRequirementGroup. To combine and execute user-Issued commands.

CDeletePageCommand, same as above

Other commands.

Note: The customer only needs to know how to issue commands to Invoker (multiple commands) instead of directly communicating the commands to specific executors. Of course, the customer needs to know what commands are available.

Note: The customer only sends commands and does not need to know who will execute them or how to execute them. This reflects the high cohesion feature. After issuing a command, the user is allowed to recall it. Therefore, you can add the command "Undo", which is a state change.

Check the Code:

// Invoker. h

# Pragma once
# Include "ICommand. h"
Class CInvoker
{
Public:
CInvoker (void );
~ CInvoker (void );
Void SetCommand (ICommand * pcommand );
Void Action ();
Private:
ICommand * m_pCommand;
};

// Invoker. cpp

# Include "StdAfx. h"
# Include "Invoker. h"
CInvoker: CInvoker (void)
{
}
CInvoker ::~ CInvoker (void)
{
}
Void CInvoker: SetCommand (ICommand * pcommand)
{
This-> m_pCommand = pcommand;
}
Void CInvoker: Action ()
{
This-> m_pCommand-> Execute ();
}

// IGroup. h

# Pragma once
Class IGroup
{
Public:
IGroup (void)
{
}
Virtual ~ IGroup (void)
{
}
Virtual void Find () = 0;
Virtual void Add () = 0;
Virtual void Delete () = 0;
Virtual void Change () = 0;
Virtual void Plan () = 0;
};

// RequirementGroup. h

# Pragma once
# Include "igroup. h"
Class CRequirementGroup:
Public IGroup
{
Public:
CRequirementGroup (void );
~ CRequirementGroup (void );
Void Find ();
Void Add ();
Void Delete ();
Void Change ();
Void Plan ();
};

// RequirementGroup. cpp

# Include "StdAfx. h"
# Include "RequirementGroup. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CRequirementGroup: CRequirementGroup (void)
{
}
CRequirementGroup ::~ CRequirementGroup (void)
{
}
Void CRequirementGroup: Find ()
{
Cout <"find the requirement group..." <endl;
}
Void CRequirementGroup: Add ()
{
Cout <"the customer requested to add a requirement..." <endl;
}
Void CRequirementGroup: Delete ()
{
Cout <"requires deletion of a requirement..." <endl;
}
Void CRequirementGroup: Change ()
{
Cout <"customer requests to modify a requirement..." <endl;
}
Void CRequirementGroup: Plan ()
{
Cout <"customer requirement change plan..." <endl;
}
// PageGroup. h

# Pragma once
# Include "igroup. h"
Class CPageGroup:
Public IGroup
{
Public:
CPageGroup (void );
~ CPageGroup (void );
Void Find ();
Void Add ();
Void Delete ();
Void Change ();
Void Plan ();
};

// PageGroup. cpp

# Include "StdAfx. h"
# Include "PageGroup. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CPageGroup: CPageGroup (void)
{
}
CPageGroup ::~ CPageGroup (void)
{
}
Void CPageGroup: Find ()
{
Cout <"find the art group..." <endl;
}
Void CPageGroup: Add ()
{
Cout <"the customer requested to add a page..." <endl;
}
Void CPageGroup: Delete ()
{
Cout <"the customer requested to delete a page..." <endl;
}
Void CPageGroup: Change ()
{
Cout <"the customer requested to modify a page..." <endl;
}
Void CPageGroup: Plan ()
{
Cout <"customer requirements page change plan..." <endl;
}
// CodeGroup. h

# Pragma once
# Include "igroup. h"
Class CCodeGroup:
Public IGroup
{
Public:
CCodeGroup (void );
~ CCodeGroup (void );
Void Find ();
Void Add ();
Void Delete ();
Void Change ();
Void Plan ();
};

// CodeGroup. cpp

# Include "StdAfx. h"
# Include "CodeGroup. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CCodeGroup: CCodeGroup (void)
{
}
CCodeGroup ::~ CCodeGroup (void)
{
}
Void CCodeGroup: Find ()
{
Cout <"find the code group..." <endl;
}
Void CCodeGroup: Add ()
{
Cout <"the customer requested to add a feature..." <endl;
}
Void CCodeGroup: Delete ()
{
Cout <"the customer requested to delete a feature..." <endl;
}
Void CCodeGroup: Change ()
{
Cout <"the customer requested to modify a feature..." <endl;
}
Void CCodeGroup: Plan ()
{
Cout <"code change plan requested by the customer..." <endl;
}
// ICommand. h

# Pragma once
# Include "RequirementGroup. h"
# Include "PageGroup. h"
# Include "CodeGroup. h"
Class ICommand
{
Public:
ICommand (void)
{
M_prg = new CRequirementGroup ();
M_ppg = new CPageGroup ();
M_pcg = new CCodeGroup ();
}
Virtual ~ ICommand (void)
{
Delete m_prg;
Delete m_ppg;
Delete m_pcg;
}
Virtual void Execute () = 0;
Protected:
CRequirementGroup * m_prg;
CPageGroup * m_ppg;
CCodeGroup * m_pcg;
};
// AddRequirementCommand. h

# Pragma once
# Include "icommand. h"
Class CAddRequirementCommand:
Public ICommand
{
Public:
CAddRequirementCommand (void );
~ CAddRequirementCommand (void );
Void Execute ();
};

// AddRequirementCommand. cpp

# Include "StdAfx. h"
# Include "AddRequirementCommand. h"
CAddRequirementCommand: CAddRequirementCommand (void)
{
}
CAddRequirementCommand ::~ CAddRequirementCommand (void)
{
}
Void CAddRequirementCommand: Execute ()
{
// Execute the command to add another requirement
This-> ICommand: m_prg-> Find ();

// Add a requirement
This-> ICommand: m_prg-> Add ();

// Give a plan
This-> ICommand: m_prg-> Plan ();
}
// DeletePageCommand. h

# Pragma once
# Include "icommand. h"
Class CDeletePageCommand:
Public ICommand
{
Public:
CDeletePageCommand (void );
~ CDeletePageCommand (void );
Void Execute ();
};
// DeletePageCommand. cpp

# Include "StdAfx. h"
# Include "DeletePageCommand. h"
CDeletePageCommand: CDeletePageCommand (void)
{
}
CDeletePageCommand ::~ CDeletePageCommand (void)
{
}
Void CDeletePageCommand: Execute ()
{
// Execute the command to add another requirement
This-> ICommand: m_ppg-> Find ();

// Add a requirement
This-> ICommand: m_ppg-> Delete ();

// Give a plan
This-> ICommand: m_ppg-> Plan ();
}
//Command. cpp

# Include "stdafx. h"
# Include "IGroup. h"
# Include "CodeGroup. h"
# Include "PageGroup. h"
# Include "RequirementGroup. h"
# Include "Invoker. h"
# Include "AddRequirementCommand. h"
# Include "DeletePageCommand. h"
# Include <iostream>
Using std: cout;
Using std: endl;

Void DoIt ()
{
Cout <"---------- the customer wants to add a requirement ----------" <endl;
IGroup * rg = new CRequirementGroup ();
Rg-> Find ();
Rg-> Add ();
Rg-> Plan ();
Delete rg;
Cout <endl;

Cout <"---------- the customer wants to modify another page ----------" <endl;
IGroup * pg = new CPageGroup ();
Pg-> Find ();
Pg-> Add ();
Pg-> Plan ();
Delete pg;
Cout <endl;

Cout <"---------- the customer wants to delete another feature ----------" <endl;
IGroup * cg = new CCodeGroup ();
Cg-> Find ();
Cg-> Add ();
Cg-> Plan ();
Delete cg;
Cout <endl;
}

Void DoNew ()
{
Cout <"---------- the customer is tired and wants to find only one person and tell him what to do ----------" <endl;
Cout <"---------- the customer requested to add a requirement ----------" <endl;
CInvoker gary;
ICommand * pcommand = new CAddRequirementCommand ();
Gary. SetCommand (pcommand );
Gary. Action ();
Delete pcommand;
Cout <endl;

// The customer only needs to find CInvoker to modify the modification.
Cout <"---------- the customer requested to delete a page ----------" <endl;
CInvoker ricky;
ICommand * pcommand2 = new CDeletePageCommand ();
Ricky. SetCommand (pcommand2 );
Ricky. Action ();
Delete pcommand2;
Cout <endl;
}

Int _ tmain (int argc, _ TCHAR * argv [])
{
// The customer's original running process
DoIt ();

// The customer is in trouble. He needs to find different groups for every change and talk about different things.
// The customer only wants to find a person and tell him what to do. He doesn't want to care about which groups to do or how to do it.
DoNew ();

_ CrtSetDbgFlag (_ CRTDBG_LEAK_CHECK_DF | _ CRTDBG_ALLOC_MEM_DF );
_ CrtDumpMemoryLeaks ();
Return 0;
}

I remember adding a small Timesheet function to the system. The command mode was used here. At that time, it was just a trainer, because the command mode was only applicable to scenarios where not many changes were made, because a command is defined as an ICommand implementation class, it may be difficult to control the increase in the number of ICommand Derived classes. Is the class diagram used when the Code implements the command mode.

They need to stick to learning, but they are also painful. Everyone wants to go off work every day, take a break at home, and watch TV or something. But I want to stick to it and encourage myself constantly. Complete your learning plan. Come on!

 


Concept: 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.

Main (), customer

CInvoker, command receiver, such as Project Manager

IGroup, executor Interface

CRequirementGroup, one of the actual executors

CPageGroup, the actual executor 2

CCodePage, Operator 3

ICommand, command INTERFACE

CAddRequirementCommand, Execute function, will call multiple commands of CRequirementGroup. To combine and execute user-Issued commands.

CDeletePageCommand, same as above

 

Note: The customer only needs to know how to issue commands to Invoker (multiple commands) instead of directly communicating the commands to specific executors. Of course, the customer needs to know what commands are available.

 

Note: The customer only sends commands and does not need to know who will execute them or how to execute them. This reflects the high cohesion feature. After issuing a command, the user is allowed to recall it. Therefore, you can add the command "Undo", which is a state change.

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.