Design mode-Command mode (c + + implementation)

Source: Internet
Author: User

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)

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.