模式定义
原型模式(Prototype Pattern)是一种创建型设计模式,通过克隆已有对象来创建新对象,避免重复执行昂贵的初始化操作。该模式特别适用于需要高效创建相似对象的场景,是自动驾驶感知系统中处理大量重复数据结构的理想选择。
自动驾驶感知场景分析
在自动驾驶感知系统中,典型的应用场景包括:
- 障碍物克隆:快速复制已识别的障碍物模板
- 点云分割:高效生成相似的点云聚类实例
- 传感器配置:复用基础配置模板创建新传感器实例
本文将重点实现障碍物对象的原型管理模块。
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
原型模式C++实现及在自动驾驶感知的应用

最低0.47元/天 解锁文章
1757

被折叠的 条评论
为什么被折叠?



