当前位置: 首页 > news >正文

C++实现设计模式---原型模式 (Prototype)

原型模式 (Prototype)

原型模式 是一种创建型设计模式,它通过复制现有对象来创建新对象,而不是通过实例化。


意图

  • 使用原型实例指定要创建的对象类型,并通过复制该原型来生成新对象。
  • 提供一种高效创建对象的方式,尤其是当对象的创建成本较高时。

使用场景

  1. 对象创建成本高
    • 如果直接实例化对象开销较大(如对象初始化需要大量资源),可以通过克隆已存在的对象快速生成新实例。
  2. 复杂对象需要重复创建
    • 当对象的结构较复杂,需要创建多个相似对象时。
  3. 避免直接依赖具体类
    • 原型模式通过复制实例而不是直接依赖构造函数,可以减少对具体类的依赖。

参与者角色

  1. 原型接口 (Prototype)
    • 定义一个克隆方法,用于复制对象。
  2. 具体原型 (Concrete Prototype)
    • 实现原型接口,定义克隆方法,返回当前实例的复制品。
  3. 客户端 (Client)
    • 使用原型接口创建新对象,而不直接依赖具体类。

示例代码

以下示例展示了如何使用原型模式创建几种不同类型的图形对象。

#include <iostream>
#include <memory>
#include <string>// 原型接口
class Shape {
public:virtual ~Shape() {}virtual std::unique_ptr<Shape> clone() const = 0;virtual void draw() const = 0;
};// 具体原型:圆形
class Circle : public Shape {
private:int radius;public:Circle(int r) : radius(r) {}// 实现克隆方法std::unique_ptr<Shape> clone() const override {return std::make_unique<Circle>(*this);}void draw() const override {std::cout << "Drawing a Circle with radius: " << radius << std::endl;}
};// 具体原型:矩形
class Rectangle : public Shape {
private:int width, height;public:Rectangle(int w, int h) : width(w), height(h) {}// 实现克隆方法std::unique_ptr<Shape> clone() const override {return std::make_unique<Rectangle>(*this);}void draw() const override {std::cout << "Drawing a Rectangle with width: " << width<< " and height: " << height << std::endl;}
};// 客户端代码
int main() {// 创建具体原型std::unique_ptr<Shape> circlePrototype = std::make_unique<Circle>(10);std::unique_ptr<Shape> rectanglePrototype = std::make_unique<Rectangle>(20, 30);// 克隆原型auto circle1 = circlePrototype->clone();auto circle2 = circlePrototype->clone();auto rectangle1 = rectanglePrototype->clone();// 使用克隆对象circle1->draw(); // 输出:Drawing a Circle with radius: 10circle2->draw(); // 输出:Drawing a Circle with radius: 10rectangle1->draw(); // 输出:Drawing a Rectangle with width: 20 and height: 30return 0;
}

代码解析

1. 原型接口

  • 定义了 clone() 方法,用于创建当前对象的副本:
virtual std::unique_ptr<Shape> clone() const = 0;

2. 具体原型类

  • CircleRectangle 是具体原型类,分别实现了 clone() 方法,返回自身的副本:
std::unique_ptr<Shape> clone() const override {return std::make_unique<Circle>(*this);
}

3. 客户端

  • 客户端通过调用 clone() 方法创建对象,而无需直接依赖具体类:
auto circle1 = circlePrototype->clone();
circle1->draw();

优缺点

优点

  1. 对象复制高效
    • 直接复制对象比重新实例化速度更快。
  2. 减少依赖
    • 客户端代码无需依赖具体类的构造函数。
  3. 动态创建对象
    • 可在运行时动态生成对象,而不是在编译时确定。

缺点

  1. 深拷贝复杂性
    • 如果对象中包含指针或其他复杂资源,需特别注意深拷贝逻辑。
  2. 实现复杂
    • 需要为每个类实现 clone() 方法,增加了一定的代码量。

适用场景

  • 需要大量相似对象:如图形编辑器中需要重复创建类似图形。
  • 对象初始化成本高:如大型游戏中加载模型、地图等资源。
  • 需要动态生成对象:如根据配置文件动态生成对象。

改进与扩展

  1. 深拷贝支持

    • 如果对象包含动态内存或指针成员变量,需要实现深拷贝以避免资源共享问题。
  2. 结合原型注册表

    • 使用注册表保存所有原型实例,根据名称或类型动态克隆对象:
    class PrototypeRegistry {
    private:std::unordered_map<std::string, std::unique_ptr<Shape>> prototypes;public:void registerPrototype(const std::string& name, std::unique_ptr<Shape> prototype) {prototypes[name] = std::move(prototype);}std::unique_ptr<Shape> create(const std::string& name) const {return prototypes.at(name)->clone();}
    };
    

总结

原型模式通过复制已有对象来快速创建新对象,避免了复杂的初始化逻辑,并减少了对具体类的依赖。
它适用于需要动态生成大量相似对象或高效创建对象的场景。

http://www.lryc.cn/news/521189.html

相关文章:

  • 鸿蒙面试 2025-01-10
  • Linux Top 命令 load average 指标解读
  • 31_搭建Redis分片集群
  • 客户案例 | Ansys与索尼半导体解决方案公司合作推进自动驾驶汽车基于场景的感知测试
  • c#-Halcon入门教程——标定
  • MC1.12.2 macOS高清修复OptiFine运行崩溃
  • 精选2款.NET开源的博客系统
  • 转运机器人在物流仓储行业的优势特点
  • 简识MySQL的InnoDB Locking锁的分类
  • 如何通过openssl生成.crt和.key
  • .NetCore 使用 NPOI 读取带有图片的excel数据
  • linux上使用update-alternatives来选择软件版本
  • 【Elasticsearch复合查询】
  • Java List去重:Stream、HashMap与TreeSet对比分析
  • 大师课程:专业角色AE+AI动画动态设计关键帧学院视频课程 Key Frame Academy – Character Animation Launchpad
  • 游戏盾SDK如何防护APP攻击
  • Spring Boot 3.x 整合 Logback 日志框架(支持异步写入)
  • 从0开始学习搭网站第二天
  • 【Unity-Animator】通过 StateMachineBehaviour 实现回调
  • 鸿蒙中自定义slider实现字体大小变化
  • 数据结构与算法之栈: LeetCode 71. 简化路径 (Ts版)
  • STM32-笔记40-BKP(备份寄存器)
  • NAS中不同RAID级别特点与适用场景
  • node.js的进程保活
  • meta name=“viewport“ content=“width=device-width, initial-scale=1.0“
  • 【vue3】 defineExpose 的使用
  • 思维转换:突破思维桎梏,创造更高效的工作与生活
  • OpenCV相机标定与3D重建(55)通用解决 PnP 问题函数solvePnPGeneric()的使用
  • vue3学习日记5 - 项目起步
  • java导出pdf文件