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

网站建设设计报价网站建设优秀网站建设

网站建设设计报价,网站建设优秀网站建设,seo外链群发工具,企业所得税政策最新2024税率组合模式(Composite Pattern)允许将对象组合成树形结构,使得客户端以统一的方式处理单个对象和组合对象。以下是一个经典的 C 实现示例,包含透明式设计(基类定义统一接口)和内存管理: #include…

组合模式(Composite Pattern)允许将对象组合成树形结构,使得客户端以统一的方式处理单个对象和组合对象。以下是一个经典的 C++ 实现示例,包含透明式设计(基类定义统一接口)和内存管理:

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <memory>// 抽象组件类
class Component {
public:virtual ~Component() = default;virtual void operation() const = 0;// 管理子组件的透明接口(Leaf需处理不支持的操作)virtual void add(std::unique_ptr<Component> component) {throw std::runtime_error("Unsupported operation: add");}virtual void remove(Component* component) {throw std::runtime_error("Unsupported operation: remove");}virtual const Component* getChild(int index) const {throw std::runtime_error("Unsupported operation: getChild");}
};// 叶子节点类
class Leaf : public Component {
public:explicit Leaf(std::string name) : name_(std::move(name)) {}void operation() const override {std::cout << "Leaf[" << name_ << "] 执行操作" << std::endl;}private:std::string name_;
};// 组合节点类
class Composite : public Component {
public:void operation() const override {std::cout << "Composite 执行操作,包含" << children_.size() << "个子组件:" << std::endl;for (const auto& child : children_) {child->operation(); // 递归调用子组件的操作}}void add(std::unique_ptr<Component> component) override {children_.push_back(std::move(component));}void remove(Component* component) override {auto it = std::find_if(children_.begin(), children_.end(),[component](const std::unique_ptr<Component>& c) {return c.get() == component;});if (it != children_.end()) {children_.erase(it);}}const Component* getChild(int index) const override {if (index >= 0 && index < children_.size()) {return children_[index].get();}return nullptr;}private:std::vector<std::unique_ptr<Component>> children_; // 使用智能指针管理生命周期
};// 客户端代码
int main() {// 创建组合结构:root -> [branch1, branch2]// branch1 -> [leaf1, leaf2]// branch2 -> [leaf3]auto root = std::make_unique<Composite>();auto branch1 = std::make_unique<Composite>();branch1->add(std::make_unique<Leaf>("A"));branch1->add(std::make_unique<Leaf>("B"));auto branch2 = std::make_unique<Composite>();branch2->add(std::make_unique<Leaf>("C"));root->add(std::move(branch1));root->add(std::move(branch2));// 统一调用操作root->operation();return 0;
}

关键点解析:

  1. 透明式设计

    • Component 基类定义了所有组件(包括叶子节点)的公共接口,包括 add/remove 等管理子组件的方法。
    • 叶子节点 Leaf 继承自 Component,但重写 add/remove 时抛出异常(代码中省略异常以简化,实际可添加)。
    • 组合节点 Composite 实现子组件管理逻辑,并递归调用子组件的 operation()
  2. 内存管理

    • 使用 std::unique_ptr 管理子组件生命周期,确保组合对象析构时自动释放所有子组件。
    • 避免手动 new/delete,减少内存泄漏风险。
  3. 递归结构

    • Compositeoperation() 会遍历所有子组件并调用它们的操作,形成递归处理。

输出结果:

Composite 执行操作,包含2个子组件:
Composite 执行操作,包含2个子组件:
Leaf[A] 执行操作
Leaf[B] 执行操作
Composite 执行操作,包含1个子组件:
Leaf[C] 执行操作

此实现展示了组合模式的核心思想:客户端无需区分叶子节点和组合节点,统一通过 Component 接口操作,简化了复杂树形结构的处理。

http://www.yayakq.cn/news/820100/

相关文章:

  • 怎么做自己的门户网站互联网网站建设新闻
  • 那个网站做毕业设计浙江省工程建设协会网站
  • 商城网站策划书专门做签到的网站
  • 成都网站快速排名优化网站建设制作德州
  • 广州乐地网站建设公司国外包装设计网站
  • 免费网站的建设国内外优秀设计网站
  • 网站开发跟网页制作动漫制作专业学校有哪些
  • wap购物网站源码成都科技公司排名
  • 武威百度做网站多少钱微信小程序开发报价
  • 网站建设与管理的论文巩义做网站推广
  • 江苏省品牌专业建设网站网站程序开发的基本步骤
  • 租一个服务器建网站多少钱做网站运营好还是SEO好
  • linux网站开发软件常见的cms网站程序有哪些
  • 金华城乡建设网站seosem有什么区别
  • dede 电商网站模板网站开发背景图模板
  • 网站关键词多少个最好做响应式网站的体会
  • 怎么策划一个网站青岛网站建设哪家好 网络服务
  • 主营 网站建设 app开发长沙网站建设外包
  • 网站建设移动端是什么意思时尚杂志排版设计
  • 4399页游网站延安网站建设
  • 做视频网站 服务器定远建设局网站
  • 如何在手机上搭建网站莱芜搜狗推广
  • 网站建设涉及的内容典型的口碑营销案例
  • 国内免费空间可以做什么网站wordpress接入对象存储
  • wordpress 文章和页面的区别山东自助seo建站
  • 建好网站后如何向里面加东西哔哩哔哩适合夫妻看的电视剧
  • 卖酒的网站做线下怎么做ks免费刷粉网站推广低价
  • 怎么做企业网站建设专业制作门牌
  • 现在搜什么关键词能搜到网站wordpress登录后才能下载
  • 网站搜索排名高怎么做常德优化公司