原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
//Prototype.h
#ifndef _PROTOTYPE_H
#define _PROTOTYPE_H
class Prototype
{
public:
virtual ~Prototype();
virtual Prototype* Clone() const = 0;
Prototype();
};
class ConcretePrototype:public Prototype
{
public:
ConcretePrototype();
ConcretePrototype(const ConcretePrototype& cp);
~ConcretePrototype();
Prototype* Clone() const;
};
#endif
//Prototype.cpp
#include "Prototype.h"
#include <iostream>
using namespace std;
Prototype::Prototype()
{
}
Prototype::~Prototype()
{
}
Prototype* Prototype::Clone() const
{
return 0;
}
ConcretePrototype::ConcretePrototype()
{
}
ConcretePrototype::~ConcretePrototype()
{
}
ConcretePrototype::ConcretePrototype(const ConcretePrototype& cp)
{
cout<<"ConcretePrototype copy..."<<endl;
}
Prototype* ConcretePrototype::Clone() const
{
return new ConcretePrototype(*this);
}
//main.cpp
#include "Prototype.h"
#include <iostream>
int main()
{
Prototype* p = new ConcretePrototype();
Prototype* p1= p->Clone();
return 0;
}