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

Composite Pattern

Composite Pattern

The intent of Composite pattern is to composite objects into tree structures to represent a “part-whole” hierarchy .The Composite Pattern allow clients to treat individual objects and composite objects uniformly.

UML

Used in Qt

Examples include the QObject hierarchy, QGraphicsItem and QGraphicsScene, and so on.

Some Codes:

1.Defining the Component Interface
#include <iostream>
#include <vector>
#include <memory>// Component (Base class)
class Graphic {
public:virtual ~Graphic() = default;// Pure virtual function for drawingvirtual void draw() const = 0;// Common operation like resizingvirtual void resize(double factor) = 0;
};
2. Defining the Leaf Classes
// Leaf (Concrete) classes
class Circle : public Graphic {
private:double x, y, radius;public:Circle(double x, double y, double radius) : x(x), y(y), radius(radius) {}void draw() const override {std::cout << "Drawing Circle at (" << x << ", " << y << ") with radius " << radius << std::endl;}void resize(double factor) override {radius *= factor;}
};class Square : public Graphic {
private:double sideLength;public:Square(double sideLength) : sideLength(sideLength) {}void draw() const override {std::cout << "Drawing Square with side length " << sideLength << std::endl;}void resize(double factor) override {sideLength *= factor;}
};
3、Defining the Composite Class
// Composite class
class CompositeGraphic : public Graphic {
private:std::vector<std::shared_ptr<Graphic>> children;public:// Add a child graphicvoid add(std::shared_ptr<Graphic> graphic) {children.push_back(graphic);}// Remove a child graphicvoid remove(std::shared_ptr<Graphic> graphic) {children.erase(std::remove(children.begin(), children.end(), graphic), children.end());}// Draw all childrenvoid draw() const override {for (const auto& child : children) {child->draw();  // Recursively draw all children}}// Resize all childrenvoid resize(double factor) override {for (const auto& child : children) {child->resize(factor);  // Recursively resize all children}}
};
4.Client Code
int main() {// Create some leaf objectsstd::shared_ptr<Graphic> circle1 = std::make_shared<Circle>(0.0, 0.0, 5.0);std::shared_ptr<Graphic> circle2 = std::make_shared<Circle>(2.0, 3.0, 3.0);std::shared_ptr<Graphic> square = std::make_shared<Square>(4.0);// Create a composite objectstd::shared_ptr<CompositeGraphic> compositeGraphic = std::make_shared<CompositeGraphic>();compositeGraphic->add(circle1);compositeGraphic->add(circle2);compositeGraphic->add(square);// Draw all graphics (including leaves and composites)std::cout << "Drawing the entire graphic collection:" << std::endl;compositeGraphic->draw();// Resize all graphics (including leaves and composites)compositeGraphic->resize(1.5);std::cout << "\nAfter resizing:" << std::endl;compositeGraphic->draw();return 0;
}
http://www.lryc.cn/news/501376.html

相关文章:

  • Springboot MVC
  • MySQL数据表记录增操作
  • maven报错“找不到符号“
  • python进阶-05-利用Selenium来实现动态爬虫
  • P1226 【模板】快速幂
  • 【C++】求第二大的数详细解析
  • 从零开始学TiDB(3)TiKV 持久化机制
  • Elasticsearch+Kibana+IK分词器+拼音分词器安装
  • 子网划分实例
  • 上海亚商投顾:创业板指震荡调整 机器人概念股再度爆发
  • 【C++ 20进阶(2):初始化 Initializer
  • 【重生之我在B站学MySQL】
  • Python实现中国象棋
  • LBS 开发微课堂|通过openGL ES轻松实现建筑物渲染及动画
  • map1[item.id]和map1.get(item.id)的区别为何前者取出的是空,后者取出的是正确的值
  • window端sqlplus连接linux_oracle11g
  • Go支付中台方案:多平台兼容与多项目对接
  • MySQL触发器的使用详解
  • 关于NLP交互式系统的一些基础入门
  • 如何在HTML中修改光标的位置(全面版)
  • PHP8 动态属性被弃用兼容方案
  • WPF表格控件的列利用模块适配动态枚举类
  • 【sgUploadImage】自定义组件:基于elementUI的el-upload封装的上传图片、相片组件,适用于上传缩略图、文章封面
  • Scala的隐式转换
  • 从视频编码的进化历程看技术革新
  • ECharts柱状图-阶梯瀑布图,附视频讲解与代码下载
  • 如何让Google快速收录你的页面?
  • 比例负载分配L(P);动态调整服务率:LDS
  • C++ ——— 类的 6 个默认成员函数之 构造函数
  • win11 恢复任务栏copilot图标, 亲测有效