Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support undo operations.
Resolution;
The command is encapsulated in the Command class of a class, and the receiving object is also encapsulated in the javaser class in a class. The invoker class that calls this command is similar to the principle of registering a callback function.
# Include "stdafx. H"
# Include <iostream>
Using namespace STD;
// Command mode encapsulation removes the coupling between business logic and actual abstraction, and supports transaction operations
Class command
{
Public:
Virtual ~ Command (){}
Virtual void execute () = 0;
};
Class Cycler
{
Public:
Void action () {cout <"fact active/N ";}
};
// Generate command classes for different purposes based on Initialization
Class concreatecommand: Public command
{
Public:
Concreatecommand (Explorer * preceiver): m_preceiver (preceiver ){}
Virtual ~ Concreatecommand ();
Virtual void execute ();
PRIVATE:
Extends er * m_preceiver;
};
Class invoker
{
Public:
Invoker (command * pcommand): m_pcommand (pcommand ){}
~ Invoker ();
// Implement the DO and undo functions, add a series of commands, or cancel some commands to form a thing
Void invoke (); // callback command function
PRIVATE:
Command * m_pcommand;
};
//////////////////////////////////////// ///////////////////////////////////
Invoker ::~ Invoker ()
{
Delete m_pcommand;
M_pcommand = NULL;
}
Void concreatecommand: Execute ()
{
If (null! = M_preceiver)
M_preceiver-> action ();
Cout <"execute by concreatecommand/N ";
}
Void invoker: invoke ()
{
If (null! = M_pcommand)
M_pcommand-> execute ();
}
Concreatecommand ::~ Concreatecommand ()
{
Delete m_preceiver;
M_preceiver = NULL;
}
//////////////////////////////////////// ///////////////////////////////////
Int _ tmain (INT argc, char * argv [])
{
Extends er * preceiver = new receiver;
Command * pcommand = new concreatecommand (preceiver );
Invoker * pinvoker = new invoker (pcommand );
/*
Pinvoker-> do (pcommand );
Pinvoker-> do (pcommand1 );
Pinvoker-> undo ();
Pinvoker-> invoke ();
*/
Pinvoker-> invoke ();
Delete pinvoker;
System ("pause ");
Return 0;
}