model
implement
prototype
class Prototype
{
protected:
Prototype();
public:
virtual ~Prototype();
public:
virtual Prototype* Clone() const = 0;
};
Prototype::Prototype()
{
}
Prototype::~Prototype()
{
}
concrete prototype
class ConcretePrototype : public Prototype
{
public:
ConcretePrototype();
ConcretePrototype(const ConcretePrototype& other);
~ConcretePrototype();
public:
Prototype* Clone() const;
};
ConcretePrototype::ConcretePrototype()
{
}
ConcretePrototype::ConcretePrototype(const ConcretePrototype& other)
{
cout << "ConcretePrototype copy..." << endl;
}
ConcretePrototype::~ConcretePrototype()
{
}
Prototype* ConcretePrototype::Clone() const
{
return new ConcretePrototype(*this);
}
应用
void prototype_test()
{
Prototype* prototype = new ConcretePrototype();
Prototype* prototypeCp = prototype->Clone();
}
总结
- 抽象:concrete prototype的公共基类prototype进行了抽象
- 扩展:concrete prototype可以进行扩展
- 开发者不需要知道concrete prototype,开发者只与公共基类prototype交互