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

//Component.h

#pragma once

class Component
{
public:
Component();
virtual ~Component();
virtual void Operation() = ;
virtual void Add(const Component&);
virtual void Remove(const Component&);
virtual Component* getChild(int);
protected:
private: };

//Component.cpp

#include"Component.h"
Component::Component(){}
Component::~Component(){}
void Component::Add(const Component& com){}
void Component::Remove(const Component& com){}
Component* Component::getChild(int index)
{
return ;
}

//composite.h

#include"Component.h"
#include<vector> class Composite :public Component
{
public:
Composite();
virtual ~Composite();
void Add(Component* com);
void Remove(Component* com);
void Operation();
Component* Getchild(int index);
protected:
private:
std::vector<Component*>comVec;
};

//Composite.cpp

#include"Component.h"
#include"composite.h" const int null = ; Composite::Composite(){}
Composite::~Composite(){} void Composite::Operation(){
for (std::vector<Component*>::iterator comIter = comVec.begin(); comIter != comVec.end(); ++comIter)
{
(*comIter)->Operation();
}
}
void Composite::Add(Component* com)
{
comVec.push_back(com);
}
void Composite::Remove(Component* com)
{
//comVec.erase(&com);//此处有问题,求解释!!!
}
Component* Composite::Getchild(int index)
{
return comVec[index];
}

//Leaf.h

#include"Component.h"
class Leaf :public Component
{
public:
Leaf();
virtual ~Leaf();
void Operation();
protected:
private: };

//Leaf.cpp

#include"Leaf.h"
#include<iostream>
Leaf::Leaf(){
}
Leaf::~Leaf(){}
void Leaf::Operation(){
std::cout << "Leaf Operation..." << std::endl;
}

//main.cpp

#include"Component.h"
#include"composite.h"
#include"Leaf.h"
#include<iostream>
#include<string>
int main(int args, char* argv)
{
Leaf* I = new Leaf();
I->Operation();
Composite* com = new Composite();
com->Add(I);
com->Operation();
Component* II = com->Getchild();
getchar();
return ;
}

设计模式-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. config-server-bus动态更新配置

    config-server用来搭建配置中心,而配置信息一般使用gitlab仓库来存储,这样在你的配置发生改变时,不需要从新打包,而如果使用native的试,则需要从新打一个config-server的 ...

  2. Cesium数据可视化-仓储调度系统可视化部分(附github源码)

    Cesium数据可视化-仓储调度系统可视化部分 目的 仓储调度系统需要一个可视化展示物资运输实况的界面,需要配合GPS设备发送的位置信息,实时绘制物资运输情况和仓储仓库.因此,使用Cesium可视化该 ...

  3. 使用objc runtime实现iOS绿色的懒加载

    使用objc runtime实现懒加载 地址:AutoPropertyCocoa 本文所指懒加载形式如下 - (id)lazyloadProperty{ if(_lazyloadProperty == ...

  4. 分布式全局唯一ID生成策略​

    一.背景 分布式系统中我们会对一些数据量大的业务进行分拆,如:用户表,订单表.因为数据量巨大一张表无法承接,就会对其进行分库分表. 但一旦涉及到分库分表,就会引申出分布式系统中唯一主键ID的生成问题. ...

  5. 11.2 Data Guard Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 1304939.1)

    11.2 Data Guard Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 1304939.1) APPLIES ...

  6. Centos7_sl命令跑火车

    一.更新yum源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo 二.安装sl命令 yum ...

  7. Node.js上传文件出现Unexpected field

    上传文件时,input框的name值要与node接口中single(' ')中的参数一致,否则会报"意外字段的错" 前端用的layui 后端node接口

  8. 火车票买不到?看我用python监控票源

    同事说最近火车票难买,我就帮他用个脚本监控 一下. 最近高铁票比较难买,还有什么候补.要不停的刷,才有时候可以捡漏.有时候明明候补了,到快开车告诉你余票不足,候补失败. 凡事预则立,我打算写个脚本提前 ...

  9. Linux中长时间运行程序的方法

    一.场景: 如果临时有一个命令需要长时间运行,比如 python hello.py ,什么方法能最简便的保证它在后台稳定运行呢?解决方法:      当用户注销(logout)或者网络断开时,终端会收 ...

  10. 如何设计APP版本号?

    示例: 2.14.21 (主版本号.次版本号.补丁号) 我们可以这样设计,软件包的版本号以英文句号分隔的三个数字来定义,分别代表主版本号.次版本号和补丁号.如果只是修复了错误,没有添加任何功能,也不会 ...