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

相关推荐
樱木Plus16 小时前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
willow17 小时前
Axios由浅入深
设计模式·axios
blasit2 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
七月丶3 天前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞3 天前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼3 天前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟3 天前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder3 天前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
肆忆_4 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++