Command mode in Design Mode

Source: Internet
Author: User

Command mode in Design Mode

Four roles:

Receiver: The command is finally passed here to execute the command, but multiple commands can be executed.

Command: Has a pointer to the worker er. You need to execute a Command

Invoker: Record and trigger command

Client: Application

/* Command pattern */

/* Author hugewave */
# Include
# Include
# Include
Using namespace std;
Class CReceiver
{
Public:
Void funcone ()
{
Cout < };

Void functwo ()
{
Cout < };
};


Class CCommand
{
Protected:
CReceiver * p_rec;
Public:
CCommand (CReceiver * rec): p_rec (rec)
{
};


Public:
Virtual void Do () = 0;
};


Class CConcreteCommandOne: public CCommand
{
Public:
CConcreteCommandOne (CReceiver * rec): CCommand (rec)
{
};
Public:
Void Do ()
{
P_rec-> funcone ();
};


};


Class CConcreteCommandTwo: public CCommand
{
Public:
CConcreteCommandTwo (CReceiver * rec): CCommand (rec)
{
};
Public:
Void Do ()
{
P_rec-> functwo ();
};


};


Class CInvoker
{
Private:
Vector Cmdlist;
Public:
Void AddCmd (CCommand * cmd)
{
Cmdlist. push_back (cmd );
}


Void ExcuteCmd ()
{
Vector : Iterator it;
For (it = cmdlist. begin (); it! = Cmdlist. end (); ++ it)
{
(* It)-> Do ();
}
}
};


Int main ()
{
// Client
CReceiver * p_rec = new CReceiver ();
CInvoker * p_invoker = new CInvoker ();
CCommand * cmd1 = new CConcreteCommandOne (p_rec );
CCommand * cmd2 = new CConcreteCommandTwo (p_rec );


P_invoker-> AddCmd (cmd1 );
P_invoker-> AddCmd (cmd2 );


P_invoker-> ExcuteCmd ();
Return 1;
}

 

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.