Headfirst设计模式的C++实现——组合模式(Composite)
menu_component.h
#ifndef _MENU_COMPONENT_H_
#define _MENU_COMPONENT_H_ #include <string> class MenuComponent {
public:
class Iterator {
public:
virtual bool has_next() = ; virtual MenuComponent *next() = ;
}; MenuComponent( const std::string &_name, const std::string &_description ) :
name(_name), description(_description) {} const std::string& get_name() { return name; } const std::string& get_description() { return description; } virtual void print() = ; virtual Iterator *get_iterator() = ;
private:
std::string name;
std::string description;
};
#endif
menu_item.h
#ifndef _MENU_ITEM_H_
#define _MENU_ITEM_H_ #include "menu_component.h"
#include <iostream> class MenuItem : public MenuComponent {
public:
MenuItem( const std::string &_name, const std::string &_description, double _price, bool _vegetarian ) :
MenuComponent( _name, _description ), price(_price), vegetarian(_vegetarian) {} void print() {
std::cout << get_name() << " "
<< get_description() << " "
<< get_price() << " "
<< is_vegetarian() << std::endl;
} double get_price() { return price; } bool is_vegetarian() { return vegetarian; } Iterator *get_iterator() { return NULL; }
private:
double price;
bool vegetarian;
};
#endif
menu.h
#ifndef _MENU_H_
#define _MENU_H_ #include "ivector.h"
#include <stack> class Menu : public MenuComponent{
public:
Menu( const std::string &_name, const std::string &_description ) :
MenuComponent( _name, _description ),
_iterator( menu_components.get_iterator() ) {} void add( MenuComponent* component ) { menu_components.add( component ); } void print() { std::cout << get_name() << " " << get_description() << std::endl; } Iterator *get_iterator() { return &_iterator; } private:
IVector menu_components; class _Iterator : public Iterator {
public:
_Iterator( Iterator *it) { s.push(it); } bool has_next() {
if ( s.empty() ) { return false; }
if ( s.top()->has_next() ) { return true; }
s.pop();
return has_next();
} MenuComponent *next() {
if ( has_next() ) {
MenuComponent *next = s.top()->next();
if ( NULL != next->get_iterator() ) { s.push( next->get_iterator() );}
return next;
}
}
private:
std::stack<Iterator *> s;
} _iterator;
};
#endif
ivector.h
#ifndef _IVECTOR_H_
#define _IVECTOR_H_ #include "menu_component.h"
#include <vector> class IVector{
public:
IVector() : data_iterator( *this ) {} void add( MenuComponent *component ) { data_v.push_back( component ); } MenuComponent::Iterator *get_iterator() { return &data_iterator; }
private:
std::vector<MenuComponent *> data_v; class _Iterator : public MenuComponent::Iterator {
public:
_Iterator (IVector &_ivector) : ivector(_ivector), pos() {} bool has_next() { return pos < ivector.data_v.size(); } MenuComponent *next() { return ivector.data_v[pos++]; }
private:
int pos; IVector &ivector;
} data_iterator;
};
#endif
main.cpp
#include "menu_item.h"
#include "menu.h" int main() { Menu *p = new Menu("total", "total");
p->add(new MenuItem("MenuItem 1", "test description", 9.4, true));
p->add(new MenuItem("MenuItem 2", "test description", 9.4, true)); Menu *p1 = new Menu("sub menu", "test description");
p1->add(new MenuItem("MenuItem 3", "test description", 9.4, true));
p1->add(new MenuItem("MenuItem 4", "test description", 9.4, true));
p->add(p1); MenuComponent::Iterator *it = p->get_iterator();
while ( it->has_next() ) {
MenuItem *menu_item = (MenuItem *)it->next();
menu_item->print();
} return ;
}
Headfirst设计模式的C++实现——组合模式(Composite)的更多相关文章
- 设计模式(七)组合模式Composite(结构型)
设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...
- Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus[转]
1 2{<HeadFirst设计模式>之组合模式 } 3{ 组合与单项的抽象父类 } 4{ 编译工具:Delphi2007 for win32} 5{ E-M ...
- 设计模式(八)组合模式 Composite
组合模式: 允许你将对象组合成树形结构来表现“整体/部分”层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 组合模式适用于创建复杂的对象,这个对象包含某些个别的对象以及这些对象的组合. 从 ...
- 乐在其中设计模式(C#) - 组合模式(Composite Pattern)
原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...
- Java设计模式(8)组合模式(Composite模式)
Composite定义:将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性. Composite比较容易理解,想到Composite就应该想到树 ...
- 设计模式系列之组合模式(Composite Pattern)——树形结构的处理
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 浅谈设计模式--组合模式(Composite Pattern)
组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...
- 二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
- 【设计模式】组合模式 Composite Pattern
树形结构是软件行业很常见的一种结构,几乎随处可见, 比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...
随机推荐
- NUMBER_GET_NEXT
1. SNRO /SNUM创建一个流水号对象 CALL FUNCTION 'NUMBER_RANGE_ENQUEUE' EXPORTING OBJECT = '' EXCEPTIONS FOREIGN ...
- eclipse内使用tomcat项目究竟被部署到了哪里
笔者在使用eclipse+tomcat做本地调试,项目没跑起来,原因就很奇怪啊(某前辈说过:奇怪源于无知),然后就想它究竟是把项目放到哪个目录下呢,我的tomcat/webapps目录下并没有啊. 默 ...
- MySQL通过RPM安装
以前写过一篇文章,RedHat Linux 6.1 安装MySQL,本文是从解决依赖的角度上再次描述如何在Linux下以RPM包方式安装MySQL. [root@serv01 ~]# ls /iso/ ...
- cocos2d-x 触摸偏移
转自:http://www.cnblogs.com/fjut/archive/2012/04/28/2475693.html //ccTouchBegan必须实现,否则会报错 bool PicScan ...
- 数据持久化(五)之CoreData
@简单的说,Core Data就是能够存储到磁盘的对象图,[...]Core Data能够帮我们做非常多任务作.它能够作为软件的整个模型层. 它不只在磁盘上存储数据.也把我们须要的数据对象读取到内存中 ...
- U-BOOT 移植到友善之臂mini2440
U-BOOT 移植到友善之臂mini2440 开发环境:ubuntu 10.10 编译器:友善之臂mini2440光盘自带arm-linux-gcc 4.4.3 一. 在denx官网下载源码,我所用版 ...
- TIME-WAIT和CLOSE-WAIT
系统调优,你所不知道的TIME_WAIT和CLOSE_WAIT 2016-03-11 运维帮 来源微信订阅号:大房说 作者:大房 你遇到过TIME_WAIT的问题吗? 我相信很多都遇到过这个问题. ...
- 阿里封神谈hadoop学习之路
阿里封神谈hadoop学习之路 封神 2016-04-14 16:03:51 浏览3283 评论3 发表于: 阿里云E-MapReduce >> 开源大数据周刊 hadoop 学生 s ...
- 文件I/O(不带缓冲)之lseek函数
每个打开的文件都有一个与其相关联的“当前文件偏移量”(current file offset).它通常是一个非负整数,用以度量从文件开始处计算的字节数.通常,读.写操作都从当前文件偏移量处开始,并使偏 ...
- 二、Socket之UDP异步传输文件
上一篇文章一.Socket之UDP异步传输文件中,实现了文件的基本传输,但是传输过程中的信息是看不到的,这一篇是对上一篇进行了一些改进,并且可以了解传输的信息(加入了Log),还加入了接收或者拒绝接收 ...