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

  1. 设计模式(七)组合模式Composite(结构型)

    设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...

  2. Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus[转]

     1  2{<HeadFirst设计模式>之组合模式 }  3{ 组合与单项的抽象父类           }  4{ 编译工具:Delphi2007 for win32}  5{ E-M ...

  3. 设计模式(八)组合模式 Composite

    组合模式: 允许你将对象组合成树形结构来表现“整体/部分”层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 组合模式适用于创建复杂的对象,这个对象包含某些个别的对象以及这些对象的组合. 从 ...

  4. 乐在其中设计模式(C#) - 组合模式(Composite Pattern)

    原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...

  5. Java设计模式(8)组合模式(Composite模式)

    Composite定义:将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性. Composite比较容易理解,想到Composite就应该想到树 ...

  6. 设计模式系列之组合模式(Composite Pattern)——树形结构的处理

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  7. 浅谈设计模式--组合模式(Composite Pattern)

    组合模式(Composite Pattern) 组合模式,有时候又叫部分-整体结构(part-whole hierarchy),使得用户对单个对象和对一组对象的使用具有一致性.简单来说,就是可以像使用 ...

  8. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

  9. 【设计模式】组合模式 Composite Pattern

    树形结构是软件行业很常见的一种结构,几乎随处可见,  比如: HTML 页面中的DOM,产品的分类,通常一些应用或网站的菜单,Windows Form 中的控件继承关系,Android中的View继承 ...

随机推荐

  1. IDF实验室-简单编程-特殊的日子 writeup

    题目:http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=50 题目提示要爆破,代表加密应该是不可逆的. 密文:4D ...

  2. hadoop 1.2 集群搭建与环境配置

    一.虚拟机环境 见我的另一篇博客http://www.cnblogs.com/xckk/p/6000881.html, 需要安装JDK环境,centos下安装JDK可参考: http://www.ce ...

  3. cocos2d-x 基本数学

    转自:http://cjhworld.blog.163.com/blog/static/207078036201331510141222/ 数学函数: ccp(x, y); // 以坐标x,y创建一个 ...

  4. .@RequestMapping 使用方法

    1.@RequestMapping  使用方法  SpringMVC中,@RequestMapping用来处理请求,比方XXX.do @RequestMapping("/aaa") ...

  5. Andorid4.x 流氓式屏蔽HOME键

    转载请列明出处 http://blog.csdn.net/steelychen/article/details/37757341 应用项目须要要屏蔽HOME键. 项目本身的要求是让按下HOME键后程序 ...

  6. [Whole Web] [Node.js] [Browserify] [Grunt] Automation task with grunt-browserify & grunt-contrib-watch

    What we want is when the server side Node.js files have been changed, we want to use browserify to b ...

  7. 《SAS编程与数据挖掘商业案例》学习笔记之十七

    继续读书笔记,本次重点sas sql语句,因为sql内容多且复杂,本文仅仅介绍商业应用中经常使用的而且easy出错的地方,内容包含:单表操作.多表关联.子查询以及merge和join的差别 1.单表操 ...

  8. java跨平台性分析

    实不相瞒,Java是我见过的执行效率最低的程序设计语言,前不久在CSDN论坛上有个评测,计算9999的阶乘,同样的循环算法,Java的耗时是.NET的5倍.我以前很喜欢Serv-U,自从它用Java重 ...

  9. spring Transaction Management --官方

    原文链接:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html 12.  ...

  10. Android实现资料收藏

    1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = new Intent(Intent.ACTI ...