Design Pattern ->Composite
Layering & Contract Philosophy With additional indirection

class CComponent
{
public: virtual void Operation() = ;
public: virtual void AddComponent(Component* p)= ;
public: virtual void RemoveComponent(Component* p)= ;
public: virtual Component* GetChild(int i) { return NULL;}
}
class CComposite: public CComponent
{
public: virtual void Operation()
{ foreach ( i ) GetChild(i)->Opeartion(); };
public: virtual void AddComponent(Component* p)
{ v.add( p );};
public: virtual void RemoveComponent(Component* p)
{ v.remove(p);}
public: virtual Component* GetChild(int i)
{ return v[i];}
private: vector< CComponent* > v;
}
class CLeaf: public CCompoent
{
public: virtual void Operation()
{ do_something_for_leaf(); };
}
class Client
{
CLeaf* pleaf1 = new CLeaf; CLeaf* pleaf2 = new CLeaf;
CLeaf* pleaf3 = new CLeaf; CLeaf* pleaf4 = new CLeaf;
CLeaf* pleaf5 = new CLeaf; CLeaf* pleaf6 = new CLeaf;
CComposite* pcomposite1, pcomposite2 = new CComposite;
pcomposite1.addcomponent(pleaf1);
pcomposite1.addcomponent(pleaf2);
pcomposite1.addcomponent(pleaf3);
pcomposite2.addcomponent(pleaf4);
pcomposite2.addcomponent(pleaf5);
pcomposite2.addcomponent(pcomposite1);
CComposite* pAll = new CComposite;
pAll.addComponent(pcomposite1);
pAll.addComponent(pcomposite2);
pAll.addComponent(pleaf6);
pAll.Opearation();
}
Applicability
Use the Composite pattern when:
- you want to represent part-whole hierarchies of objects.
- you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.
Participants
Component (Graphic)
- declares the interface for objects in the composition.
- implements default behavior for the interface common to all classes, as appropriate.
- declares an interface for accessing and managing its child components.
- (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate.
Leaf (Rectangle, Line, Text, etc.)
- represents leaf objects in the composition. A leaf has no children.
- defines behavior for primitive objects in the composition.
Composite (Picture)
- defines behavior for components having children.
- stores child components.
- implements child-related operations in the Component interface.
Client
- manipulates objects in the composition through the Component interface.
Collaborations
- Clients use the Component class interface to interact with objects in the composite structure. If the recipient is a Leaf, then the request is handled directly. If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.
Design Pattern ->Composite的更多相关文章
- 说说设计模式~大话目录(Design Pattern)
回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...
- 设计模式(Design Pattern)系列之.NET专题
最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- 为什么要提倡“Design Pattern呢
为什么要提倡“Design Pattern呢?根本原因是为了代码复用,增加可维护性. 那么怎么才能实现代码复用呢?面向对象有几个原则:开闭原则(Open Closed Principle,OCP).里 ...
- C++ Design Pattern: What is a Design Pattern?
Q: What is a Design Pattern? A: Design Patterns represent solutions to problems what arise when deve ...
- Design Pattern in Simple Examples
Instead of defining what is design pattern lets define what we mean by design and what we mean by pa ...
- java设计模式大全 Design pattern samples in Java(最经典最全的资料)
java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...
- 为什么要提倡"Design Pattern"呢? 开闭原则 系统设计时,注意对扩展开放,对修改闭合。
[亲身经历] 无规矩不成方圆 设计模式 - 搜狗百科 https://baike.sogou.com/v123729.htm?fromTitle=设计模式 为什么要提倡"Design Pat ...
随机推荐
- 数据结构31:树(Tree)详解
复制广义表数据结构中的树 树是数据结构中比较重要也是比较难理解的一类存储结构.本章主要主要围绕二叉树,对树的存储以及遍历做详细的介绍,同时还会涉及到有关树的实际应用,例如构建哈弗曼编码等. 由于树存储 ...
- [USACO08FEB]酒店Hotel 线段树 BZOJ 1593
题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- PHPExcel类库的使用
首先下载PHPEXCEL 下载地址:https://github.com/PHPOffice/PHPExcel 一.生成Excel <?php require "PHPExcel-1. ...
- C语言的存储类别和动态内存分配
存储类别分三大类: 静态存储类别 自动存储类别 动态分配内存 变量.对象--->内存管理 内存考虑效率(时间更短.空间更小) 作用域 链接.---->空间 存储器 ----->时 ...
- Android MVP模式实现组件和业务逻辑分离
1,Activity代码展示,只需要一下3行重要代码即可完成任何复杂的逻辑 /** * 登录界面 * * @author lipanquan */public class LoginActivity ...
- 5-----Scrapy框架中Spiders用法
Spider类定义了如何爬去某个网站,包括爬取的动作以及如何从网页内容中提取结构化的数据,总的来说spider就是定义爬取的动作以及分析某个网页 工作流程分析 1.以初始的URL初始化Request, ...
- Win10通过SSH与树莓派Raspbain系统互传文件
1.在Linux系统上安装ssh-server(由于Raspbain系统自带ssh-server,这个步骤可以省略) 查看ssh是否运行的命令: ps -ef | grep ssh 如果没有安装,则安 ...
- Idea创建Maven项目没有src
第一次创建,下载非常慢,解决方法 1.配置环境变量 第二种:创建Maven项目时加上 archetypeCatalog=internal 参数 第三种:为自己的Maven配置国内镜像源 打开自己的 M ...
- ST-LINK接口定义
ST-LINKIII管脚定义及接法: ST-LINK IIILED灯三种状态含义: 常亮:目标板与ST-LINK在SWIM模式或者JTAG/SWD模式下已经通讯初始化. 闪烁:目标板与ST-L ...
- linux 安装dubbo+zookeeper
dubbo+zookeeper 启动成功 注意:dubbo-admin-2.5.4 不支持java8