原型模式(C++)

#include <iostream>
using namespace std;

class prototype
{
public:
    prototype(){cout<<"build prototype"<<endl;}
    virtual ~prototype(){cout<<"unbuild prototype"<<endl;}
    virtual prototype *clone()=0;
};

class subtype : public prototype
{
public:
    subtype(){cout<<"build subtype"<<endl;}
    subtype(prototype *pt){cout<<"build a subtype"<<endl;}
    virtual ~subtype(){cout<<"unbuild subtype"<<endl;}
    prototype *clone(){cout<<"clone"<<endl;return new subtype(*this);}
};

int main()
{
    prototype *p1=new subtype;
    prototype *p2=p1->clone();

    delete p1;
    delete p2;
    system("pause");
    return 0;
}
posted @ 2012-06-24 21:03  Dsp Tian  阅读(441)  评论(0编辑  收藏  举报