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

青岛手机网站制作18款未成年禁用软件app

青岛手机网站制作,18款未成年禁用软件app,免费企业网站程序上传,wordpress 主题 餐饮模式定义 原型模式(Prototype Pattern)是一种创建型设计模式,通过克隆已有对象来创建新对象,避免重复执行昂贵的初始化操作。该模式特别适用于需要高效创建相似对象的场景,是自动驾驶感知系统中处理大量重复数据结构的…

模式定义

原型模式(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;// 实现对象复用逻辑
};

原型模式总结

核心价值

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

适用场景

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

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


文章转载自:

http://e5Fl6Zza.LxjxL.cn
http://e7M3HMgE.LxjxL.cn
http://4lTZN6wj.LxjxL.cn
http://zrU7ZheP.LxjxL.cn
http://VR9Gdqlg.LxjxL.cn
http://Lq9IyWax.LxjxL.cn
http://dTXqMbdQ.LxjxL.cn
http://8VL7i7ZP.LxjxL.cn
http://CR9lZcu6.LxjxL.cn
http://SSsrq6Cg.LxjxL.cn
http://JSAWARmg.LxjxL.cn
http://MnTiQQpF.LxjxL.cn
http://CSH2JoOB.LxjxL.cn
http://Div5j0g2.LxjxL.cn
http://16EiBMTi.LxjxL.cn
http://x7N2BbAL.LxjxL.cn
http://FTjf0Gs1.LxjxL.cn
http://QxumHNwa.LxjxL.cn
http://zg80oJdr.LxjxL.cn
http://ez7t6zO1.LxjxL.cn
http://Mxbdd3uH.LxjxL.cn
http://rcTIHQvA.LxjxL.cn
http://cfkHSs1k.LxjxL.cn
http://oXP1mvB6.LxjxL.cn
http://pFTEYX49.LxjxL.cn
http://zw1MRgqq.LxjxL.cn
http://RlNWINJR.LxjxL.cn
http://2VgxXWgJ.LxjxL.cn
http://rMAQnMql.LxjxL.cn
http://k59L8Nc5.LxjxL.cn
http://www.dtcms.com/wzjs/723762.html

相关文章:

  • 稳定的常州网站推广四川省建设网招标公告
  • 石家庄行业网站网站建设不完整(网站内容太少)
  • 购物网站 系统设计中国建设银行网站运营模式
  • 网站建设学校常用的网站有哪些
  • 电子商务网站建设实训wordpress 插件管理
  • 想要一个网站哪家云服务器性价比高
  • 泉山微网站开发北京王府井大楼
  • 中国建设银行建银购网站赞赏分享wordpress代码
  • .net 网站模板下载地址做网站一定需要服务器吗
  • 外国语学院英文网站建设电子商务网站建设方
  • 婚礼纪网站怎么做请帖无锡工业设计公司
  • 网站基础设施建设土木英才网招聘信息
  • 做摄影和后期的兼职网站我们的优势的网站
  • 公司做网站的开支会计分录怎么做皮肤科医生免费问诊
  • 旅游网站对比模板下载深圳定制巴士线路查询
  • 深圳送花网站哪个好网站搭建 虚拟空间
  • 做网站是先做后台还是前端百度推广 网站吸引力
  • 网站建设质量要求库存管理系统软件
  • 国家城乡建设网站网站优化与seo
  • 网站建设系统规划动漫模板素材
  • 网站建设参考wordpress萧涵主题
  • 网站开发用笔记本电脑网上做任务的网站有哪些
  • 河北省住房及城乡建设部网站网易企业邮箱登录页
  • 网站存储空间重庆网站备案公司
  • 汕头企业建站系统模板医院图书馆网站建设的意义
  • 免费域名网站php淘客网站超级搜怎么做
  • google广告联盟网站帮别人发广告赚钱平台
  • 旅游商务网站开发哪里ui培训班好
  • 怎么注册一个公司网站网络服务器地址
  • 免费企业建站模板实业 东莞网站建设