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的更多相关文章

  1. 说说设计模式~大话目录(Design Pattern)

    回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...

  2. 设计模式(Design Pattern)系列之.NET专题

    最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...

  3. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  4. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  5. 为什么要提倡“Design Pattern呢

    为什么要提倡“Design Pattern呢?根本原因是为了代码复用,增加可维护性. 那么怎么才能实现代码复用呢?面向对象有几个原则:开闭原则(Open Closed Principle,OCP).里 ...

  6. 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 ...

  7. 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 ...

  8. java设计模式大全 Design pattern samples in Java(最经典最全的资料)

    java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...

  9. 为什么要提倡"Design Pattern"呢? 开闭原则 系统设计时,注意对扩展开放,对修改闭合。

    [亲身经历] 无规矩不成方圆 设计模式 - 搜狗百科 https://baike.sogou.com/v123729.htm?fromTitle=设计模式 为什么要提倡"Design Pat ...

随机推荐

  1. 2017第八届蓝桥杯决赛(C++ B组)4.发现环

    描述 小明的实验室有N台电脑,编号1~N.原本这N台电脑之间有N-1条数据链接相连,恰好构成一个树形网络.在树形网络上,任意两台电脑之间有唯一的路径相连. 不过在最近一次维护网络时,管理员误操作使得某 ...

  2. pf4j实例 插件框架

    实现整个过程需要三个部分,第一就是根接口,第二是插件,第三是应用程序.这是3个java项目. 首先要下载jar包,百度搜索maven repository,然后搜索pf4j,如下图,下载第一个的相应版 ...

  3. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall

    题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在 ...

  4. react常用开发工具

    https://www.cnblogs.com/vipstone/p/7125338.html

  5. postgresql 如何设置主键自增

    法一: CREATE TABLE customers ( customerid SERIAL primary key , companyname character varying, contactn ...

  6. freemarker ! 用法

    ${(user.name)!""} 请注意,是打了()的 也就是它会先判断user是不是为null 在判断user.name 是不是为null

  7. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_命名空间和程序集的关系

    命名空间和程序集不一定相关 1. 同一个命名空间中的各个类型可能是在不同的程序集中实现的.(System.IO.FileStream在MSCorLib.dll程序集中,而System.IO.FileS ...

  8. 网络流之最大流与最小费用流入门&&模板

    理解处 刷题处 模板处 最大流模板 处理重边的+(优化) #include<bits/stdc++.h> using namespace std; ; const int INF = 0x ...

  9. python基础之1--Python入门

    第1章 Python生态圈 第2章 编程与编程语言 python是一门编程语言,作为学习python的开始,需要事先搞明白:编程的目的是什么?什么是编程语言?什么是编程? 2.1 编程的目的: 计算机 ...

  10. jenkins插件之火线扫描(静态代码扫描)

    参考网址: https://blog.csdn.net/oggboy/article/details/78646622