Composite 模式的实现
实现要点:
1.组合模式采用树形结构来实现普遍存在的对象容器,从而将“一对多”的关系转化“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器。
2.将“客户代码与复杂的对象容器结构”解耦是组合模式的核心思想,解耦之后,客户代码将与纯粹的抽象接口——而非对象容器的复内部实现结构——发生依赖关系,从而更能“应对变化”。
3.组合模式中,是将“Add和Remove等和对象容器相关的方法”定义在“表示抽象对象的Component类”中,还是将其定义在“表示对象容器的Composite类”中,是一个关乎“透明性”和“安全性”的两难问题,需要仔细权衡。这里有可能违背面向对象的“单一职责原则”,但是对于这种特殊结构,这又是必须付出的代价。
4.组合模式在具体实现中,可以让父对象中的子对象反向追溯;如果父对象有频繁的遍历需求,可使用缓存技巧来改善效率。
5. 客户端尽量不要直接调用树叶类的方法,而是借助其父类(Component)的多态性完成调用,这样可以增加代码的复用性。
使用场景:
以下情况下适用组合模式:
1.你想表示对象的部分-整体层次结构。
2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
- /////////////////Component.h////////////////////////
- #pragma once
- class Component
- {
- public:
- virtual ~Component();
- Component();
- virtual void Operation() = ;
- virtual void Add( Component*);
- virtual void Remove( Component*);
- virtual Component* GetChild(int);
- protected:
- private:
- };
- /////////////////Component.cpp////////////////////////
- #include "Component.h"
- Component::Component()
- {
- }
- Component::~Component()
- {
- }
- void Component::Add( Component* com)
- {
- }
- void Component::Remove( Component* com)
- {
- }
- void Component::Operation()
- {
- }
- Component* Component::GetChild(int index)
- {
- return ;
- }
- /////////////////Composite.h////////////////////////
- #pragma once
- #include "Component.h"
- #include <vector>
- using namespace std;
- class Composite : public Component
- {
- public:
- Composite(const string&);
- ~Composite();
- void Operation() ;
- void Add( Component*);
- void Remove( Component*);
- Component* GetChild(int);
- protected:
- private:
- vector<Component*> comVec ;
- string _name ;
- };
- /////////////////Composite.cpp////////////////////////
- #include "Composite.h"
- #include "Component.h"
- #include <iostream>
- #include <string>
- using namespace std;
- Composite::Composite(const string& name)
- {
- _name = name ;
- }
- Composite::~Composite()
- {
- }
- void Composite::Operation()
- {
- cout<<"Composite operation : "<<_name<<endl;
- vector<Component*>::iterator iter = comVec.begin();
- for (;iter != comVec.end() ; iter++)
- {
- if (_name == "Com2")
- {
- cout<<"------";
- }
- if (_name == "Com1")
- {
- cout<<"---";
- }
- (*iter)->Operation();
- }
- }
- void Composite::Add( Component* com)
- {
- comVec.push_back(com);
- }
- void Composite::Remove( Component* com)
- {
- for (vector<Component*>::iterator it = comVec.begin() ; it != comVec.end() ;)
- {
- if (com == *it)
- {
- it = comVec.erase(it);
- }
- else
- {
- it++;
- }
- }
- }
- Component* Composite::GetChild(int index)
- {
- return comVec[index] ;
- }
- ////////////////////Leaf.h///////////////////////
- #pragma once
- #include "Component.h"
- #include <iostream>
- #include <string>
- using namespace std;
- class Leaf : public Component
- {
- public:
- Leaf(const string&);
- ~Leaf();
- void Operation();
- protected:
- private:
- string _name;
- };
- Leaf::Leaf(const string& name)
- {
- _name = name ;
- }
- Leaf::~Leaf()
- {
- }
- void Leaf::Operation()
- {
- cout<<"Leaf operation : "<< _name <<endl;
- }
- /////////////////main////////////////////////////
- #include "Component.h"
- #include "Composite.h"
- #include "Leaf.h"
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- Component* leaf1 = new Leaf("leaf1") ;
- Component* leaf2 = new Leaf("leaf2") ;
- Component* leaf3 = new Leaf("leaf3") ;
- Component* com1 = new Composite("Com1");
- com1->Add(leaf1);
- com1->Add(leaf2);
- com1->Add(leaf3);
- Component* leaf4 = new Leaf("leaf4") ;
- Component* leaf5 = new Leaf("leaf5") ;
- Component* com2 = new Composite("Com2");
- com2->Add(leaf4);
- com2->Add(leaf5);
- com1->Add(com2);
- com1->Operation();
- getchar();
- return ;
- }
Composite 模式的实现的更多相关文章
- 组合模式/composite模式/对象结构型模式
组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...
- C++基础——模拟事务 (2)Composite模式
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
- Composite模式
1 意图:将对象组成树形结构,以表示“部分——整体”的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 2 动机:同意处理图元对象和包含图元的容器对象.Composite通过 ...
- Java 实现组合(Composite)模式
类图 /** * 树 总体 * * @author stone * */ public class Tree { private TreeNode root; //根节点 public Tree(St ...
- 【结构型】Composite模式
组合模式意在将对象组合成树形结构以表示部分与整体的层次结构关系,并且用户对单个对象的操作以有对组合对象的操作都是一致的.即:组合对象 is-a 单个对象,同时又可以组合着 n 个的单个对象(甚至于其他 ...
- (原创)composite模式和bridge模式是天生的好朋友
composite模式的意图是:将对象组合成树形结构以表示“部分-整体”的层次结构.composite使得用户对单个对象和组合对象的使用具有一致性.它的类图如下: composite模式的实现分为透明 ...
- Java设计模式(8)组合模式(Composite模式)
Composite定义:将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性. Composite比较容易理解,想到Composite就应该想到树 ...
- 设计模式之——Composite模式
composite模式又叫做组合模式/复合模式. 它是一种能够使容器与内容具有一致性,创造出递归结构的模式. 示例程序是列出文件夹以及其内部文件与文件夹一览的功能: 可以由示例图看出,有一个电影文件夹 ...
- Composite模式 组合模式
Android的ViewGroup 和 View 的关系,即是采用组合模式 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件 ...
- Composite模式(组合设计模式)
Composite 设计模式? 在计算机的文件系统中,有"文件夹"的概念(在有些操作系统(Linux操作系统)中,也称为"目录").文件夹里面既可以放入文件,也 ...
随机推荐
- java---Unicode-字符转换器
实现一个字符(包括汉字)的简单互相转换: package cn.hncu.gui2; import java.awt.Button; import java.awt.Color; import jav ...
- 4种字符串匹配算法:KMP(下)
回顾:4种字符串匹配算法:BS朴素 Rabin-karp(上) 4种字符串匹配算法:有限自动机(中) 1.图解 KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R ...
- ubuntu制作usb启动盘
准备: u盘 iso镜像文件--ubuntu-12.04.2-desktop-amd64.iso 烧盘软件--unetbootin-linux-583 步骤: 格式化u盘 查看u盘信息 #mount/ ...
- hdu 4474 大整数取模+bfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...
- 真机测试---iOS证书(.p12)和描述文件(.mobileprovision)
iOS证书和描述文件: 证书类型 使用场景 开发(Development)证书和描述文件 用于开发测试,在starain中打包后可在真机环境通过Safari调试 发布(Distribution)证书和 ...
- [置顶] [MATLAB技术贴]漫谈MATLAB矩阵转置
矩阵转置是matlab最基本的操作了,但这个基本操作,也是很多初学者容易出现问题的地方.本帖通过几个实例演示matlab矩阵转置的操作. 方法一:' 运算符与 .' 运算符 >>a ...
- db2 alter table 语法
DB2 alter:add/delete/reset column 1.添加字段 alter table [table_name] add [column_name] [column_type] ...
- C# ToString格式大全
C# 货币 2.5.ToString("C"); // ¥2.50 // D 10进制数 25.ToString("D5"); // 25000 // E ...
- Windows Live Writer的Markdown插件MarkdownInLiveWriter支持语法高亮了
我前几天开发的Windows Live Writer的Markdown的插件MarkdownInLiveWriter支持语法高亮了.参见下图: 基本上就是把我的另一个插件CodeInLiveWrite ...
- 开发库比较(3) - Mobile Web 开发 - Sencha, jquerymobiel, phonejs, jqtouch, jqmobi
我们一直坚信Html/css在界面上最终会一统江湖,因为在众多的界面编写中,qt,gtk,wpf,win form, wxwidgets等等,只有Html/CSS是真正拥有统一标准,只有这个有潜力作用 ...