个人理解
类中提供了复制类的函数,能够在你需要他的副本的情况下快速的创建出一个相同的类来。
原型模式
#include<iostream>
using namespace std;
class Clone
{
public:
virtual Clone* clone() = 0;
virtual void eat() = 0;
};
class person : public Clone
{
public:
person(string name)
{
this->m_name = name;
}
person(const person& other)
{
this->m_name = other.m_name;
}
Clone* clone()
{
return new person(*this);
}
void eat()
{
cout << m_name << " is a foodie" << endl;
}
private:
string m_name;
};
int main()
{
Clone* woman = new person("xiaozhang");
Clone* woman_1 = woman->clone();
woman->eat();
woman_1->eat();
if (woman)
{
delete woman;
woman = NULL;
}
if (woman_1)
{
delete woman_1;
woman_1 = NULL;
}
return 0;
}
|