定义

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

组合模式(Composite)将小对象组合成树形结构,使用户操作组合对象如同操作一个单个对象。组合模式定义了“部分-整体”的层次结构,基本对象可以被组合成更大的对象,而且这种操作是可重复的,不断重复下去就可以得到一个非常大的组合对象,但这些组合对象与基本对象拥有相同的接口,因而组合是透明的,用法完全一致。

代码:

#include <iostream>
#include <list>
#include <string>
using namespace std; class IFile
{
public:
virtual void display() = ;
virtual int add(IFile *ifile) = ;
virtual int remove(IFile *ifile) = ;
virtual list<IFile*> *getChild() = ;
}; class File : public IFile
{
public:
File(string name) :_name(name){}
virtual void display()
{
cout << _name << endl;
} virtual int add(IFile *ifile)
{
return -;
} virtual int remove(IFile *ifile)
{
return -;
} virtual list<IFile*>* getChild()
{
return NULL;
} private:
string _name;
}; class Dir : public IFile
{
public:
Dir(string name)
{
_name = name;
_list.clear();
} virtual void display()
{
cout << _name << endl;
} virtual int add(IFile *ifile)
{
_list.push_back(ifile);
return ;
} virtual int remove(IFile *ifile)
{
_list.remove(ifile);
return ;
} virtual list<IFile*> *getChild()
{
return &_list;
} private:
string _name;
list<IFile*> _list;
}; void showTree(IFile *root, int level)
{
if (root == NULL || level < )
return; for (int i = ; i < level; ++i)
{
cout << "\t";
} // 显示根节点
root->display(); // 判断是否为目录
// 如果不是目录,为文件(返回值为NULL),直接返回
//否则遍历目录
list<IFile*> *file_list = root->getChild();
if (file_list == NULL)
return; for (auto it = file_list->begin(); it != file_list->end(); ++it)
{
// 普通文件
if ((*it)->getChild() == NULL)
{
for (int i = ; i <= level; ++i)
{
cout << "\t";
}
// 显示普通文件
(*it)->display();
}
else //目录
{
// 递归 显示该目录下的文件
showTree(*it, level + );
}
}
} void test()
{
IFile *root = new Dir("/");
IFile *aa = new Dir("aa");
IFile *bb = new Dir("bb");
IFile *cc = new Dir("cc");
IFile *dd = new File("dd");
IFile *ee = new File("ee");
IFile *ff = new File("ff");
IFile *gg = new File("gg");
IFile *hh = new Dir("hh");
cc->add(gg);
cc->add(hh); aa->add(cc);
aa->add(dd);
bb->add(ee);
bb->add(ff);
root->add(aa);
root->add(bb); showTree(root, ); } int main()
{
test();
cin.get();
return ;
}

效果:

组合(composite)模式的更多相关文章

  1. Java 实现组合(Composite)模式

    类图 /** * 树 总体 * * @author stone * */ public class Tree { private TreeNode root; //根节点 public Tree(St ...

  2. C++设计模式实现--组合(Composite)模式

    一. 举例 这个样例是书上的,如果有一个公司的组结结构例如以下: 它的结构非常像一棵树,当中人力资源部和財务部是没有子结点的,详细公司才有子结点. 并且最关健的是,它的每一层结构非常相似. 代码实现例 ...

  3. 设计模式C++描述----11.组合(Composite)模式

    一. 举例 这个例子是书上的,假设有一个公司的组结结构如下: 它的结构很像一棵树,其中人力资源部和财务部是没有子结点的,具体公司才有子结点. 而且最关健的是,它的每一层结构很相似. 代码实现如下: / ...

  4. 八、结构模式之组合(Composite)模式

    组合模式属于对象的结构模式,有时又叫做部分-整体模式,组合模式将对象组织到树结构中,可以用来描述整体与部分的联系.其可以使客户端将单纯元素和组合元素同等对待. 当需求中是体现部分与整体层次的结构时,以 ...

  5. Head First 设计模式 —— 11. 组合 (Composite) 模式

    思考题 我们不仅仅要支持多个菜单,升值还要支持菜单中的菜单.你如何处理这个新的设计需求? P355 [提示]在我们的新设计中,真正需要以下三点: P354 我们需要某种属性结构,可以容纳菜单.子菜单和 ...

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

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

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

    1.出现原因 1.在面向对象系统中,我们常会遇到一类具有“容器”特征的对象——即它们在充当对象的同时,又是其他对象的容器. 如何将“客户代码与复杂的对象容器结构”解耦(将这种组合容器对象设计成树形结构 ...

  8. Java设计模式(8)组合模式(Composite模式)

    Composite定义:将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性. Composite比较容易理解,想到Composite就应该想到树 ...

  9. Composite模式 组合模式

    Android的ViewGroup 和 View 的关系,即是采用组合模式 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件 ...

随机推荐

  1. django JsonResponse和HttpResponse的在后端和前端区别

    JsonResponse和HttpResponse的区别 1.from django.http import JsonResponse return JsonResponse('例子') 2.impo ...

  2. 【次小生成树】【Kruskal】【prim】【转】

    原博客出处:https://blog.csdn.net/yasola/article/details/74276255 通常次小生成树是使用Prim算法进行实现的,因为可以在Prim算法松弛的同时求得 ...

  3. Tomcat端口被占用解决办法

    1.在cmd运行窗口中输入netstat -ano 查看当前被占用的端口所对应的进城PID: 2.输入命令tasklist,找到进程号PID对应的进程名称(映像名称) 3.打开任务管理器(ctrl+a ...

  4. ios 上浏览器返回上一页不会刷新页面问题,页面初始化的方法不执行

    https://blog.csdn.net/yang450712123/article/details/79276102 https://blog.csdn.net/Chengbin_Huang/ar ...

  5. vue全家桶+Koa2开发笔记(5)--nuxt

    1. nuxt项目初始化报错 下面是使用 koa 模板方法初始化一个项目,使用该方法需要将 nuxt 的版本降至1.4.2: 官方 https://zh.nuxtjs.org/guide/instal ...

  6. pipenv 方便的python 开发工作流工具

    pipenv 将 composer.bundler.npm.yarn.cargo 等比较方便的包管理工具添加到了python 语言中,可以 帮助我们自动的管理virtualenv ,同时可以方便的从p ...

  7. Gravitational Teleport docker-compose组件独立部署运行

    Gravitational Teleport 可以作为堡垒机进行使用,上次写过一个all in one 的,这次参考官方 的配置运行一个proxy node auth 分离的应用 备注: 基于dock ...

  8. 关于margin padding

    margin padding设置百分比是以父元素的宽度作参考. 定位的left,top等取百分比 则以参照定位元素的padding+width做参考 margin 四个同时设定 以margin-lef ...

  9. SqlDependency和SqlCacheDependency缓存的用法及具体步骤

    SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决什么问题? Asp.Net中的cac ...

  10. Oracle11g 密码延迟认证导致library cache lock的情况分析

    在 Oracle 11g 中,为了提升安全性,Oracle 引入了『密码延迟验证』的新特性.这个特性的作用是,如果用户输入了错误的密码尝试登录,那么随着登录错误次数的增加,每次登录前验证的时间也会增加 ...