当前位置: 首页 > 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;// 实现对象复用逻辑
};

原型模式总结

核心价值

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

适用场景

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

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

相关文章:

  • Flash存储器(一):接口标准全解析
  • z-library电子图书馆最新地址的查询方法
  • 多肽 N 端乙酰化有什么作用?
  • java八股之并发编程
  • 4月18日复盘
  • 定时器复习DSP【2025/4/18】
  • datasheet数据手册-阅读方法
  • MMAction2安装
  • Docker安装hoppscotch
  • 什么是MMOE?
  • 【统计显著性检验】
  • 考研单词笔记 2025.04.18
  • openEuler系统下源码编译安装Nginx实践教程
  • 关于GPU的涡轮散热与被动散热
  • Fastapi 日志处理
  • 医学教育视频会议系统私有化部署方案
  • rLLM - 使LLM的强化学习民主化
  • 《软件设计师》复习笔记(11.3)——需求获取、分析、定义、验证、管理
  • 每日一题---移动零
  • C# 点击导入,将需要的参数传递到弹窗的页面
  • 95后男中音胡斯豪敲开芝加哥抒情歌剧院大门
  • 观察|智驾监管升级挤掉宣传水分,行业或加速驶入安全快车道
  • 西安旅游:2024年营业收入约5.82亿元,同比增长5.88%
  • 从高铁到住房:“富足议程”能否拯救美国的进步主义?
  • “杭州六小龙”爆火出圈后,浙江高规格部署人工智能发展
  • 寒武纪一季度营收猛增42倍,净利3.55亿元,连续两个季度盈利