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

相关推荐
QT 小鲜肉14 分钟前
【Git、GitHub、Gitee】按功能分类汇总Git常用命令详解(超详细)
c语言·网络·c++·git·qt·gitee·github
派大星爱吃猫16 分钟前
C++中的inline函数(内联函数)
c++·inline·内联函数
清风wxy26 分钟前
Duilib_CEF桌面软件实战之Duilib编译与第一个界面程序
c++·笔记·ui·mfc
郝学胜-神的一滴27 分钟前
Linux下,获取子进程退出值和异常终止信号
linux·服务器·开发语言·c++·程序人生
notfindjob1 小时前
MFC动态加载图片
c++·mfc
CodeByV1 小时前
【C++】继承
开发语言·c++
在未来等你1 小时前
AI Agent设计模式 Day 2:Plan-and-Execute模式:先规划后执行的智能策略
设计模式·llm·react·ai agent·plan-and-execute
乱舞八重击(junluoyu)2 小时前
1.PagedAtteion算法
c++
2301_803554523 小时前
C++ 锁类型大全详解
开发语言·c++
曼巴UE53 小时前
UE5 C++ Slate 画曲线
开发语言·c++·ue5