C++ 全栈知识体系C++ 全栈知识体系
✿导航
  • 基础
  • 函数
  • 知识点
  • IO框架
  • 新版本特性
  • 数据库原理
  • SQL语言
  • SQL - MySQL
  • NoSQL - Redis
  • NoSQL - ElasticSearch
  • 算法基础
  • 常见算法
  • 领域算法
  • 分布式算法
  • 数据结构与算法
  • 计算机网络
  • 操作系统
  • 计算机组成
  • 开发
  • 测试
  • 架构基础
  • 分布式系统
  • 微服务
  • 中间件
  • 概念
  • 理论
  • 架构设计原则
  • 设计模式
  • 协议
  • 技术选型
  • 编码规范
  • 流水线构建 - CI/CD
  • 知识点 - Linux
  • 网站 - Nginx
  • 容器化 - Docker
  • 容器编排 - Kubernetes
  • 服务网格 - Service Mesh Istio
  • 常用快捷键 - Shortcut
  • 工具使用 - Tools
  • 开源项目
  • 学习项目
  • 个人项目
  • 项目开发
  • 项目Idea
  • 并发
  • 部署
  • 分布式
  • 知识
  • 问题
  • 编程语言与技术
  • 系统与架构
  • 软件开发实践
  • 数据处理与应用设计
  • 个人
  • 产品
  • 团队
  • 知识体系
  • Vue
关于
✿导航
  • 基础
  • 函数
  • 知识点
  • IO框架
  • 新版本特性
  • 数据库原理
  • SQL语言
  • SQL - MySQL
  • NoSQL - Redis
  • NoSQL - ElasticSearch
  • 算法基础
  • 常见算法
  • 领域算法
  • 分布式算法
  • 数据结构与算法
  • 计算机网络
  • 操作系统
  • 计算机组成
  • 开发
  • 测试
  • 架构基础
  • 分布式系统
  • 微服务
  • 中间件
  • 概念
  • 理论
  • 架构设计原则
  • 设计模式
  • 协议
  • 技术选型
  • 编码规范
  • 流水线构建 - CI/CD
  • 知识点 - Linux
  • 网站 - Nginx
  • 容器化 - Docker
  • 容器编排 - Kubernetes
  • 服务网格 - Service Mesh Istio
  • 常用快捷键 - Shortcut
  • 工具使用 - Tools
  • 开源项目
  • 学习项目
  • 个人项目
  • 项目开发
  • 项目Idea
  • 并发
  • 部署
  • 分布式
  • 知识
  • 问题
  • 编程语言与技术
  • 系统与架构
  • 软件开发实践
  • 数据处理与应用设计
  • 个人
  • 产品
  • 团队
  • 知识体系
  • Vue
关于
  • 概念

    • 概念 - 概述
    • 概念 - 计算机专有名词
    • 概念 - 正向代理和反向代理
    • 概念 - 云网络
    • 概念 - rest api
    • 概念 - 脑裂
  • 理论

    • 事务理论 - ACID
    • 分布式理论 - CAP
    • 分布式理论 - BASE
  • 架构设计原则

    • 架构设计原则 - 合适、简单、演化
    • 架构设计原则 - 高内聚、低耦合
    • 架构设计原则 - 正交四原则
    • 架构设计原则 - SOLID详解
    • 架构设计原则 - 分层架构MVC
    • 架构设计原则 - DDD领域驱动设计:贫血模型和充血模型
    • 架构设计原则 - DDD领域驱动设计
  • 设计模式

    • 创建型模式 - Create model

      • 创建型模式 - 单例模式(Singleton)
      • 创建型模式 - 工厂模式(Factory)
      • 创建型模式 - 抽象工厂(Abstract Factory)
      • 创建型模式 - 生成器(Builder)
      • 创建型模式 - 原型模式(Prototype)
    • 结构型模式 - Structural model

      • 结构型模式 - 外观(Facade)
      • 结构型模式 - 适配器(Adapter)
      • 结构型模式 - 桥接(Bridge)
      • 结构型模式 - 组合(Composite)
      • 结构型模式 - 装饰(Decorator)
      • 结构型模式 - 享元(Flyweight)
      • 结构型模式 - 代理(Proxy)
    • 行为型模式 - Behavioral model

      • 行为型模式 - 责任链(Chain Of Responsibility)
      • 行为型模式 - 策略(Strategy)
      • 行为型模式 - 模板模式(Template)
      • 行为型模式 - 命令模式(Command)
      • 行为型模式 - 观察者(Observer)
      • 行为型模式 - 访问者(Visitor)
      • 行为型模式 - 状态(State)
      • 行为型模式 - 解释器(Interpreter)
      • 行为型模式 - 迭代器(Iterator)
      • 行为型模式 - 中介者(Mediator)
      • 行为型模式 - 备忘录(Memento)
  • 协议

    • 协议 - Http
    • 协议 - SNMP
    • 协议 - NETCONF
    • 协议 - TLS和SSL
    • 协议 - Http-wiki
    • 协议 - TCP/IP
    • 协议 - Https常见的认证模式
  • 技术选型

    • 技术选型 - 常用的技术框架
    • 技术选型 - 如何写一个自己的项目
    • 技术选型 - 基于drogon实现用户中心后端
  • 编码规范

    • 编码规范 - Google C++ Style Guide
    • 编码规范 - 编程风格
    • 编码规范 - 头文件包含规范
    • 编码规范 - 常用编码命名规则
    • 编码规范 - 编码命名规范

结构型 - 组合(Composite)

组合模式(composite pattern): 允许你将对象组合成树形结构来表现"整体/部分"层次结构. 组合能让客户以一致的方式处理个别对象以及对象组合

​[[toc]]

概念

Composite 模式典型的结构图为:

组件(Component)类是组合类(Composite)和叶子类(Leaf)的父类,可以把组合类看成是树的中间节点。 组合对象拥有一个或者多个组件对象,因此组合对象的操作可以委托给组件对象去处理,而组件对象可以是另一个组合对象或者叶子对象。

代码实现

#ifndef _COMPONENT_H_
#define _COMPONENT_H_
class Component
{
public:
    Component();
    virtual ~Component();

public:
    virtual void Operation() = 0;
    virtual void Add(const Component &);
    virtual void Remove(const Component &);
    virtual Component *GetChild(int);

protected:
private:
};
#endif //~_COMPONENT_H_

#include "Component.h"

Component::Component()
{
}

Component::~Component()
{
}

void Component::Add(const Component &com)
{
}

Component *Component::GetChild(int index)
{
    return 0;
}

void Component::Remove(const Component &com)
{
}

#ifndef _COMPOSITE_H_
#define _COMPOSITE_H_
#include "Component.h"
#include <vector>
using namespace std;

class Composite : public Component
{
public:
    Composite();
    ~Composite();

public:
    void Operation();
    void Add(Component *com);
    void Remove(Component *com);
    Component *GetChild(int index);

protected:
private:
    vector<Component *> comVec;
};
#endif //~_COMPOSITE_H_

#include "Composite.h"
#include "Component.h"
#define NULL 0 // define NULL POINTOR

Composite::Composite()
{
    // vector<Component*>::iterator itend =comVec.begin();
}

Composite::~Composite()
{
}

void Composite::Operation()
{
    vector<Component *>::iterator comIter = comVec.begin();
    for (; comIter != comVec.end(); comIter++)
    {
        (*comIter)->Operation();
    }
}

void Composite::Add(Component *com)
{
    comVec.push_back(com);
}

void Composite::Remove(Component *com)
{
    //comVec.erase(&com);

    for (std::vector<Component *>::iterator iter = comVec.begin(); iter != comVec.end(); )
    {
        if (*iter == com)
        {
            iter =comVec.erase(iter);
        }
        else
        {
            iter++;
        }
    }
}

Component *Composite::GetChild(int index)
{
    return comVec[index];
}

#ifndef _LEAF_H_
#define _LEAF_H_
#include "Component.h"

class Leaf : public Component
{
public:
    Leaf();
    ~Leaf();
    void Operation();

protected:
private:
};

#endif //~_LEAF_H_

#include "Leaf.h"
#include <iostream>
using namespace std;

Leaf::Leaf()
{
}

Leaf::~Leaf()
{
}

void Leaf::Operation()
{
    cout << "Leaf operation....." << endl;
}

#include "Component.h"
#include "Composite.h"
#include "Leaf.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
	Leaf *l = new Leaf();
	l->Operation();
	Composite *com = new Composite();
	com->Add(l);
	com->Operation();
	Component *ll = com->GetChild(0);
	ll->Operation();
	return 0;
}

[root@VM-16-6-centos Composite]# ./ComponentTest
Leaf operation.....
Leaf operation.....
Leaf operation.....

代码说明

Composite 模式在实现中有一个问题就是要提供对于子节点( Leaf)的管理策略,这里使用的是 STL 中的 vector,可以提供其他的实现方式,如数组、链表、 Hash 表等。

讨论

Composite 模式通过和 Decorator 模式有着类似的结构图, 但是 Composite 模式旨在构造类,而 Decorator 模式重在不生成子类即可给对象添加职责。 Decorator 模式重在修饰, 而Composite 模式重在表示。

Last Updated:
Contributors: klc407073648
Prev
结构型模式 - 桥接(Bridge)
Next
结构型模式 - 装饰(Decorator)