c++23种设计模式之原型模式

来源:互联网 发布:html5 css3游戏源码 编辑:程序博客网 时间:2024/04/30 05:40
#include <stdio.h>////c++ 23种设计模式之原型模式//适用情况:1、请求一个原型克隆自身namespace DesignPattern_Prototype{class Prototype{public:virtual Prototype * Clone()=0;virtual void showInfo()=0;};class ConcretePrototype1:public Prototype{public:virtual Prototype *Clone(){ConcretePrototype1 *p=new ConcretePrototype1;*p=*this;//复制对象return p;}void showInfo(){printf("我是 ConcretePrototype1 \n");}};class ConcretePrototype2:public Prototype{public:virtual Prototype* Clone(){ConcretePrototype2 *p=new ConcretePrototype2;*p=*this;return p;}void showInfo(){printf("我是 ConcretePrototype2 \n");}};}void main(){using namespace DesignPattern_Prototype; ConcretePrototype1 *obj1=new ConcretePrototype1; ConcretePrototype2 *obj2=new ConcretePrototype2(); Prototype *newobj1=obj1->Clone(); Prototype *newobj2=obj2->Clone(); newobj1->showInfo(); newobj2->showInfo();}

原创粉丝点击