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. Android设计模式—策略模式

    1.策略模式概念 定义一系列算法,把他们独立封装起来,并且这些算法之间可以相互替换.策略模式主要是管理一堆有共性的算法,客户端可以根据需要,很快切换这些算法,并且保持可扩展性. 策略模式的本质:分离算 ...

  2. 对PostgreSQL cmin和cmax的理解

    看例子: 开两个终端来对比: 在终端A: [pgsql@localhost bin]$ ./psql psql () Type "help" for help. pgsql=# b ...

  3. iOS8 CLLocationManager 、CLGeocoder获取地理位置

    最近在ios8.0使用CLLocationManager定位服务,发现老不能定位,查看设置菜单中的项也是处于未知状态.想起之前都有一个弹出框提示用户是否允许定位,这次一直没有出现了.原来ios8.0下 ...

  4. oracle查询前10条记录

    select * from table_name where rownum<11;

  5. TQ210裸机编程(2)——LED流水灯

    两个文件start.S和led.c start.S .global _start                @声明一个全局的标号 _start:     bl main               ...

  6. Java Annotations: Explored & Explained--转载

    原文地址:http://www.javacodegeeks.com/2012/08/java-annotations-explored-explained.html One of the many w ...

  7. Win7无线网络共享设置方法

    http://jingyan.baidu.com/article/4f34706e89bb2ae387b56d0b.html

  8. VIM 分割窗口

    VIM 分割窗口     *08.1*  分割窗口 打开新窗口最简单的命令如下: :split 这个命令把屏幕分解成两个窗口并把光标置于上面的窗口中: +----------------------- ...

  9. Android_Spinner_Listener

    xml布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...

  10. Linux下解决permission denied问题

    由于权限问题,在linux下启动tomcat出现权限permission denied 提示解决方法如下: 1.cd 进入tomcat/bin目录 2.运行下面命令 sudo chmod 777 st ...