(十三)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博客

相关推荐
SimpleLearingAI16 分钟前
C++虚函数详解
开发语言·c++
小柯博客38 分钟前
STM32MP2安全启动技术深度解析
c语言·c++·stm32·嵌入式硬件·安全·开源·github
cpp_25011 小时前
P1832 A+B Problem(再升级)
数据结构·c++·算法·动态规划·题解·洛谷·背包dp
geovindu1 小时前
go: Proxy Pattern
开发语言·后端·设计模式·golang·代理模式
结衣结衣.1 小时前
手把手教你实现文档搜索引擎
linux·c++·搜索引擎·开源·c++11
t***5441 小时前
如何在 Dev-C++ 中切换编译器
java·开发语言·c++
大肥羊学校懒羊羊2 小时前
完数与盈数的计算题解
数据结构·c++·算法
澈2072 小时前
构造函数与析构函数完全指南
开发语言·c++
W23035765732 小时前
C++ 高并发线程池实战(二):动态缓存线程池 + 调用者运行拒绝策略完整版实现
开发语言·c++·缓存
t***5442 小时前
如何在 Dev-C++ 中使用 Clang 编译器
开发语言·c++