Headfirst设计模式的C++实现——组合模式(Composite)
Posted Ren.Yu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于Headfirst设计模式的C++实现——组合模式(Composite)的主要内容,如果未能解决你的问题,请参考以下文章
Headfirst设计模式的C++实现——简单工厂模式(Simple Factory)之二
Headfirst设计模式的C++实现——抽象工厂(Abstract Factory)
Headfirst设计模式的C++实现——迭代器(Iterator)