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 + +)