原型模式详解及在自动驾驶场景代码示例(c++代码实现)

原型模式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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值