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继承 ...
随机推荐
- 完全分布式Hadoop2.3安装与配置
一.Hadoop基本介绍 Hadoop优点 1.高可靠性:Hadoop按位存储和处理数据 2.高扩展性:Hadoop是在计算机集群中完成计算任务,这个集群可以方便的扩展到几千台 3.高效性:Hadoo ...
- C#WinForm应用程序实现自动填充网页上的用户名和密码并点击登录按钮【转载】
使用WebBrowser控件,在documentComplete事件处理器里写 HtmlElement name = webBrowser1.Document.GetElementById(" ...
- git在windows下clone、pull或者push内存溢出的解决办法
发现国内使用git来真正管理源码的人不多,特别是大数据量的源码,今天使用git clone android的源码时突然出现remote out of memery,解决办法: git config - ...
- java.util.Scanner的日常用法
Scanner是新增的一个简易文本扫描器,在 JDK 5.0之前,是没有的.查看最新在线文档: public final class Scanner extends Object implements ...
- 深刻理解Python中的元类(metaclass)
译注:这是一篇在Stack overflow上很热的帖子.提问者自称已经掌握了有关Python OOP编程中的各种概念,但始终觉得元类(metaclass)难以理解.他知道这肯定和自省有关,但仍然觉得 ...
- C#_StringBuilder分离字符串实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Stri ...
- 用antlr文法编写的hermit swrl规则(分享)
/* * To change this license header, choose License Headers in Project Properties. * To change this t ...
- window.external.notify() 与 UglifyJS 压缩优化冲突
近期研究了一下 UglifyJs 对 JS 代码的压缩,发现 UglifyJS 压缩后,无法调用 window.external.notify() 方法,JS 代码如下: function MyNot ...
- java 获取本机ip地址
/** * 取当前系统站点本地地址 linux下 和 window下可用 * * @return */ public static String getLocalIP() { String sIP = ...
- WebGoat视频教程下载
WebGoat视频教程下载:http://pan.baidu.com/s/1pJlsfQ7