1[Email protected] ~/learn_code/design_pattern/19_order]$ Cat Order.cpp2#include <iostream>3#include <string>4#include <vector>5#include <algorithm>6#include <iterator>7 8 using namespacestd;9 Ten classReceiver One { A Public: - voidBakemutton () - { thecout<<"roasted Lamb"<<Endl; - } - - voidBakechicken () + { -cout<<"Grilled chicken Wings"<<Endl; + } A }; at - classCommand - { - Public: -Command (receiver*pstreceiver): M_pstreceiver (pstreceiver) - { in - } to Virtual voidExcute () =0; + - protected: thereceiver*M_pstreceiver; * }; $ Panax Notoginseng classConcretecommanda: PublicCommand - { the Public: +Concretecommanda (receiver*pstreceiver): Command (pstreceiver) A { the + } - Virtual voidExcute () $ { $cout<<"Concretecommanda excuting ..."<<Endl; -M_pstreceiver->Bakemutton (); - } the - };Wuyi the classCONCRETECOMMANDB: PublicCommand - { Wu Public: -CONCRETECOMMANDB (receiver*pstreceiver): Command (pstreceiver) About { $ - } - Virtual voidExcute () - { Acout<<"concretecommandb excuting ..."<<Endl; +M_pstreceiver->Bakechicken (); the } - }; $ the classInvoke the { the Public: the voidADD (command*Pstcommand) - { in M_vecpstcommand.push_back (pstcommand); the } the voidRemove (command*Pstcommand) About { the m_vecpstcommand.erase (Find (M_vecpstcommand.begin (), M_vecpstcommand.end (), Pstcommand)); the } the voidRemoveAll () + { - m_vecpstcommand.clear (); the }Bayi voidNotify () the { the for(typeof(M_vecpstcommand.begin ()) it = M_vecpstcommand.begin (); It! = M_vecpstcommand.end (); ++it) - { -(*it)Excute (); the } the } the the Private: -Vector<command*>M_vecpstcommand; the }; the the intMainintargcChar*argv[])94 { thereceiver* Pstreceiver =NewReceiver (); thecommand* Pstconcretecommanda =NewConcretecommanda (pstreceiver); thecommand* pstconcretecommandb =Newconcretecommandb (pstreceiver);98invoke* Pstinvoke =NewInvoke (); About -Pstinvoke->Add (Pstconcretecommanda);101Pstinvoke->Add (Pstconcretecommanda);102Pstinvoke->Add (PSTCONCRETECOMMANDB);103Pstinvoke->Notify ();104cout<<"------------------"<< endl<<Endl; the 106Pstinvoke->remove (Pstconcretecommanda);//Undo Action107Pstinvoke->Remove (PSTCONCRETECOMMANDB);108Pstinvoke->Notify ();109cout<<"------------------"<< endl<<Endl; the 111 return 0; the }113 //////////////////////////////////////// the[Email protected] ~/learn_code/design_pattern/19_order]$.Order the Concretecommanda excuting ... the roasted Lamb117 Concretecommanda excuting ...118 roasted Lamb119 concretecommandb excuting ... - Grilled chicken Wings121------------------122 123 Concretecommanda excuting ...124 roasted Lamb the------------------
Design mode-Command mode (c + + implementation)