Prototype的主要缺陷是每一个Prototype的子类都必须实现Clone操作,这很烦。
{
return new ConcretePrototype(*this);
}
class PrototypeWrapper
{
public:
~PrototypeWrapper() {}
virtual Prototype* clone() = 0;
};
template <typename T>
class PrototypeWrapperImpl : public PrototypeWrapper
{
public:
PrototypeWrapperImpl()
{
_prototype = new T();
}
virtual Prototype* clone()
{
return new T(*_prototype);
}
T* _prototype;
};
PrototypeWrapper* prototype = new PrototypeWrapperImpl<ConcretePrototype>();
Prototype* p = prototype->clone();