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

原型模式

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

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

cpp 复制代码
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博客

相关推荐
房开民1 小时前
c++总结
java·开发语言·c++
好大哥呀1 小时前
C++ 多态
java·jvm·c++
墨韵流芳2 小时前
CCF-CSP第41次认证第三题——进程通信
c++·人工智能·算法·机器学习·csp·ccf
hz_zhangrl3 小时前
CCF-GESP 等级考试 2026年3月认证C++五级真题解析
c++·青少年编程·程序设计·gesp·c++五级·gesp2026年3月·gesp c++五级
Σίσυφος19003 小时前
C++ 多肽经典面试题
开发语言·c++·面试
crescent_悦4 小时前
C++:The Largest Generation
java·开发语言·c++
paeamecium5 小时前
【PAT甲级真题】- Student List for Course (25)
数据结构·c++·算法·list·pat考试
大数据新鸟5 小时前
设计模式详解——观察者模式
观察者模式·设计模式
c++逐梦人7 小时前
C++11——— 包装器
开发语言·c++
武藤一雄8 小时前
C# 设计模式大全(第一弹|7种)
microsoft·设计模式·微软·c#·.net·.netcore