Headfirst设计模式的C++实现——组合模式(Composite)

menu_component.h

 1 #ifndef _MENU_COMPONENT_H_
 2 #define _MENU_COMPONENT_H_
 3
 4 #include <string>
 5
 6 class MenuComponent {
 7 public:
 8     class Iterator {
 9     public:
10         virtual bool has_next() = 0;
11
12         virtual MenuComponent *next() = 0;
13     };
14
15     MenuComponent( const std::string &_name, const std::string &_description ) :
16         name(_name), description(_description) {}
17
18     const std::string& get_name() { return name; }
19
20     const std::string& get_description() { return description; }
21
22     virtual void print() = 0;
23
24     virtual Iterator *get_iterator() = 0;
25 private:
26     std::string name;
27     std::string description;
28 };
29 #endif

menu_item.h

 1 #ifndef _MENU_ITEM_H_
 2 #define _MENU_ITEM_H_
 3
 4 #include "menu_component.h"
 5 #include <iostream>
 6
 7 class MenuItem : public MenuComponent {
 8 public:
 9     MenuItem( const std::string &_name, const std::string &_description, double _price, bool _vegetarian ) :
10         MenuComponent( _name, _description ), price(_price), vegetarian(_vegetarian) {}
11
12     void print() {
13         std::cout << get_name() << " "
14                   << get_description() << " "
15                   << get_price() << " "
16                   << is_vegetarian() << std::endl;
17     }
18
19     double get_price() { return price; }
20
21     bool is_vegetarian() { return vegetarian; }
22
23     Iterator *get_iterator() { return NULL; }
24 private:
25     double price;
26     bool vegetarian;
27 };
28 #endif

menu.h

 1 #ifndef _MENU_H_
 2 #define _MENU_H_
 3
 4 #include "ivector.h"
 5 #include <stack>
 6
 7 class Menu : public MenuComponent{
 8 public:
 9     Menu( const std::string &_name, const std::string &_description ) :
10         MenuComponent( _name, _description ),
11         _iterator( menu_components.get_iterator() ) {}
12
13     void add( MenuComponent* component ) { menu_components.add( component ); }
14
15     void print() { std::cout << get_name() << " " << get_description() << std::endl; }
16
17     Iterator *get_iterator() { return &_iterator; }
18
19 private:
20     IVector menu_components;
21
22     class _Iterator : public Iterator {
23     public:
24         _Iterator( Iterator *it) { s.push(it); }
25
26         bool has_next() {
27             if ( s.empty() ) { return false; }
28             if ( s.top()->has_next() ) { return true; }
29             s.pop();
30             return has_next();
31         }
32
33         MenuComponent *next() {
34             if ( has_next() ) {
35                 MenuComponent *next = s.top()->next();
36                 if ( NULL != next->get_iterator() ) { s.push( next->get_iterator() );}
37                 return next;
38             }
39         }
40     private:
41         std::stack<Iterator *> s;
42     } _iterator;
43 };
44 #endif

ivector.h

 1 #ifndef _IVECTOR_H_
 2 #define _IVECTOR_H_
 3
 4 #include "menu_component.h"
 5 #include <vector>
 6
 7 class IVector{
 8 public:
 9     IVector() : data_iterator( *this ) {}
10
11     void add( MenuComponent *component ) { data_v.push_back( component ); }
12
13     MenuComponent::Iterator *get_iterator() { return &data_iterator; }
14 private:
15     std::vector<MenuComponent *> data_v;
16
17     class _Iterator : public MenuComponent::Iterator {
18     public:
19         _Iterator (IVector &_ivector) : ivector(_ivector), pos(0) {}
20
21         bool has_next() { return pos < ivector.data_v.size(); }
22
23         MenuComponent *next() { return ivector.data_v[pos++]; }
24     private:
25         int pos;
26
27         IVector &ivector;
28     } data_iterator;
29 };
30 #endif

main.cpp

 1 #include "menu_item.h"
 2 #include "menu.h"
 3
 4 int main() {
 5
 6     Menu *p = new Menu("total", "total");
 7     p->add(new MenuItem("MenuItem 1", "test description", 9.4, true));
 8     p->add(new MenuItem("MenuItem 2", "test description", 9.4, true));
 9
10     Menu *p1 = new Menu("sub menu", "test description");
11     p1->add(new MenuItem("MenuItem 3", "test description", 9.4, true));
12     p1->add(new MenuItem("MenuItem 4", "test description", 9.4, true));
13     p->add(p1);
14
15     MenuComponent::Iterator *it = p->get_iterator();
16     while ( it->has_next() ) {
17         MenuItem *menu_item = (MenuItem *)it->next();
18         menu_item->print();
19     }
20
21     return 0;
22 }
时间: 2024-08-10 02:09:53

Headfirst设计模式的C++实现——组合模式(Composite)的相关文章

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

设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 我们可以使用简单的对象组合成复杂的对象,而这个复杂对象有可以组合成更大的对象.我们可以把简单这些对象定义成类,然后定义一些容器类来存储这些简单对象.客户端代码必须区别对象简单对象和容器对象,而实际上大多数情况下用户认为它们是一样的.对这些类区别使用,使得程序更加复杂.递归使用的时候跟麻烦,而我们如何

设计模式(结构型)之组合模式(Composite Pattern)

PS一句:最终还是选择CSDN来整理发表这几年的知识点,该文章平行迁移到CSDN.因为CSDN也支持MarkDown语法了,牛逼啊! [工匠若水 http://blog.csdn.net/yanbober] 阅读前一篇<设计模式(结构型)之桥接模式(Bridge Pattern)> http://blog.csdn.net/yanbober/article/details/45366781 概述 组合模式又叫做部分-整体模式,使我们在树型结构的问题中模糊简单元素和复杂元素的概念,客户程序可以像

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

 1 2{<HeadFirst设计模式>之组合模式 } 3{ 组合与单项的抽象父类           } 4{ 编译工具:Delphi2007 for win32} 5{ E-Mail :[email protected]   } 6 7unit uMenuComponent; 8 9interface1011uses12  SysUtils;1314type15  TMenuComponent = class abstract(TObject)16  public17    procedu

设计模式(二)组合模式Composite(表达式求值)

组合模式目标:将对象组合成树形结构以表示部分整体的关系,Composite使得用户对单个对象和组合对象的使用具有一致性. 透露一下:这个例子可以说是组合模式的典型应用,据说(作者说)某个编译器开发团队用了两个半月的时间实现了表达式求值,被作者用十几行代码就这样实现了. 需求:表达式求值,是编译器的重要组件,本例你能找到的实际代码应该不多,因为是本人根据<C++沉思录>里面的例子亲自敲出来的(当然都是作者的功劳).目的在于支持各种一元运算,二元运算甚至更多的运算都加入到表达式求值中,程序方便扩展

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

组合模式: 允许你将对象组合成树形结构来表现“整体/部分”层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 组合模式适用于创建复杂的对象,这个对象包含某些个别的对象以及这些对象的组合. 从操作的角度而言,客户端对于 个别对象/组合 的操作是一致的. 模拟场景 如图所示,总公司下属有多个部门,而子公司可以视为多个部门的组合. 整个数据结构呈树状,完美契合组合模式的应用场景. UML: 在这个场景中: 个别对象 -> 人事部(HRDepartment)和财务部(FinanceDepart

设计模式 - 组合模式(composite pattern) 详解

组合模式(composite pattern) 详解 本文地址: http://blog.csdn.net/caroline_wendy 组合模式: 允许你将对象组合成树形结构来表现"整体/部分"层次结构. 组合能让客户以一致的方法处理个别对象以及组合对象. 建立组件类(Component), 组合类(composite)和叶子类(leaf)继承组件类, 客户类(client)直接调用最顶层的组合类(composite)即可. 具体方法: 1. 组件类(component), 包含组合

设计模式之组合模式(Composite)摘录

23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于如何创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而一个对象创建型模式将实例化委托给另一个对象.创建型模式有两个不断出现的主旋律.第一,它们都将关于该系统使用哪些具体的类的信息封装起来.第二,它们隐藏了这些类的实例是如何被创建和放在一起的.整个系统关于这些对象所知道的是由抽象类所定义的接口.因此,创建型模式在什么被创建,谁创建它,它是怎样被创建的,以

设计模式 - 组合模式(composite pattern) 迭代器(iterator) 详解

组合模式(composite pattern) 迭代器(iterator) 详解 本文地址: http://blog.csdn.net/caroline_wendy 参考组合模式(composite pattern): http://blog.csdn.net/caroline_wendy/article/details/36895627 在组合模式(composite pattern)添加迭代器功能, 遍历每一个组合(composite)的项. 具体方法: 1. 抽象组件类(abstract

设计模式之组合模式---Composite Pattern

模式的定义 组合模式(Composite Pattern)定义如下: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. 将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性.