原型模式定义:用原型实例通过拷贝方式创建新的对象,是一种创建型设计模式(至此,五种创建型设计模式就全部介绍完了(*^__^*) )
//原型模式示例代码
#include <iostream>
using namespace std;
class Prototype
{
public:
Prototype() = default;
~Prototype() = default;
virtual Prototype* clone() = 0;
};
class ConcretePrototype :public Prototype
{
public:
ConcretePrototype(int imember):_member(imember){}
~ConcretePrototype() = default;
ConcretePrototype(const ConcretePrototype& rhs)
{ //复制构造函数(若有指针成员变量,进行深拷贝!)
_member = rhs._member;
}
ConcretePrototype* clone()
{
cout << "Do Clone!!!" << endl;
return new ConcretePrototype(*this);
}
private:
int _member;
};
int main()
{
cout << "Prototype DP Test!" << endl;
ConcretePrototype* ConA = new ConcretePrototype(6);
ConcretePrototype* ConB = ConA->clone();
system("pause");
return 0;
}