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

原型模式详解及c++代码实现(以自动驾驶感知场景为例)

模式定义

原型模式(Prototype Pattern)是一种创建型设计模式,通过克隆已有对象来创建新对象,避免重复执行昂贵的初始化操作。该模式特别适用于需要高效创建相似对象的场景,是自动驾驶感知系统中处理大量重复数据结构的理想选择。


自动驾驶感知场景分析

在自动驾驶感知系统中,典型的应用场景包括:

  1. 障碍物克隆:快速复制已识别的障碍物模板
  2. 点云分割:高效生成相似的点云聚类实例
  3. 传感器配置:复用基础配置模板创建新传感器实例

本文将重点实现障碍物对象的原型管理模块。


C++实现代码(含详细注释)

#include <iostream>
#include <unordered_map>
#include <memory>
#include <cmath>// ---------------------------- 抽象原型接口 ----------------------------
class ObstaclePrototype {
public:virtual ~ObstaclePrototype() = default;// 克隆接口(关键方法)virtual std::unique_ptr<ObstaclePrototype> clone() const = 0;// 更新状态(示例方法)virtual void updatePosition(float delta_x, float delta_y) = 0;// 显示信息(示例方法)virtual void display() const = 0;
};// ---------------------------- 具体原型实现 ----------------------------
// 车辆障碍物原型
class Vehicle : public ObstaclePrototype {
private:float pos_x;        // X坐标float pos_y;        // Y坐标float length;       // 车长float width;        // 车宽std::string type;   // 车辆类型public:Vehicle(float x, float y, float l, float w, std::string t): pos_x(x), pos_y(y), length(l), width(w), type(std::move(t)) {}// 实现克隆方法(关键实现)std::unique_ptr<ObstaclePrototype> clone() const override {std::cout << "克隆车辆对象 [" << type << "]" << std::endl;return std::make_unique<Vehicle>(*this); // 调用拷贝构造函数}void updatePosition(float delta_x, float delta_y) override {pos_x += delta_x;pos_y += delta_y;}void display() const override {std::cout << "车辆类型: " << type << " 位置(" << pos_x << ", " << pos_y<< ") 尺寸: " << length << "x" << width << std::endl;}// 特有方法:计算占据面积float calculateArea() const {return length * width;}
};// 行人原型
class Pedestrian : public ObstaclePrototype {
private:float pos_x;float pos_y;float speed;int id;public:Pedestrian(float x, float y, float s, int i): pos_x(x), pos_y(y), speed(s), id(i) {}std::unique_ptr<ObstaclePrototype> clone() const override {std::cout << "克隆行人对象 #" << id << std::endl;return std::make_unique<Pedestrian>(*this);}void updatePosition(float delta_x, float delta_y) override {pos_x += delta_x * speed;pos_y += delta_y * speed;}void display() const override {std::cout << "行人 #" << id << " 位置(" << pos_x << ", " << pos_y<< ") 速度: " << speed << "m/s" << std::endl;}// 特有方法:预测运动轨迹void predictTrajectory(float time) const {std::cout << "预测 " << time << "秒后位置: (" << pos_x + speed * time << ", "<< pos_y + speed * time << ")" << std::endl;}
};// ---------------------------- 原型管理器 ----------------------------
class PrototypeManager {
private:std::unordered_map<std::string, std::unique_ptr<ObstaclePrototype>> prototypes;public:// 注册原型void registerPrototype(const std::string& key, std::unique_ptr<ObstaclePrototype> proto) {prototypes[key] = std::move(proto);std::cout << "已注册原型: " << key << std::endl;}// 创建克隆std::unique_ptr<ObstaclePrototype> createClone(const std::string& key) {if (prototypes.find(key) != prototypes.end()) {return prototypes[key]->clone();}std::cerr << "错误: 未找到原型 " << key << std::endl;return nullptr;}// 显示已注册原型void listPrototypes() const {std::cout << "\n=== 已注册原型列表 ===" << std::endl;for (const auto& pair : prototypes) {std::cout << "- " << pair.first << std::endl;}}
};// ---------------------------- 场景演示 ----------------------------
int main() {PrototypeManager manager;// 初始化并注册原型manager.registerPrototype("sedan", std::make_unique<Vehicle>(0, 0, 4.8f, 1.8f, "轿车"));manager.registerPrototype("truck", std::make_unique<Vehicle>(0, 0, 8.5f, 2.5f, "卡车"));manager.registerPrototype("adult", std::make_unique<Pedestrian>(0, 0, 1.2f, 1001));// 显示可用原型manager.listPrototypes();// 动态创建障碍物实例auto obstacle1 = manager.createClone("sedan");auto obstacle2 = manager.createClone("adult");auto obstacle3 = manager.createClone("truck");// 使用克隆对象if (obstacle1) {obstacle1->updatePosition(10.5f, 3.2f);obstacle1->display();// 类型特定操作(需要向下转型)if (auto vehicle = dynamic_cast<Vehicle*>(obstacle1.get())) {std::cout << "车辆面积: " << vehicle->calculateArea() << "m²" << std::endl;}}if (obstacle2) {obstacle2->updatePosition(2.0f, 1.5f);obstacle2->display();if (auto ped = dynamic_cast<Pedestrian*>(obstacle2.get())) {ped->predictTrajectory(5.0f);}}return 0;
}

代码解析

1. 原型接口设计
class ObstaclePrototype {
public:virtual std::unique_ptr<ObstaclePrototype> clone() const = 0;// ...
};
  • 核心克隆方法:强制子类实现对象复制功能
  • 多态支持:统一接口处理各种障碍物类型
2. 具体原型实现
class Vehicle : public ObstaclePrototype {std::unique_ptr<ObstaclePrototype> clone() const override {return std::make_unique<Vehicle>(*this); // 调用拷贝构造函数}// ...
};
  • 深拷贝实现:利用C++的拷贝构造函数确保对象独立性
  • 特有方法保留:各子类可保持专属行为特征
3. 原型管理器
class PrototypeManager {std::unordered_map<std::string, std::unique_ptr<ObstaclePrototype>> prototypes;// ...
};
  • 中央注册表:统一管理所有可用原型
  • 动态扩展:运行时添加/移除原型

运行结果

已注册原型: sedan
已注册原型: truck
已注册原型: adult=== 已注册原型列表 ===
- sedan
- truck
- adult克隆车辆对象 [轿车]
克隆行人对象 #1001
克隆车辆对象 [卡车]
车辆类型: 轿车 位置(10.5, 3.2) 尺寸: 4.8x1.8
车辆面积: 8.64m²
行人 #1001 位置(2.4, 1.8) 速度: 1.2m/s
预测 5秒后位置: (8.4, 7.8)

模式优势分析

在自动驾驶中的价值
  1. 性能优化

    • 避免重复初始化复杂对象(如3D点云数据)
    • 快速复制预处理后的标准障碍物模板
  2. 动态配置

    • 运行时添加新障碍物类型(如特殊车辆)
    • 支持OTA更新障碍物识别参数
  3. 状态保存

    • 克隆历史帧数据用于轨迹预测
    • 创建障碍物快照用于安全校验

扩展改进建议

1. 差异克隆控制
// 扩展克隆接口
enum CloneType { FULL, LIGHTWEIGHT };
virtual std::unique_ptr<ObstaclePrototype> clone(CloneType type) const;
2. 原型版本管理
class VersionedPrototype : public ObstaclePrototype {int version = 1;void updateParameters() { /*...*/ version++; }
};
3. 原型池优化
class PrototypePool {std::vector<std::unique_ptr<ObstaclePrototype>> pool;// 实现对象复用逻辑
};

原型模式总结

核心价值

  • 通过对象克隆替代昂贵的新建操作
  • 保持新对象与原型的一致性
  • 支持动态运行时对象类型扩展

适用场景

  • 需要高效创建大量相似障碍物实例的感知系统
  • 需要保存和恢复传感器数据快照的场景
  • 支持动态加载新障碍物类型的自动驾驶平台

本实现展示了原型模式在自动驾驶感知系统中的典型应用,通过标准化的克隆接口和统一的原型管理,显著提升了系统创建障碍物对象的效率和灵活性。

相关文章:

  • 如何使用Python进行自动化的系统管理?
  • 布隆过滤器如何删除数据
  • 【认知觉醒】是什么? 如何做到 ? ( 持续更新ing )
  • FPGA(现场可编程门阵列)笔记
  • DDS Discovery数据
  • STL简介 + string【上】
  • Python环境中在线训练机器学习模型所遇到的问题及解决方案
  • 不确定与非单调推理的概率方法
  • 2025年大一训练-DP1
  • WebSocket:实现实时双向通信的技术
  • 网络安全-Burp Suite基础篇
  • 手持式三维扫描设备赋能智能汽车制造
  • 第五章 SQLite数据库:5、SQLite 进阶用法:JOIN、UNION、TRIGGER、INDEX、ALIAS、INDEXED BY 等模块
  • 大屏设计与汇报:政务服务可视化实践
  • RUI桌面TV版最新版免费下载-安卓电视版使用教程
  • 2025年03月中国电子学会青少年软件编程(Python)等级考试试卷(二级)真题
  • LIB-ZC, 一个跨平台(Linux)平台通用C/C++扩展库, stream 流操作
  • Windows 11设置开机自动运行 .jar 文件
  • orcad csi 17.4 DRC规则设置及检查
  • 生成器模式深入解析与 Spring 源码应用
  • 魔都眼|上海半马鸣枪:白金标运动员、“箱根之子”齐参赛
  • 明查|俄罗斯征兵部门突袭澡堂抓捕壮丁?
  • 95后男中音胡斯豪敲开芝加哥抒情歌剧院大门
  • 价格周报|本周生猪均价环比上涨,交易均重继续上升
  • 冲线!“天工”夺得全球首个人形机器人半马冠军
  • 梅德韦杰夫:如果欧盟和美国 “撒手不管”,俄罗斯会更快解决俄乌冲突