ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

(十三)Head first design patterns原型模式(c++)

2026/9/12 18:59:53 拓冰建站 浏览量
(十三)Head first design patterns原型模式(c++)

原型模式

原型模式就是就是对对象的克隆。有一些私有变量外界难以访问,而原型模式可以做到对原型一比一的复刻。

其关键代码为下面的clone方法。此方法将本对象进行复制传递出去。

class ConcretePrototype1 : public Prototype{
public:ConcretePrototype1(string prototype_name, float concrete_prototype_field):Prototype(prototype_name), concrete_prototype_field1(concrete_prototype_field){}Prototype* Clone() { return new ConcretePrototype1(*this); }void printFeild(){ std::cout << "FEILD:\t" << concrete_prototype_field1 << std::endl; }
private:float concrete_prototype_field1;
};

参考

C++设计模式(10)——原型模式_c++原型模式-CSDN博客