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

需求中是体现部分与整体层次的结构时,希望用户可以忽略组合对象和单个对象的不同,统一地使用组合结构中的所有对象时,就可以用组合模式。

实例:

company.h company.cpp

#ifndef COMPANY_H
#define COMPANY_H #include <QString> class Company
{
public:
Company(QString name);
void virtual add(Company *c)=0;
void virtual remove(Company *c)=0;
void virtual display(QString depth)=0;
void virtual lineOfDuty()=0; protected:
QString name;
}; #endif // COMPANY_H
#include "company.h"

Company::Company(QString name)
{
this->name = name;
}

concretecompany.h concretecompany.cpp

#ifndef CONCRETECOMPANY_H
#define CONCRETECOMPANY_H #include "company.h"
#include <QString>
#include <QList> class ConcreteCompany : public Company
{
public:
ConcreteCompany(QString name);
void add(Company *c);
void remove(Company *c);
void display(QString depth);
void lineOfDuty(); protected:
QList<Company *> *children;
}; #endif // CONCRETECOMPANY_H
#include "concretecompany.h"
#include <QDebug> ConcreteCompany::ConcreteCompany(QString name) : Company(name)
{
children = new QList<Company *>();
} void ConcreteCompany::add(Company *c)
{
children->push_back(c);
} void ConcreteCompany::remove(Company *c)
{
children->removeOne(c);
} void ConcreteCompany::display(QString depth)
{
qDebug() << depth << name;
for(int i=0; i!=children->count(); i++)
{
children->at(i)->display( depth + "--");
}
} void ConcreteCompany::lineOfDuty()
{
for(int i=0; i!=children->count(); i++)
{
children->at(i)->lineOfDuty();
}
}

financedepartment.h financedepartment.cpp 财务

#ifndef FINANCEDEPARTMENT_H
#define FINANCEDEPARTMENT_H #include "company.h"
#include <QString> class FinanceDepartment : public Company
{
public:
FinanceDepartment(QString name);
void add(Company *c);
void remove(Company *c);
void display(QString depth);
void lineOfDuty();
}; #endif // FINANCEDEPARTMENT_H
#include "financedepartment.h"
#include <QDebug> FinanceDepartment::FinanceDepartment(QString name) : Company(name)
{
} void FinanceDepartment::add(Company *c)
{} void FinanceDepartment::remove(Company *c)
{} void FinanceDepartment::display(QString depth)
{
qDebug() << depth << name;
} void FinanceDepartment::lineOfDuty()
{
qDebug() << "lineOfDuty, money";
}

hrdepartment.h hrdepartment.cpp    HR

#ifndef HRDEPARTMENT_H
#define HRDEPARTMENT_H #include "company.h"
#include <QString> class HRDepartment : public Company
{
public:
HRDepartment(QString name);
void add(Company *c);
void remove(Company *c);
void display(QString depth);
void lineOfDuty();
}; #endif // HRDEPARTMENT_H
#include "hrdepartment.h"
#include <QDebug> HRDepartment::HRDepartment(QString name) : Company(name)
{
} void HRDepartment::add(Company *c)
{} void HRDepartment::remove(Company *c)
{} void HRDepartment::display(QString depth)
{
qDebug() << depth << name;
} void HRDepartment::lineOfDuty()
{
qDebug() << "lineOfDuty, HR";
}

main.cpp

#include <QCoreApplication>
#include "concretecompany.h"
#include "hrdepartment.h"
#include "financedepartment.h" int main(int argc, char *argv[])
{
ConcreteCompany *root = new ConcreteCompany("beijing_company");
root->add(new HRDepartment("beijing_HR"));
root->add(new FinanceDepartment("beijing_money")); ConcreteCompany *sub = new ConcreteCompany("hangzhou_company");
sub->add(new HRDepartment("hangzhou_HR"));
sub->add(new FinanceDepartment("hangzhou_money"));
root->add(sub); root->display("--");
}

大话设计模式--组合模式 Composite -- C++实现实例的更多相关文章

  1. 浅谈设计模式--组合模式(Composite Pattern)

    组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...

  2. 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释

    组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...

  3. 设计模式组合模式(Composite)精华

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,他们帮助如何创建一个系统独立.这是一个这些对象和陈述的组合. 创建使用继承类的类架构更改实例.的对象类型模型的建 ...

  4. 设计模式 -- 组合模式 (Composite Pattern)

    定义: 对象组合成部分整体结构,单个对象和组合对象具有一致性. 看了下大概结构就是集团总公司和子公司那种层级结构. 角色介绍: Component :抽象根节点:其实相当去总公司,抽象子类共有的方法: ...

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

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

  6. C#设计模式——组合模式(Composite Pattern)

    一.概述 在软件开发中,我们往往会遇上类似树形结构的对象体系.即某一对象既可能在树形结构中作为叶节点存在,也可能作为分支节点存在.比如在文件系统中,文件是作为叶节点存在,而文件夹就是分支节点.在设计这 ...

  7. 设计模式-组合模式(Composite)

    一.概念 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.模式动机 组合模式,通过设计一个抽像的组件类,使它既代表叶子对象,又代表组合对 ...

  8. 大话设计模式--工厂模式 factory -- C++实现实例

    实现<大话设计模式>的C++版本... 1. 工厂模式 使用的范围是 同一个基类,下面很多子类. (1)这里很容易出现的一个问题n多的子类继承自抽象基类,我们不得不在每次要用到子类的地方就 ...

  9. 说说设计模式~组合模式(Composite)

    返回目录 何时能用到它? 组合模式又叫部分-整体模式,在树型结构中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦.对于今天这个 ...

随机推荐

  1. lua math 库

    lua math库 (2012-05-18 17:26:28) 转载▼ 标签: 游戏 分类: Lua atan2.sinh.cosh.tanh这4个应该用不到. 函数名 描述 示例 结果 pi 圆周率 ...

  2. centos6搭建docker镜像私服

    1.创建私服容器 docker run -d -e SETTINGS_FLAVOR=dev -e STORAGE_PATH=/tmp/registry -v /opt/data/registry:/t ...

  3. redis+node.js

    1.什么的cache 是一种更快的记忆存储数据集 存储空间有限 储存一部分重要数据 是一种相对的概念,只要比原本数据存储更快的介质就能作为cache 2.caching 策略 有限的存储空间,只能存储 ...

  4. zendstudio的安装和破解

    参考博客地址:http://www.oxox.work/web/php-basic/zendstudio/ 注明:还未验证

  5. 忘记glassfish密码,那就重置密码呗

    方法一:如果现有的 domain 上并没有你所需要的东西,删除现有的 domain,重新创建一个 domain. 找到安装glassfish的目录下的 \bin\asadmin 目录,然后打开asad ...

  6. codevs 必做:2776、1222

    2776 寻找代表元  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 广州二中苏元实验学校一共有n个社团,分 ...

  7. spring配置加载2次实例问题。

    WEB.XML 中SPRING 配置及重复加载问题 Posted on 2012-11-13, 15:48, by tmser, under java 周边 . 项目内存溢出,mat 查看了一下发现s ...

  8. Map集合按value的大小排序

    public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Int ...

  9. python函数的作用域

    python函数的作用域 以下内容参考自runoob网站,以总结python函数知识点,巩固基础知识,特此鸣谢! 原文地址:http://www.runoob.com/python3/python3- ...

  10. NSURLSession各文件关系

    NSURLSession   通过session创建任务 @property (class, readonly, strong) NSURLSession *sharedSession; + (NSU ...