Prototype Pattern Sample Code (c + +)

Source: Internet
Author: User

1 // --------------------------------------------------------------------------------------2 /** Abstract.h **/3 4 classAbstract {5 Private:6typedef map<type_t, Abstract*>RegMap;7     Staticregmap&GetRegistry () {8         StaticRegMap Registry;//initialized at the first time, the function is called9         returnregistry;Ten     } One  Public: AAbstract (Consttype_t&type) { -         if(Lookup (type)! =NULL) -Getregistry.insert (Type, This); the     } -  -     Virtual~Abstract () {} -  +     Virtualabstract* Clone () =0;  -  +     Staticabstract* Create (Consttype_t&type) {  Aabstract* stub =lookup (type); at         returnStub! = null:stub->Clone (): NULL; -     } -  -     Virtual voidDestroy () {Delete  This; } -  -     Static ConstAbstract* Lookup (Consttype_t&type) { in         if(GetRegistry (). Find (type)! =getregistry (). End ()) -             returngetregistry () [type]; to         returnNULL; +     } - }; the  * // -------------------------------------------------------------------------------------- $ /** ConcreteA.h **/Panax Notoginseng  - classConcretea: PublicAbstract { the     //.. data Members +concretea* Clone () {return NewConcretea (* This); } A  Public: theConcretea (): Abstract (conc_type_a) {}//conc_type_a:constant +     //... other members - }; $  $ // -------------------------------------------------------------------------------------- - /** ConcreteA.cpp **/ -  the     StaticConcretea __concrete_a_stub/*(...)*/;//static initialized - Wuyi // -------------------------------------------------------------------------------------- the /** ConcreteB.h **/ -  Wu classCONCRETEB: PublicAbstract { -     //.. data Members Aboutconcreteb* Clone () {return NewCONCRETEB (* This); } $  Public: -Concreteb (): Abstract (conc_type_b) {}//conc_type_b:constant -     //... other members - }; A  + // -------------------------------------------------------------------------------------- the /** ConcreteB.cpp **/ -  $     StaticConcreteb __concrete_b_stub/*(...)*/;  the  the // -------------------------------------------------------------------------------------- the /** Client **/ the  -abstract* p_abstract = abstract::create (conc_type_a/*or Conc_type_b*/); in //... some code theP_abstract->destroy (); the  About // --------------------------------------------------------------------------------------

Prototype Pattern Sample Code (c + +)

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.