C ++ design mode-command

Source: Internet
Author: User

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;
}

 

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.