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 ...
随机推荐
- [SDOI2009]HH的项链 树状数组 BZOJ 1878
题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...
- 直播点赞,上升的动画-- CAKeyFrameAnimation
// // ViewController.m // DMHeartFlyAnimation // // Created by Rick on 16/3/9. // Copyright © 20 ...
- shiro 的简单应用
shiro 的简单应用 shiro官网:https://shiro.apache.org/ shiro 简介: Apache Shiro(日语"堡垒(Castle)"的意思)是 ...
- import与from...import...的区别
from ... import ... 的用法和直接import的区别: 1.直接使用import时,如果需要使用到导入模块内的属性和方法,必须使用模块名.属性和模块名.方法的方式进行调用 2.使用f ...
- 【ABP开发】:asp.net core 中使用mysql
EntityFrameworkCore项目--Nuget包管理,卸载包: Microsoft.EntityFrameworkCore.SqlServer: EntityFrameworkCore项目和 ...
- Web项目和Windows应用程序的配置文件
1.Web项目,配置文件应创建在Web项目下,即使是要把配置文件作为一个单独的文件进行配置(比如log4net.config),也需要把该配置文件放在Web项目下:同理Windows应用程序的化,配置 ...
- c++中enum 如何使用(转)
ENUM概况 enum枚举类型是C/C++中的一种数据类型,与struct和class一样是用户自定义的类型,其特点在于enum类型的变量取值是有限的,是可以一一列举出来的. ENUM定义 C++ e ...
- acm之奇葩数据输入专题
1.每組测试数据都在一行,不知道每组测试数据的长度,以换行为测试数据输入的结束 关键代码:if (cin.get() == '\n') {语句} 例如:找出每组测试的最大值: 输入: 3 1 4 ...
- 【ACM】蛇形填数 - 逻辑怪
蛇形填数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为:10 11 12 19 16 1 ...
- 使用bootstrap创建上传文件
1.导入样式,注意顺序 <!-- bootstrap样式 --> <link rel="stylesheet" href="/static/bootst ...