Prototype模式去掉Clone方法

意图:

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

结构图:

结构图

Prototype的主要缺陷是每一个Prototype的子类都必须实现Clone操作,这很烦。
一般都这样实现:

1
2
3
4
5
 
Prototype* ConcretePrototype::Clone()
{
return new ConcretePrototype(*this);
}

现在想去掉这个重复的操作

结构图如下:

结构图

实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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);
}
private:
T* _prototype;
};

使用:

1
2
3

PrototypeWrapper* prototype = new PrototypeWrapperImpl<ConcretePrototype>();
Prototype* p = prototype->clone();
David++