当前位置: 首页> 教育> 高考 > 开发网站实时监控_手机制作游戏软件_贺贵江seo教程_引擎优化seo是什么

开发网站实时监控_手机制作游戏软件_贺贵江seo教程_引擎优化seo是什么

时间:2025/7/11 0:49:40来源:https://blog.csdn.net/Black_Silencer/article/details/144384153 浏览次数:0次
开发网站实时监控_手机制作游戏软件_贺贵江seo教程_引擎优化seo是什么

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;
}
关键字:开发网站实时监控_手机制作游戏软件_贺贵江seo教程_引擎优化seo是什么

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: