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继承 ...
随机推荐
- iOS 根据文件名获取到文件路径
根据文件名来获取文件路径(Document目录下) //根据文件名来获取文件路径 - (NSString *)dataFilePath:(NSString *)sender { NSArray *pa ...
- FloatingWindow
https://github.com/dev0x10/android-bubble https://github.com/dev0x10/FloatingView https://github.com ...
- Java基础 Day14 泛型
//为什么要使用泛型 //1.解决元素存储的安全性的问题 //2.解决获取元素时,须要类型转换的问题 //未使用泛型 package org.tizen.test; import java.util. ...
- What Influences Method Call Performance in Java?--reference
reference from:https://www.voxxed.com/blog/2015/02/too-fast-too-megamorphic-what-influences-method-c ...
- 深入研究Block捕获外部变量和__block实现原理
Blocks是C语言的扩充功能,而Apple 在OS X Snow Leopard 和 iOS 4中引入了这个新功能“Blocks”.从那开始,Block就出现在iOS和Mac系统各个API中,并被大 ...
- /lib /usr/lib /usr/local/lib 的区别
/lib是内核级的,/usr/lib是系统级的,/usr/local/lib是用户级的. /lib/ — 包含许多被 /bin/ 和 /sbin/ 中的程序使用的库文件.目录 /usr/lib/ 中含 ...
- 这五个题你懂了javascript你就入门了
1. if (!("a" in window)) { var a = 1; } alert(a); 阅读代码:如果window不包含属性a,就声明一个变量a,然后赋值为1,最后弹出 ...
- 你了解C语言中的关键字volatile吗?
我们在学习C语言的32个关键字时,大家都不太注意volatile这个关键字,volatile是一个类型修饰符.volatile的中文意思是“易变的”.那么在程序中我们在什么情况下才使用他呢?我们在分析 ...
- mongodb用mongoose取到的对象不能增加属性
先定义了一个article的schema var mongoose = require('mongoose'); var Schema = mongoose.Schema; exports.schem ...
- Nodejs v4.x.0API文档学习(2)Assert断言测试模块
文档参考地址:https://nodejs.org/dist/latest-v4.x/docs/api/ Assert(断言) assert模块提供了一组简单的断言测试方法,可以拥有测试不变量.该模块 ...