以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码

//Component.h

  1. #pragma once
  2.  
  3. class Component
  4. {
  5. public:
  6. Component();
  7. virtual ~Component();
  8. virtual void Operation() = ;
  9. virtual void Add(const Component&);
  10. virtual void Remove(const Component&);
  11. virtual Component* getChild(int);
  12. protected:
  13. private:
  14.  
  15. };

//Component.cpp

  1. #include"Component.h"
  2. Component::Component(){}
  3. Component::~Component(){}
  4. void Component::Add(const Component& com){}
  5. void Component::Remove(const Component& com){}
  6. Component* Component::getChild(int index)
  7. {
  8. return ;
  9. }

//composite.h

  1. #include"Component.h"
  2. #include<vector>
  3.  
  4. class Composite :public Component
  5. {
  6. public:
  7. Composite();
  8. virtual ~Composite();
  9. void Add(Component* com);
  10. void Remove(Component* com);
  11. void Operation();
  12. Component* Getchild(int index);
  13. protected:
  14. private:
  15. std::vector<Component*>comVec;
  16. };

//Composite.cpp

  1. #include"Component.h"
  2. #include"composite.h"
  3.  
  4. const int null = ;
  5.  
  6. Composite::Composite(){}
  7. Composite::~Composite(){}
  8.  
  9. void Composite::Operation(){
  10. for (std::vector<Component*>::iterator comIter = comVec.begin(); comIter != comVec.end(); ++comIter)
  11. {
  12. (*comIter)->Operation();
  13. }
  14. }
  15. void Composite::Add(Component* com)
  16. {
  17. comVec.push_back(com);
  18. }
  19. void Composite::Remove(Component* com)
  20. {
  21. //comVec.erase(&com);//此处有问题,求解释!!!
  22. }
  23. Component* Composite::Getchild(int index)
  24. {
  25. return comVec[index];
  26. }

//Leaf.h

  1. #include"Component.h"
  2. class Leaf :public Component
  3. {
  4. public:
  5. Leaf();
  6. virtual ~Leaf();
  7. void Operation();
  8. protected:
  9. private:
  10.  
  11. };

//Leaf.cpp

  1. #include"Leaf.h"
  2. #include<iostream>
  3. Leaf::Leaf(){
  4. }
  5. Leaf::~Leaf(){}
  6. void Leaf::Operation(){
  7. std::cout << "Leaf Operation..." << std::endl;
  8. }

//main.cpp

  1. #include"Component.h"
  2. #include"composite.h"
  3. #include"Leaf.h"
  4. #include<iostream>
  5. #include<string>
  6. int main(int args, char* argv)
  7. {
  8. Leaf* I = new Leaf();
  9. I->Operation();
  10. Composite* com = new Composite();
  11. com->Add(I);
  12. com->Operation();
  13. Component* II = com->Getchild();
  14. getchar();
  15. return ;
  16. }

设计模式-Composite(结构型模式) 用于 递归构建 树 状 的组合结构,与Decorator的区别是 Composite旨在通过构造子类而添加新操作,而Decorator直接添加新操作。的更多相关文章

  1. GoF的23种设计模式之结构型模式的特点和分类

    结构型模式描述如何将类或对象按某种布局组成更大的结构.它分为类结构型模式和对象结构型模式,前者采用继承机制来组织接口和类,后者釆用组合或聚合来组合对象. 由于组合关系或聚合关系比继承关系耦合度低,满足 ...

  2. Go语言实现的23种设计模式之结构型模式

    摘要:本文主要聚焦在结构型模式(Structural Pattern)上,其主要思想是将多个对象组装成较大的结构,并同时保持结构的灵活和高效,从程序的结构上解决模块之间的耦合问题. 本文分享自华为云社 ...

  3. Java设计模式之结构型模式

    结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模式.享元模式. 一.适配器模式: 意图: 将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原本由于接 ...

  4. Java经典23种设计模式之结构型模式(一)

    结构型模式包含7种:适配器模式.桥接模式.组合模式.装饰模式.外观模式.享元模式.代理模式. 本文主要介绍适配器模式和桥接模式. 一.适配器模式(Adapter) 适配器模式事实上非常easy.就像手 ...

  5. 设计模式-Decorator(结构型模式) 用于通过 组合 的方式 给定义的类 添加新的操作,这里不用 继承 的原因是 增加了系统的复杂性,继承使深度加深。

    以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Decorator.h #pragma once class Component { public: virtual ~C ...

  6. GoF23种设计模式之结构型模式之组合模式

    一.概述 将对象组合成树型结构以表示“部分--整体”的层次关系.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.适用性 1.你想表示对象的部分--整体层次结构的时候. 2.你希望用户忽略组 ...

  7. GoF23种设计模式之结构型模式之外观模式

    一.概述         为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 二.适用性 1.当你要为一个复杂子系统提供一个简单接口的时候.子系统 ...

  8. GoF23种设计模式之结构型模式之桥接模式

    一.概述         将类的抽象部分与实现分部分离开来,使它们都可以独立地变化. 二.适用性 1.你不希望在抽象和实现之间有一个固定的绑定关系的时候.例如:在程序运行时实现部分应可以被选择或切换. ...

  9. GoF23种设计模式之结构型模式之享元模式

    一.概述  运用共享技术有效地支持大量细粒度的对象. 二.适用性 1.当一个应用程序使用了大量的对象的时候. 2.由于使用大量的独享而造成很大的存储开销的时候. 3.对象的大多数状态都可变为外部状态的 ...

随机推荐

  1. 设置Redis的LRU策略

    概念 LRU(Least Recently Used)最近最少使用算法是众多置换算法中的一种. maxmemory Redis中有一个maxmemory概念,主要是为了将使用的内存限定在一个固定的大小 ...

  2. How to: Use the Entity Framework Code First in XAF 如何:在 XAF 中使用EF CodeFirst

    This topic demonstrates how to create a simple XAF application with a business model in a DbContext ...

  3. JavaScript 标准内置对象Promise使用学习总结

    Javascript标准内置对象Promise使用学习总结   by:授客 QQ:1033553122 1.   基础用法 var condition = true; let p = new Prom ...

  4. C lang: Compound literal

    Xx_Introduction C99 stantard. Upate array and struct a compound literal. Literal is date type value. ...

  5. dex方法隐藏后的反编译和运行时 效果

    隐藏smali方法后 java源码: int b = fun2(); baksmali解释为: invoke-virtual                  {v1}, <int MainAc ...

  6. 表单生成器(Form Builder)之mongodb表单数据查询——返回分页数据和总条数

    上一篇笔记将开始定义的存储结构处理了一下,将FormItems数组中的表单项都拿到mongodb document的最外层,和以前的关系型数据类似,之不过好多列都是动态的,不固定,不过这并没有什么影响 ...

  7. CG-CTF SQL注入

    SQL注入1 题目 访问题目网址 先查看一下源码 仔细分析一下核心源码 <?php if($_POST[user] && $_POST[pass]) { //判断user和pas ...

  8. React: React组件的生命周期

    一.简介 在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分.在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数 ...

  9. sublime插件开发教程3

    今天就看下api的使用方法 中文的api文档 https://mux.alimama.com/posts/549.html#sublime.View sublime模块 方法 返回值 描述 set_t ...

  10. requeests模块响应体属性和方法重新整理

    下面的属性方法都是基于response对象` import requests response = requests.get('url') 一.url 返回值的url 二. text 获得响应体文本信 ...