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

c++开发模式,组合模式

组合模式,顾名思义,通过组合关系定义类间的关联关系,实现了将对象组合成树形结构,最终实现类的复用。可能是由于设计模式看的多了,初看组合模式的类图,感觉和装饰者模式类图很相似,都是使用继承和组合关系,当然,也只是结构相似而已。

#include <iostream>
#include <vector>
using namespace std;class Component {
public:virtual void Operation() { }virtual void Add(const Component& com) { }virtual void Remove(const Component& com) { }virtual Component* GetChild(int index) {return 0;}virtual ~Component() { }
};class Composite :public Component {
public:void Add(Component* com) {_coms.push_back(com);}void Operation() {for (auto com : _coms)com->Operation();}void Remove(Component* com) {//_coms.erase(&com);}Component* GetChild(int index) {return _coms[index];}private:std::vector<Component*> _coms;
};class Leaf :public Component {
public:void Operation() {cout << "Leaf::Operation..." << endl;}
};int main() {Leaf *leaf = new Leaf();leaf->Operation();Composite *com = new Composite();com->Add(leaf);com->Operation();Component *leaf_ = com->GetChild(0);leaf_->Operation();delete leaf;delete com;return 0;
}
http://www.lryc.cn/news/109496.html

相关文章:

  • 【GITHUB】FlipIt – Windows的开源翻页时钟
  • 基于 Flink Paimon 实现 Streaming Warehouse 数据一致性管理
  • 云游戏App简记
  • ChatGPT已打破图灵测试,新的测试方法在路上
  • Flask学习笔记_异步CMS(五)
  • 争夺年度智能汽车「中间件」方案提供商TOP10,谁率先入围
  • 【Spring Cloud一】微服务基本知识
  • swift - 如何在数组大小更改后刷新 ForEach 显示元素的数量(SwiftUI、Xcode 11 Beta 5)
  • 编程导航算法村第七关 |二叉树的遍历
  • 【docker】docker-compose安装带ui页面的kafka集群
  • java实现多级菜单
  • HTML中元素和标签有什么区别?
  • android 如何分析应用的内存(十三)——perfetto
  • Chapter20 音乐
  • 详解Nodejs中的模块化
  • debug思路 - maven构建报错
  • DSP学习笔记
  • Java中的Apache Commons Math是什么?
  • 规划路线(微信小程序、H5)
  • 【CSS】视频文字特效
  • linux-MySQL的数据目录
  • AI绘图实战(十二):让AI设计LOGO/图标/标识 | Stable Diffusion成为设计师生产力工具
  • 机器视觉系统设计:基础知识
  • C# Blazor 学习笔记(11):路由跳转和信息传值
  • Centos 7 安装 Python 时 zlib not available 错误解决
  • python sqllite基本操作
  • 记录--基于css3写出的流光登录(注释超详细!)
  • 【测试设计】性能测试工具选择:wrk?jmeter?locust?还是LR?
  • 为什么升级JDK 11后堆外内存使用增长了?
  • Vue自定义防重复点击指令(v-repeatClick)