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