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

C++设计模式之组合模式中如何实现同一层部件的有序性

在组合模式中,为了实现同一层上部件的有序性,可以采取以下几种设计方法:

1. 使用有序集合

使用有序集合(如 std::liststd::vector 或其他有序容器)来存储和管理子部件。这种方法可以确保子部件按照特定顺序排列,并且可以通过索引访问。

示例代码:
#include <vector>
#include <iostream>class Component {
public:virtual void operation() = 0;virtual ~Component() {}
};class Leaf : public Component {
public:void operation() override {std::cout << "Leaf operation" << std::endl;}
};class Composite : public Component {
public:void operation() override {for (auto& component : components) {component->operation();}}void add(Component* component) {components.push_back(component);}void remove(Component* component) {components.erase(std::remove(components.begin(), components.end(), component), components.end());}private:std::vector<Component*> components;
};int main() {Composite* root = new Composite();root->add(new Leaf());root->add(new Leaf());root->add(new Leaf());root->operation(); // 输出 "Leaf operation" 三次delete root;return 0;
}

在这个示例中,Composite 类使用 std::vector 来存储子部件,确保它们按添加顺序排列。

2. 使用索引管理

在添加子部件时,可以指定一个索引位置,从而控制子部件的排列顺序。

示例代码:
#include <vector>
#include <iostream>class Component {
public:virtual void operation() = 0;virtual ~Component() {}
};class Leaf : public Component {
public:void operation() override {std::cout << "Leaf operation" << std::endl;}
};class Composite : public Component {
public:void operation() override {for (auto& component : components) {component->operation();}}void add(Component* component, int index) {if (index < 0 || index > components.size()) {index = components.size();}components.insert(components.begin() + index, component);}void remove(Component* component) {components.erase(std::remove(components.begin(), components.end(), component), components.end());}private:std::vector<Component*> components;
};int main() {Composite* root = new Composite();root->add(new Leaf(), 0); // 插入到位置 0root->add(new Leaf(), 1); // 插入到位置 1root->add(new Leaf(), 0); // 插入到位置 0,原来的部件后移root->operation(); // 输出 "Leaf operation" 三次,顺序为新插入的第一个,然后是原来的第一个,最后是原来的第二个delete root;return 0;
}

在这个示例中,add 方法允许你指定插入子部件的索引位置,从而控制子部件的排列顺序。

3. 使用排序标准

如果你需要更复杂的排序逻辑(例如按某些属性排序),可以在添加子部件后对集合进行排序。

示例代码:
#include <vector>
#include <algorithm>
#include <iostream>class Component {
public:virtual void operation() = 0;virtual ~Component() {}virtual int getPriority() const = 0; // 排序标准
};class Leaf : public Component {
public:Leaf(int priority) : priority(priority) {}void operation() override {std::cout << "Leaf operation with priority " << priority << std::endl;}int getPriority() const override {return priority;}
private:int priority;
};class Composite : public Component {
public:void operation() override {std::sort(components.begin(), components.end(), [](Component* a, Component* b) {return a->getPriority() < b->getPriority();});for (auto& component : components) {component->operation();}}void add(Component* component) {components.push_back(component);}void remove(Component* component) {components.erase(std::remove(components.begin(), components.end(), component), components.end());}int getPriority() const override {return 0; // 组合节点的优先级}private:std::vector<Component*> components;
};int main() {Composite* root = new Composite();root->add(new Leaf(3));root->add(new Leaf(1));root->add(new Leaf(2));root->operation(); // 输出 "Leaf operation with priority 1", "Leaf operation with priority 2", "Leaf operation with priority 3"delete root;return 0;
}

在这个示例中,Leaf 类有一个 priority 属性,Composite 类在执行操作时会根据优先级对子部件进行排序,从而实现有序性。

总结

通过使用有序集合、索引管理或排序标准,你可以在组合模式中实现同一层上部件的有序性。这些方法可以根据具体需求灵活选择,以满足不同的排序和组织要求

http://www.lryc.cn/news/495027.html

相关文章:

  • duxapp RN 端使用AppUpgrade 进行版本更新
  • 【计网】自定义序列化反序列化(三) —— 实现网络版计算器【下】
  • 神经网络中的优化方法(一)
  • Linux 计算机网络基础概念
  • qt QGraphicsEllipseItem详解
  • Python websocket
  • 【MySQL-5】MySQL的内置函数
  • 深度学习笔记之BERT(三)RoBERTa
  • C++知识点总结(59):背包型动态规划
  • C++:反向迭代器的实现
  • webGL入门教程_04vec3、vec4 和齐次坐标总结
  • uniapp中父组件数组更新后与页面渲染数组不一致实战记录
  • 优化 Conda 下载速度:详细的代理配置和网络管理策略
  • 服务器遭受DDoS攻击后如何恢复运行?
  • MFC音视频播放器-支持电子放大等功能
  • c语言编程1.17蓝桥杯历届试题-回文数字
  • el-table 纵向 横向 多级表头
  • uniapp开发微信小程序笔记8-uniapp使用vant框架
  • 分布式项目使用Redis实现数据库对象自增主键ID
  • npm-运行项目报错:A complete log of this run can be found .......npm-cache_logs\
  • SolarCube: 高分辨率太阳辐照预测基准数据集
  • 华为小米苹果三星移动设备访问windows共享文件夹windows11
  • 网络安全三防指南:只防病毒不安全
  • 论文概览 |《Urban Analytics and City Science》2023.05 Vol.50 Issue.4
  • 【ROS2】ROS2 C++版本 与 Python版本比较
  • 物联网射频识别和RFID开发(一):RFID基础—概念、应用
  • JVM:即时编译器,C2 Compiler,堆外内存排查
  • webpack5 的五大核心配置(二)
  • 【查询基础】.NET开源 ORM 框架 SqlSugar 系列
  • git push使用