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

相关推荐
xlq223225 小时前
22.多态(上)
开发语言·c++·算法
D_evil__5 小时前
[C++高频精进] 并发编程:线程基础
c++
Mr_WangAndy6 小时前
C++17 新特性_第二章 C++17 语言特性_std::any和string_view
c++·string_view·c++40周年·c++17新特性·c++新特性any
水天需0107 小时前
C++ 三种指针转换深度解析
c++
言言的底层世界8 小时前
c++中STL容器及算法等
开发语言·c++·经验分享·笔记
Mr_WangAndy8 小时前
C++17 新特性_第一章 C++17 语言特性___has_include,u8字符字面量
c++·c++40周年·c++17新特性·__has_include·u8字面量
liu****8 小时前
八.函数递归
c语言·开发语言·数据结构·c++·算法
Vanranrr9 小时前
C++临时对象与悬空指针:一个导致资源加载失败的隐藏陷阱
服务器·c++·算法
__万波__9 小时前
二十三种设计模式(二)--工厂方法模式
java·设计模式·工厂方法模式
BestOrNothing_20159 小时前
【C++基础】Day 5:struct 与 class
c++·c·class类·struct结构体·typename模板·private与public