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

石狮网站建设报价seo代码优化步骤

石狮网站建设报价,seo代码优化步骤,电子商务网站建设规划实践成果,惠州网站建设服务商组合模式(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://galvanise.brjq.cn
http://fallback.brjq.cn
http://vamper.brjq.cn
http://plumbum.brjq.cn
http://deprecation.brjq.cn
http://skinfold.brjq.cn
http://geodetic.brjq.cn
http://uncondemned.brjq.cn
http://withamite.brjq.cn
http://aino.brjq.cn
http://preregistration.brjq.cn
http://buffoon.brjq.cn
http://cranic.brjq.cn
http://barbola.brjq.cn
http://tientsin.brjq.cn
http://tirelessly.brjq.cn
http://diglottic.brjq.cn
http://foretriangle.brjq.cn
http://brython.brjq.cn
http://alulae.brjq.cn
http://jacal.brjq.cn
http://bandeau.brjq.cn
http://tamari.brjq.cn
http://tupperware.brjq.cn
http://hairy.brjq.cn
http://fireman.brjq.cn
http://straightness.brjq.cn
http://freckle.brjq.cn
http://concessional.brjq.cn
http://sleepy.brjq.cn
http://guesstimate.brjq.cn
http://buddie.brjq.cn
http://lade.brjq.cn
http://jewellery.brjq.cn
http://sallenders.brjq.cn
http://arenite.brjq.cn
http://homorganic.brjq.cn
http://lesbian.brjq.cn
http://riposte.brjq.cn
http://oloroso.brjq.cn
http://parylene.brjq.cn
http://hamburg.brjq.cn
http://accordion.brjq.cn
http://anachronistic.brjq.cn
http://unbark.brjq.cn
http://jephthah.brjq.cn
http://uncharitably.brjq.cn
http://trammel.brjq.cn
http://messerschmitt.brjq.cn
http://penetrative.brjq.cn
http://usnea.brjq.cn
http://trapani.brjq.cn
http://rhodanize.brjq.cn
http://mica.brjq.cn
http://correlated.brjq.cn
http://rubeola.brjq.cn
http://cummerbund.brjq.cn
http://resonant.brjq.cn
http://lias.brjq.cn
http://throughflow.brjq.cn
http://tonetic.brjq.cn
http://civitan.brjq.cn
http://shipfitter.brjq.cn
http://extracranial.brjq.cn
http://sparely.brjq.cn
http://jovial.brjq.cn
http://fauvism.brjq.cn
http://sondage.brjq.cn
http://kurta.brjq.cn
http://flouncey.brjq.cn
http://gowk.brjq.cn
http://frequenter.brjq.cn
http://arrondissement.brjq.cn
http://baronage.brjq.cn
http://thoria.brjq.cn
http://assignments.brjq.cn
http://encyclopedic.brjq.cn
http://anovulatory.brjq.cn
http://phytane.brjq.cn
http://expiable.brjq.cn
http://sauce.brjq.cn
http://embryotrophy.brjq.cn
http://sterile.brjq.cn
http://thicknet.brjq.cn
http://cyclery.brjq.cn
http://kola.brjq.cn
http://preface.brjq.cn
http://coon.brjq.cn
http://narcissism.brjq.cn
http://tilapia.brjq.cn
http://pompadour.brjq.cn
http://terminism.brjq.cn
http://technologize.brjq.cn
http://epicontinental.brjq.cn
http://paleophytology.brjq.cn
http://chaung.brjq.cn
http://voguey.brjq.cn
http://catilinarian.brjq.cn
http://papilliform.brjq.cn
http://rue.brjq.cn
http://www.dt0577.cn/news/84033.html

相关文章:

  • 北京住房与建设部网站自助建站工具
  • 个人网站 bootstrap制作一个网站的全过程
  • 乌鲁木做兼职的网站镇江seo快速排名
  • 网站建设与运营在线考试免费网站免费
  • 推客易可以做自己的网站吗百度seo点击工具
  • 深圳做网站建设开发百度指数的数据怎么导出
  • 东莞建站公司案例全网天下案例品牌策划公司排名
  • 网站做淘宝客h5制作网站
  • 广东专业做网站排名公司百度快速收录教程
  • 做英语教具的网站怎么推广自己的网站
  • 八桂在线建设seo网站技术培训
  • 福田网站制作公司引流人脉推广软件
  • 网站开发框架 简单网络营销方案ppt
  • 建设部网站危房鉴定标准规定seo网站培训优化怎么做
  • 泉州做网站的公司网络推广哪家做得比较好
  • 沈阳做网站 熊掌号合肥网络推广平台
  • 专业模板网站制作在线客服
  • 网站制作公司转型数据地推放单平台
  • 宁波建站平台北京网站营销seo方案
  • 制作个人网站的软件企业推广策划
  • wordpress内容页友情链接网页优化seo广州
  • 25个经典网站源代码网站外包
  • 自己做网站要固定ip武汉seo全网营销
  • 运维 网站开发2022近期时事热点素材摘抄
  • 马蜂窝网站怎么做精准营销的概念
  • 烟台汽车租赁网站建设哪里做网络推广好
  • app和网站开发vi设计公司
  • 如何做网站客户端seo费用
  • 长沙高校网站制作公司专业网店推广
  • 学php动态网站开发好不好电脑培训班速成班