Composite Pattern

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

2.Composite 模式结构图

3.实现

 1 #ifndef _COMPONENT_H_
 2 #define _COMPONENT_H_
 3
 4 class Component
 5 {
 6 public:
 7     Component();
 8     virtual ~Component();
 9 public:
10     virtual void Operation() = 0;
11     virtual void Add(const Component& );
12     virtual void Remove(const Component& );
13     virtual Component* GetChild(int );
14 protected:
15 private:
16 };
17
18 #endif

Component.h

 1 #include "Component.h"
 2
 3 Component::Component()
 4 {
 5
 6 }
 7 Component::~Component()
 8 {
 9
10 }
11 void Component::Add(const Component& com)
12 {
13
14 }
15 Component* Component::GetChild(int index)
16 {
17     return 0;
18 }
19 void Component::Remove(const Component& com)
20 {
21
22 }

Component.cpp

 1 #ifndef _COMPOSITE_H_
 2 #define _COMPOSITE_H_
 3
 4 #include "Component.h"
 5 #include <vector>
 6 using namespace std;
 7
 8 class Composite:public Component
 9 {
10 public:
11     Composite();
12     ~Composite();
13 public:
14     void Operation();
15     void Add(Component* com);
16     void Remove(Component* com);
17     Component* GetChild(int index);
18 protected:
19 private:
20     vector<Component*> comVec;
21 };
22
23 #endif

Composite.h

 1 #include "Composite.h"
 2 #include "Component.h"
 3 #define NULL 0 //define NULL POINTOR
 4
 5 Composite::Composite()
 6 { //vector<Component*>::iterator itend = comVec.begin();
 7 }
 8 Composite::~Composite()
 9 {
10
11 }
12 void Composite::Operation()
13 {
14     vector<Component*>::iterator comIter = comVec.begin();
15     for (;comIter != comVec.end();comIter++)
16     {
17         (*comIter)->Operation();
18     }
19 }
20 void Composite::Add(Component* com)
21 {
22     comVec.push_back(com);
23 }
24 void Composite::Remove(Component* com)
25 {
26     comVec.erase(&com);
27 }
28 Component* Composite::GetChild(int index)
29 {
30     return comVec[index];
31 }

Composite.cpp

 1 #ifndef _LEAF_H_
 2 #define _LEAF_H_
 3
 4 #include "Component.h"
 5
 6 class Leaf:public Component
 7 {
 8 public:
 9     Leaf();
10     ~Leaf();
11 void Operation();
12 protected:
13 private:
14 };
15
16 #endif

Leaf.h

 1 #include "Leaf.h"
 2 #include <iostream>
 3
 4 using namespace std;
 5
 6 Leaf::Leaf()
 7 {
 8
 9 }
10 Leaf::~Leaf()
11 {
12
13 }
14 void Leaf::Operation()
15 {
16     cout<<"Leaf operation....."<<endl;
17 }

Leaf.cpp

 1 #include "Component.h"
 2 #include "Composite.h"
 3 #include "Leaf.h"
 4 #include <iostream>
 5
 6 using namespace std;
 7
 8 int main(int argc,char* argv[])
 9 {
10     Leaf* l = new Leaf();
11     l->Operation();
12     Composite* com = new Composite();
13     com->Add(l);
14     com->Operation();
15     Component* ll = com->GetChild(0);
16     ll->Operation();
17
18     return 0;
19 }

main.cpp

时间: 2024-08-02 17:11:32

Composite Pattern的相关文章

DesignPattern_Java:Composite Pattern

组合模式 Composite Pattern 合成模式 compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly. 将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性. 抽象构件角色(

设计模式之八:组合模式(Composite Pattern)

什么是组合模式呢?简单来说组合模式就是将对象合成树形结构以表示"部分整体"的层次结构,组合模式使用户对单个对象和组合对象使用具有一致性. 组合模式(Composite Pattern)有时候又叫部分-整体模式,它使我们在树型结构的问题中,模糊了简单元素和负责元素的概念,客户程序可以向处理简单元素一样处理负责元素,从而使得客户程序与复杂元素的的内部结构解耦. 组合模式让你可以优化处理递归或分级数据结构.关于分级数据结构的一个经典例子就是电脑中的文件系统.文件系统由目录和文件组成,所有目录

设计模式 - 组合模式(composite pattern) 详解

组合模式(composite pattern) 详解 本文地址: http://blog.csdn.net/caroline_wendy 组合模式: 允许你将对象组合成树形结构来表现"整体/部分"层次结构. 组合能让客户以一致的方法处理个别对象以及组合对象. 建立组件类(Component), 组合类(composite)和叶子类(leaf)继承组件类, 客户类(client)直接调用最顶层的组合类(composite)即可. 具体方法: 1. 组件类(component), 包含组合

设计模式 - 组合模式(composite pattern) 迭代器(iterator) 详解

组合模式(composite pattern) 迭代器(iterator) 详解 本文地址: http://blog.csdn.net/caroline_wendy 参考组合模式(composite pattern): http://blog.csdn.net/caroline_wendy/article/details/36895627 在组合模式(composite pattern)添加迭代器功能, 遍历每一个组合(composite)的项. 具体方法: 1. 抽象组件类(abstract

第 15 章 组合模式【Composite Pattern】

以下内容出自:<<24种设计模式介绍与6大设计原则>> 大家在上学的时候应该都学过“数据结构”这门课程吧,还记得其中有一节叫“二叉树”吧,我们上 学那会儿这一章节是必考内容,左子树,右子树,什么先序遍历后序遍历什么,重点就是二叉树的的遍历,我还记得当时老师就说,考试的时候一定有二叉树的构建和遍历,现在想起来还是觉的老师是正确的,树状结果在实际项目应用的非常广泛. 咱就先说个最常见的例子,公司的人事管理就是一个典型的树状结构,你想想你公司的结构是不是这样: 老大,往下一层一层的管理,

第9章 组合模式(Composite Pattern)

原文 第9章 组合模式(Composite Pattern) 概述: 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦. 将对象组合成树形结构以表示"部分-整体"的层次结构.Composite模式使得用户对单个对象和组合对象的使用具有一致性.[GOF <设计模式>] 结构图: 举例: 假设我们公司有做个打卡的程序,能满足各种打卡的需要,比如整个

NET设计模式 第二部分 结构性模式(10):组合模式(Composite Pattern)

组合模式(Composite Pattern) ——.NET设计模式系列之十一 Terrylee,2006年3月 概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦. 意图 将对象组合成树形结构以表示“部分-整体”的层次结构.Composite模式使得用户对单个对象和组合对象的使用具有一致性.[GOF <设计模式>] 结构图 图1 Composite模式结构图

设计模式之组合模式---Composite Pattern

模式的定义 组合模式(Composite Pattern)定义如下: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. 将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性.

C#设计模式之九组合模式(Composite Pattern)【结构型】

原文:C#设计模式之九组合模式(Composite Pattern)[结构型] 一.引言 今天我们要讲[结构型]设计模式的第四个模式,该模式是[组合模式],英文名称是:Composite Pattern.当我们谈到这个模式的时候,有一个物件和这个模式很像,也符合这个模式要表达的意思,那就是"俄罗斯套娃"."俄罗斯套娃"就是大的瓷器娃娃里面装着一个小的瓷器娃娃,小的瓷器娃娃里面再装着更小的瓷器娃娃,直到最后一个不能再装更小的瓷器娃娃的那个瓷器娃娃为止(有点绕,下面我会

C#设计模式:组合模式(Composite Pattern)

一,C#设计模式:组合模式(Composite Pattern) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _9.组合模式 { //组合模式主要用来处理一类具有"容器特征"的对象--即它们在充当对象的同时,又可以作为容器包含其他多个对象. //组合模式,将对象组合成树形结构以表示