组合模式

定义

将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式是的用户对单个对象和组合对象的使用具有一致性。

动机

当你发现需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一的使用组合对象结构中的所有对象时,就应该考虑用组合模式了。

UML类图

场景拆解

以一个大型公司为需求背景,组织我们的代码。一个北京总公司,下面有郑州和西安两个分公司。然后每个公司不管是总公司还是分公司都有自己的人力资源部,IT部和市场部。

源码实现

  • component.h
#ifndef COMPONENT_H
#define COMPONENT_H
#include <QList> class Component
{
public:
Component(QString name);
virtual ~Component();
virtual void add(Component* com, int depth = 0) = 0;
virtual void remove(Component* com) = 0;
virtual void display() = 0;
virtual void LineOfDuty() = 0; QString name();
void setDepth(int depth);
int depth();
private:
QString m_Name;
int m_Depth;
}; class ConcreteCompany : public Component
{
public:
ConcreteCompany(QString name);
virtual ~ConcreteCompany();
virtual void add(Component* com, int depth = 0);
virtual void remove(Component* com);
virtual void display();
virtual void LineOfDuty();
private:
QList<Component*> m_ChildCom;
}; class HumanResource : public Component
{
public:
HumanResource(QString name);
virtual ~HumanResource();
virtual void add(Component* com, int depth = 0);
virtual void remove(Component* com);
virtual void display();
virtual void LineOfDuty();
}; class IT : public Component
{
public:
IT(QString name);
virtual ~IT();
virtual void add(Component* com, int depth = 0);
virtual void remove(Component* com);
virtual void display();
virtual void LineOfDuty();
}; class Marketing : public Component
{
public:
Marketing(QString name);
virtual ~Marketing();
virtual void add(Component* com, int depth = 0);
virtual void remove(Component* com);
virtual void display();
virtual void LineOfDuty();
};
#endif // COMPONENT_H
  • component.cpp
/************************************
* @brief : 安排一下故事背景:有一个王者农药全国总公司在深圳,现在想要在全国开办事处
* 1.北京办事处- 招聘部,研发部,市场部
* 2.郑州办事处- 招聘部,研发部,市场部
* 3.西安办事处- 招聘部,研发部,市场部
* and so on...
* @author : wzx
* @date : 2020-05-11
* @project : Composite
*************************************/
#include <QDebug>
#include "component.h" #define DELETEOBJECT(x) if(x) { delete x; x = nullptr; } Component::Component(QString name):m_Name(name) {} Component::~Component(){} QString Component::name()
{
return m_Name;
} void Component::setDepth(int depth)
{
m_Depth = depth;
} int Component::depth()
{
return m_Depth;
} ConcreteCompany::ConcreteCompany(QString name)
: Component(name)
{ } ConcreteCompany::~ConcreteCompany()
{
for(auto com : m_ChildCom)
{
DELETEOBJECT(com);
}
m_ChildCom.clear();
} void ConcreteCompany::add(Component* com, int depth)
{
com->setDepth(depth);
m_ChildCom.append(com);
} void ConcreteCompany:: remove(Component* com)
{
m_ChildCom.removeOne(com);
} void ConcreteCompany::display()
{
QString str;
for(int n = 0; n < depth(); ++n)
str += "--";
qDebug() << qPrintable(str) << (name());
for(auto com : m_ChildCom)
{
com->display();
}
} void ConcreteCompany::LineOfDuty()
{ } HumanResource::HumanResource(QString name)
: Component(name)
{ } HumanResource::~HumanResource()
{ } void HumanResource::add(Component* com, int depth)
{
com->setDepth(depth);
} void HumanResource::remove(Component* com)
{ } void HumanResource::display()
{
QString str;
for(int n = 0; n < depth(); ++n)
str += "--";
qDebug() << qPrintable(str) << (name());
} void HumanResource::LineOfDuty()
{
qDebug() << "人力资源部,负责招聘";
} IT::IT(QString name)
: Component(name)
{ } IT::~IT()
{ } void IT::add(Component* com, int depth)
{
com->setDepth(depth);
} void IT::remove(Component* com)
{ } void IT::display()
{
QString str;
for(int n = 0; n < depth(); ++n)
str += "--";
qDebug() << qPrintable(str) << (name());
} void IT::LineOfDuty()
{
qDebug() << "IT部门,负责写代码";
} Marketing::Marketing(QString name)
: Component(name)
{ } Marketing::~Marketing()
{ } void Marketing::add(Component* com, int depth)
{
com->setDepth(depth);
} void Marketing::remove(Component* com)
{ } void Marketing::display()
{
QString str;
for(int n = 0; n < depth(); ++n)
str += "--";
qDebug() << qPrintable(str) << name();
} void Marketing::LineOfDuty()
{
qDebug() << "市场部门,负责市场推广";
}
  • main.cpp
#include <QCoreApplication>
#include <QDebug>
#include "component.h" int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); Component* root = new ConcreteCompany("北京总部");
root->setDepth(0);
root->add(new HumanResource("人力资源部门"), 1);
root->add(new IT("IT部门"), 1);
root->add(new Marketing("市场部门"), 1); Component* zz = new ConcreteCompany("郑州办事处");
zz->add(new HumanResource("人力资源部门"), 2);
zz->add(new IT("IT部门"), 2);
zz->add(new Marketing("市场部门"), 2);
root->add(zz, 1); Component* xa = new ConcreteCompany("西安办事处");
xa->add(new HumanResource("人力资源部门"), 2);
xa->add(new IT("IT部门"), 2);
xa->add(new Marketing("市场部门"), 2);
root->add(xa, 1); root->display();
return a.exec();
}
  • 运行结果

"北京总部"

-- "人力资源部门"

-- "IT部门"

-- "市场部门"

-- "郑州办事处"

---- "人力资源部门"

---- "IT部门"

---- "市场部门"

-- "西安办事处"

---- "人力资源部门"

---- "IT部门"

---- "市场部门"

优点

组合模式定义了包含基本对象和组合对象的类层次结构。基本对象可以组合成更复杂的组合对象,而这个组合对象又可以被组合,这样不断的递归下去,客户代码中,任何用到基本对象的地方都可以使用组合对象了。

缺点

参考《大化设计模式》

组合模式(c++实现)的更多相关文章

  1. ComponentPattern (组合模式)

    import java.util.LinkedList; /** * 组合模式 * * @author TMAC-J 主要用于树状结构,用于部分和整体区别无区别的场景 想象一下,假设有一批连锁的理发店 ...

  2. 设计模式(十一):从文Finder中认识"组合模式"(Composite Pattern)

    上一篇博客中我们从从电影院中认识了"迭代器模式"(Iterator Pattern),今天我们就从文件系统中来认识一下“组合模式”(Composite Pattern).说到组合模 ...

  3. 设计模式(十)组合模式(Composite Pattern)

    一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...

  4. 设计模式--组合模式Composite(结构型)

    一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...

  5. 组合模式/composite模式/对象结构型模式

    组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...

  6. c#设计模式-组合模式

    在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象和复合对象 ...

  7. C#设计模式系列:组合模式(Composite)

    1.组合模式简介 1.1>.定义 组合模式主要用来处理一类具有“容器特征”的对象——即它们在充当对象的同时,又可以作为容器包含其他多个对象. 1.2>.使用频率 中高 2.组合模式结构图 ...

  8. php实现设计模式之 组合模式

    <?php /** * 组合模式 * * 将对象组合成树形结构以表示"部分-整体"的层次结构,使得客户对单个对象和复合对象的使用具有一致性 * * * 1) 抽象构件角色Co ...

  9. 轻松掌握:JavaScript组合模式

    组合模式 组合模式:将一组对象组合成树形结构,并统一对待组合对象和叶对象,忽略它们之间的不同(因为叶对象也可以也可以包含叶对象而成为组合对象),组合模式中的对象只能是一对多的关系,不能出现多对一. 基 ...

  10. java设计模式之组合模式

    组合模式 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.掌握组合模式的重点是要理解清楚 “部分/整体” 还有 ”单个对象“ 与 & ...

随机推荐

  1. 学习JVM参数前必须了解的

    JVM参数是什么 大家照相通常使用手机就够用了,但是针对发烧友来说会使用更专业的设备,比如单反相机,在单反里有好几个模式,P/A/S/M,其中P是傻瓜模式,程序会自动根据环境设置快门速度和光圈大小,以 ...

  2. Flask接口开发过程中的心得2019.10.03

    完善了一下慕课网实战中的post接口开发,得到了一些进步: 代码如下: #coding=utf-8 from flask import Flask from flask import request ...

  3. Python 代码编辑器怎么选?PyCharm、VS Code、Jupyter Notebook 都各有特色

    Python 代码编辑器怎么选?PyCharm.VS Code.Jupyter Notebook 都各有特色,Jupyter 适合做数据分析这些需要可视化的操作,PyCharm 更适合做完整的 Pyt ...

  4. 微信小程序标签常见知识点归纳整理

    1. <image src='/images/logo.png' mode='widthFix'></image> mode 属性表示图片随着指定的宽度自动拉伸以显示原图的正确 ...

  5. pytorch中CUDA类型的转换

    import torch import numpy as np device = torch.device("cuda:0" if torch.cuda.is_available( ...

  6. Jenkins(1)- centos7.X下安装Jenkins

    如果想从头学起Jenkins的话,可以看看这一系列的文章哦 https://www.cnblogs.com/poloyy/category/1645399.html 下载安装包 wget http:/ ...

  7. Jmeter:运行报:Error occurred starting thread group :线程组, error message:Invalid duration 0 set in Thread Group:线程组, see log file for more details

    最近在用jmeter做压测,上周五压测的脚本,今天早晨结束后. 点击同样的脚本,运行就报Error occurred starting thread group :线程组, error message ...

  8. 2019-2020-1 20199303《Linux内核原理与分析》第五周作业

    系统调用的三层机制 API:第一层是指Libc中定义的API,这些API封装了系统调用,使用int 0x80触发一个系统调用中断:当然,并非所有的API都使用了系统调用,如完成数学加减运算的API就没 ...

  9. char * 转 wchar *

    ) ) https://github.com/ffftp/ffftp/commit/0b1ea7a23dc6c72d519edbdeb30f48b3c7c8e867

  10. Python与Excel交互——Xlwings实战

    这一期直接来实战. 比如说,我们在一个快递网站上爬取了几个快递的轨迹信息,我们需要将数据保存下来,一个常规做法是把数据保存在数据库里(Mysql,MongoDB,Redis),另一个是用Excel的形 ...