首页 > 后端开发 > C++ > 正文

C++怎么实现一个原型模式_C++通过复制现有实例来创建新对象的设计模式

尼克
发布: 2025-11-24 10:00:06
原创
980人浏览过
原型模式通过克隆现有对象创建新对象,避免复杂构造。定义抽象基类Prototype,声明纯虚clone方法;具体类如ConcretePrototype实现clone,返回自身副本;可选PrototypeManager管理原型实例,按名创建对象。

c++怎么实现一个原型模式_c++通过复制现有实例来创建新对象的设计模式

原型模式是一种创建型设计模式,它通过复制已有的实例来创建新的对象,而不是通过 new 关键字重新构造。在 C++ 中实现原型模式的关键是定义一个抽象接口,让具体类自己实现克隆方法,从而实现动态对象创建和避免复杂的构造过程。

定义原型接口

首先,定义一个抽象基类,声明一个纯虚的 clone 接口。所有可被复制的对象都继承这个接口。

class Prototype { public:virtual ~Prototype() = default; virtual Prototype* clone() const = 0; };

实现具体原型类

每个具体类需要重写 clone 方法,返回自身的一个副本。这里可以使用拷贝构造函数来确保深拷贝或浅拷贝的正确性。

class ConcretePrototype : public Prototype { private:int value; std::string data;

public: ConcretePrototype(int v, const std::string& d) : value(v), data(d) {}

<font color="#006400"><strong>// 实现克隆方法</strong></font>
Prototype* clone() <font color="#0000FF"><strong>const override</strong></font> {
    <font color="#0000FF"><strong>return</strong></font> <font color="#0000FF"><strong>new</strong></font> ConcretePrototype(*<font color="#0000FF"><strong>this</strong></font>);
}

<font color="#0000FF"><strong>void</strong></font> show() <font color="#0000FF"><strong>const</strong></font> {
    std::cout << "Value: " << value << ", Data: " << data << "\n";
}
登录后复制

};

立即学习C++免费学习笔记(深入)”;

使用原型管理器(可选)

为了更方便地管理和创建原型对象,可以引入一个原型注册表,按名称存储和获取原型实例。

抖云猫AI论文助手
抖云猫AI论文助手

一款AI论文写作工具,最快 2 分钟,生成 3.5 万字论文。论文可插入表格、代码、公式、图表,依托自研学术抖云猫大模型,生成论文具备严谨的学术专业性。

抖云猫AI论文助手 146
查看详情 抖云猫AI论文助手
class PrototypeManager { private: std::unordered_map<:string prototype> prototypes;

public:void addPrototype(const std::string& name, Prototype* proto) { if (prototypes.find(name) == prototypes.end()) { prototypes[name] = proto; } }

Prototype* create(<font color="#0000FF"><strong>const</strong></font> std::string& name) {
    <font color="#0000FF"><strong>auto</strong></font> it = prototypes.find(name);
    <font color="#0000FF"><strong>if</strong></font> (it != prototypes.end()) {
        <font color="#0000FF"><strong>return</strong></font> it->second->clone();
    }
    <font color="#0000FF"><strong>return</strong></font> nullptr;
}

~PrototypeManager() {
    <font color="#0000FF"><strong>for</strong></font> (<font color="#0000FF"><strong>auto</strong></font>& pair : prototypes) {
        <font color="#0000FF"><strong>delete</strong></font> pair.second;
    }
}
登录后复制

};

立即学习C++免费学习笔记(深入)”;

示例用法

使用原型模式创建对象:

int main() { PrototypeManager manager; manager.addPrototype("example", new ConcretePrototype(42, "sample"));
Prototype* obj1 = manager.create("example");
Prototype* obj2 = manager.create("example");

<font color="#0000FF"><strong>static_cast</strong></font><ConcretePrototype*>(obj1)->show();  <font color="#006400"><strong>// 输出相同内容</strong></font>
<font color="#0000FF"><strong>static_cast</strong></font><ConcretePrototype*>(obj2)->show();

<font color="#0000FF"><strong>delete</strong></font> obj1;
<font color="#0000FF"><strong>delete</strong></font> obj2;
<font color="#0000FF"><strong>return</strong></font> 0;
登录后复制

}

基本上就这些。关键在于 clone 方法的实现要保证对象状态完整复制,注意深拷贝问题。如果成员包含指针或资源,需手动实现拷贝构造函数以避免浅拷贝带来的问题。

以上就是C++怎么实现一个原型模式_C++通过复制现有实例来创建新对象的设计模式的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号