当前位置:   article > 正文

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

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

原型模式

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

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

  1. class ConcretePrototype1 : public Prototype{
  2. public:
  3. ConcretePrototype1(string prototype_name, float concrete_prototype_field):Prototype(prototype_name), concrete_prototype_field1(concrete_prototype_field){}
  4. Prototype* Clone() { return new ConcretePrototype1(*this); }
  5. void printFeild(){ std::cout << "FEILD:\t" << concrete_prototype_field1 << std::endl; }
  6. private:
  7. float concrete_prototype_field1;
  8. };

参考

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/41945
推荐阅读
相关标签
  

闽ICP备14008679号