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

19.组合模式(Composite)

意图:将对象组成树状结构以表示“部分-整体”的层次结构,使得Client对单个对象和组合对象的使用具有一致性。

上下文:在树型结构的问题中,Client必须以不同的方式处理单个对象和组合对象。能否提供一种封装,统一简单元素和复杂元素的概念,让对象容器自己来实现自身的复杂结构,让Client可以像处理简单元素一样来处理复杂元素,从而使Client与复杂元素的内部结构解耦?

UML

在这里插入图片描述

Component:为Composite中的对象声明接口;在适当情况下,实现所有类公共接口的默认行为;声明一个接口,用于访问和管理Component的子部件;在递归结构中定义一个接口,用于访问一个父部件,并在适当的情况下实现它。
Leaf:在Composite中表示叶子对象。
Composite:存储子部件,并定义有子部件的那些部件的行为。
Client:通过Component接口操作Composite的对象。

在这里插入图片描述

代码:

#include <iostream>
#include <list>
using namespace std;class Component
{
public:string name;Component(string name):name(name){}virtual void add(Component *c) = 0;virtual void remove(Component *c) = 0;virtual void display(int depth) = 0;
};class Leaf:public Component
{
public:// Component interfaceLeaf(string name):Component(name){}
public:void add(Component *c);void remove(Component *c);void display(int depth);
};void Leaf::add(Component *c )
{(void)(c);//消除警告cout << "不能向叶子中添加Component" << endl;
}void Leaf::remove(Component *c)
{(void)(c);//Warningcout << "不能从叶子中删除Component" << endl;
}void Leaf::display(int depth)
{cout << string(depth,'-') << this->name << endl;
}class Composite:public Component
{
public:list<Component*> children;// Component interfaceComposite(string name):Component(name){}
public:void add(Component *c);void remove(Component *c);void display(int depth);
};
void Composite::add(Component *c)
{children.push_back(c);
}void Composite::remove(Component *c)
{children.remove(c);
}void Composite::display(int depth)
{cout << string(depth,'-') << this->name << endl;list<Component*>::iterator it;for(it = children.begin();it != children.end();it++){Component *c = *it;c->display(depth + 2);}
}
int main()
{Composite *root = new Composite("树干");root->add(new Leaf("树叶1"));root->add(new Leaf("树叶2"));Composite *c1 = new Composite("树枝1");c1->add(new Leaf("树叶1-1"));c1->add(new Leaf("树叶1-2"));root->add(c1);Composite *c1_1 = new Composite("树枝1-1");c1_1->add(new Leaf("树叶1-1-1"));c1_1->add(new Leaf("树叶1-1-2"));c1->add(c1_1);root->add(new Leaf("树叶3"));root->display(1);return 0;
}

结果

-树干
---树叶1
---树叶2
---树枝1
-----树叶1-1
-----树叶1-2
-----树枝1-1
-------树叶1-1-1
-------树叶1-1-2
---树叶3
http://www.lryc.cn/news/177365.html

相关文章:

  • 应用在IPM接口隔离领域中的光电耦合器
  • rust引用
  • Android AMS——Activity Pause(八)
  • 【数据结构】冒泡排序,快速排序的学习知识总结
  • ubuntu终端 中文显示 改为 英文显示
  • ChatGPT Prompting开发实战(十二)
  • springboot整合eureka
  • 记录一个 GUI 库的对比测试结果
  • 解决 MyBatis-Plus 中增加修改时,对应时间的更新问题
  • 【力扣2057】值相等的最小索引
  • 计算机图像处理:图像轮廓
  • 解决java.io.FileNotFoundException: HADOOP_HOME and hadoop.home.dir are unset.的错误
  • 软件设计中常见的设计模式
  • 为什么我的remix没有injected web3
  • 第1章 数据结构绪论
  • 现代 GPU 容易受到新 GPU.zip 侧通道攻击
  • 8+单基因+细胞凋亡+WGCNA+单细胞+实验验证
  • BM4 合并两个排序的链表
  • 【lesson12】理解进程地址空间
  • 计算机里的神灵(SCIP)
  • 基于微信小程序的公交信息在线查询系统小程序设计与实现(源码+lw+部署文档+讲解等)
  • 【STM32】IAP升级01 bootloader实现以及APP配置(主要)
  • ruoyi(若依)接口拦截路径配置,接口访问要授权,放开授权直接访问
  • Ctfshow web入门 XSS篇 web316-web333 详细题解 全
  • watch()监听vue2项目角色权限变化更新挂载
  • 轻量化设计、佩戴更舒适——轻律 Umelody U1头戴式蓝牙耳机
  • 嵌入式Linux应用开发-基础知识-第三章 LED原理图-GPIO及操作
  • 外贸人员如何选择适合的邮箱服务
  • pt29django教程
  • 【操作系统笔记七】进程和线程