1#include <iostream>2#include <string>3 4 using namespacestd;5 6 classCprototype7 {8 Public:9 Cprototype ()Ten { One A } -Cprototype (intiage): M_iage (iage) - { the - } - Virtualcprototype* Clone () =0; - + intM_iage; - }; + A classCconcreteprototypea: PublicCprototype at { - Public: -Cconcreteprototypea (intIage,Const string&strName): Cprototype (iage), M_strname (strName) - { - - } in - //virtual cprototype* Clone () to Virtualcconcreteprototypea*Clone () + { - return NewCconcreteprototypea (* This); the } * $ Private:Panax NotoginsengCconcreteprototypea (Constcconcreteprototypea&tmp) - { the This->m_iage =Tmp.m_iage; + This->m_strname =Tmp.m_strname; A } the + Public: - stringM_strname; $ }; $ - intMainintargcChar*argv[]) - { the //cprototype* Psttypea = new Cconcreteprototypea ("Lisi"); - //cprototype* psttypeb = Psttypea->clone ();Wuyi thecconcreteprototypea* Psttypea =NewCconcreteprototypea ( -,"Lisi"); - Wucconcreteprototypea* PSTTYPEB = psttypea->Clone (); -Psttypeb->m_iage = +; AboutPsttypeb->m_strname ="Zhangsan"; $ -cout<<"Psttypea.m_iage:"<< psttypea->m_iage<<"\tpsttypea.m_strname"<< psttypea->m_strname<<Endl; -cout<<"Psttypeb.m_iage:"<< psttypeb->m_iage<<"\tpsttypeb.m_strname"<< psttypeb->m_strname<<Endl; - A DeletePsttypea; + DeletePsttypeb; the - return 0; $ } the //////////////////////////////// the[Email protected] ~/learn_code/design_pattern/7_proto_type]$.ProtoType thePsttypea.m_iage: -Psttypea.m_strname Lisi thePsttypeb.m_iage: +Psttypeb.m_strname Zhangsan
Design pattern-Prototype mode (c + + implementation)