目录

  • 1. 封装遍历
  • 2. 定义迭代器模式
    • 2.1 部分源码
      • 2.1.1 MenuItem.h
      • 2.1.2 Menu.h
      • 2.1.3 Iterator.h
      • 2.1.4 ArrayIterator.h
      • 2.1.5 ArrayIterator.cpp
      • 2.1.6 DinerMenu.h
      • 2.1.7 DinerMenu.cpp
  • 3. 单一责任
  • 4. 定义组合模式
    • 4.1 类图和部分源码
      • 4.1.1 MenuComponent.h
      • 4.1.2 MenuItem.h
      • 4.1.3 Menu.h
      • 4.1.4 Waitress.h
      • 4.1.5 main.cpp
  • 5. 组合迭代器

1. 封装遍历

因为早餐和午餐的遍历方式不同,这样在遍历菜单时由于不同的遍历方式不方便。
当我们对遍历进行封装就会对实现遍历的统一


2. 定义迭代器模式

通过类图我们可以知道:
Iterator是一个接口,通过实现接口内的方法达到遍历的标准
Aggregate是聚合的接口,通过对迭代器接口的引用实现对迭代器的解耦
Client通过对两边接口的引用实现了集合和迭代器的解耦


2.1 部分源码

2.1.1 MenuItem.h

#include <string>
using std::string;
class MenuItem{public:string name;string description;bool vegetarian;double price;MenuItem();MenuItem(string name,string description,bool vegetarian,double price);string getName();string getDescription();double getPrice();bool isVegetarian();string toString();
};

2.1.2 Menu.h

#include "Iterator.h"
class Menu{public:virtual Iterator *createIterator()=0;virtual ~Menu(){};
};

2.1.3 Iterator.h

#include "MenuItem.h"
class Iterator{public:virtual bool hasNext()=0;virtual MenuItem *next()=0;virtual ~Iterator(){};
};

2.1.4 ArrayIterator.h

#include "Iterator.h"
#include "MenuItem.h"
class ArrayIterator:public Iterator{public:ArrayIterator(MenuItem items[],int size);virtual MenuItem *next()override final;virtual bool hasNext()override final;virtual ~ArrayIterator();
private:MenuItem *items;int position=0;int size=0;
};

2.1.5 ArrayIterator.cpp

#include "ArrayIterator.h"ArrayIterator::ArrayIterator(MenuItem items[], int size)
{this->items=items;this->size=size;
}MenuItem *ArrayIterator::next()
{return &items[position++];
}bool ArrayIterator::hasNext()
{if(position>=size)return false;else return true;
}ArrayIterator::~ArrayIterator()
{}

2.1.6 DinerMenu.h

#include "Menu.h"
#include "MenuItem.h"
#include "Iterator.h"
#include "ArrayIterator.h"
class DinerMenu:public Menu{public:DinerMenu();void addItem(string name,string description,bool vegetarian, double price);MenuItem *getMenuItems();virtual Iterator *createIterator()override final;virtual ~DinerMenu();
private:int numberOfItems=0;MenuItem *menuItems;
};

2.1.7 DinerMenu.cpp

#include "DinerMenu.h"
#include "iostream"
int MAX_ITEMS = 6;
DinerMenu::DinerMenu()
{menuItems=new MenuItem[MAX_ITEMS];menuItems[0]=MenuItem("Vegetarian BLT","(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);menuItems[1]=MenuItem("BLT","Bacon with lettuce & tomato on whole wheat", false, 2.99);menuItems[2]=MenuItem("Soup of the day","Soup of the day, with a side of potato salad", false, 3.29);menuItems[3]=MenuItem("Hotdog","A hot dog, with sauerkraut, relish, onions, topped with cheese",false, 3.05);menuItems[4]=MenuItem("Steamed Veggies and Brown Rice","Steamed vegetables over brown rice", true, 3.99);menuItems[5]=MenuItem("Pasta","Spaghetti with Marinara Sauce, and a slice of sourdough bread",true, 3.89);numberOfItems=6;
}void DinerMenu::addItem(std::string name, std::string description, bool vegetarian, double price)
{MenuItem menuItem=MenuItem(name,description,vegetarian,price);if(numberOfItems>=MAX_ITEMS){std::cout<<"Sorry, menu is full!  Can't add item to menu"<<std::endl;}else{menuItems[numberOfItems]=menuItem;numberOfItems++;}
}MenuItem *DinerMenu::getMenuItems()
{return menuItems;
}Iterator *DinerMenu::createIterator()
{return new ArrayIterator(menuItems,numberOfItems);
}DinerMenu::~DinerMenu()
{}

3. 单一责任

如果允许一个类既要管理集合又要提供遍历,当集合改变需要改变类,当遍历改变也要改变类,所以尽量使一个类只有一个责任

4. 定义组合模式

在前面我们都是一个菜单一个菜单独立出来的,但是当我们菜单中又要嵌套菜单这咋办?

组合模式就是定义一个超类,两个具体类继承,一个类可以嵌套超类就相当于菜单(这样就可以嵌套菜单也可以嵌套菜单项),另一个就是具体的菜单项



4.1 类图和部分源码

4.1.1 MenuComponent.h

#include <string>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::vector;
class MenuComponent{public:virtual void add(MenuComponent *menuComponent);virtual void remove(MenuComponent *menuComponent);virtual MenuComponent *getChild(int i);virtual string getName();virtual string getDescription();virtual double getPrice();virtual bool isVegetarian();virtual void print();virtual ~MenuComponent();
};

4.1.2 MenuItem.h

#include "MenuComponent.h"
class MenuItem:public MenuComponent{public:MenuItem(string name,string description,bool vegetarian,double price);virtual string getName()override final;virtual string getDescription()override final;virtual double getPrice()override final;virtual bool isVegetarian()override final;virtual void print()override final;virtual ~MenuItem();
private:string name;string description;bool vegetarian;double price;
};

4.1.3 Menu.h

#include "MenuComponent.h"
class Menu:public MenuComponent{public:Menu(string name,string description);virtual void add(MenuComponent *menuComponent)override final;virtual void remove(MenuComponent *menuComponent)override final;virtual MenuComponent *getChild(int i)override final;virtual string getName()override final;virtual string getDescription()override final;virtual void print()override final;virtual ~Menu();
private:string name;string description;vector<MenuComponent *> menuComponents;
};

4.1.4 Waitress.h

#include "MenuComponent.h"
class Waitress{public:Waitress(MenuComponent *allMenus);void printMenu();virtual ~Waitress();
private:MenuComponent *allMenus;
};

4.1.5 main.cpp

#include <iostream>
#include "MenuComponent.h"
#include "Menu.h"
#include "Waitress.h"
#include "MenuItem.h"
using namespace std;int main()
{MenuComponent *pancakeHouseMenu =new Menu("PANCAKE HOUSE MENU", "Breakfast");MenuComponent *dinerMenu =new Menu("DINER MENU", "Lunch");MenuComponent *cafeMenu =new Menu("CAFE MENU", "Dinner");MenuComponent *dessertMenu =new Menu("DESSERT MENU", "Dessert of course!");MenuComponent *coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee");MenuComponent *allMenus = new Menu("ALL MENUS", "All menus combined");allMenus->add(pancakeHouseMenu);allMenus->add(dinerMenu);allMenus->add(cafeMenu);pancakeHouseMenu->add(new MenuItem("K&B's Pancake Breakfast","Pancakes with scrambled eggs and toast",true,2.99));pancakeHouseMenu->add(new MenuItem("Regular Pancake Breakfast","Pancakes with fried eggs, sausage",false,2.99));pancakeHouseMenu->add(new MenuItem("Blueberry Pancakes","Pancakes made with fresh blueberries, and blueberry syrup",true,3.49));pancakeHouseMenu->add(new MenuItem("Waffles","Waffles with your choice of blueberries or strawberries",true,3.59));dinerMenu->add(new MenuItem("Vegetarian BLT","(Fakin') Bacon with lettuce & tomato on whole wheat",true,2.99));dinerMenu->add(new MenuItem("BLT","Bacon with lettuce & tomato on whole wheat",false,2.99));dinerMenu->add(new MenuItem("Soup of the day","A bowl of the soup of the day, with a side of potato salad",false,3.29));dinerMenu->add(new MenuItem("Hot Dog","A hot dog, with saurkraut, relish, onions, topped with cheese",false,3.05));dinerMenu->add(new MenuItem("Steamed Veggies and Brown Rice","Steamed vegetables over brown rice",true,3.99));dinerMenu->add(new MenuItem("Pasta","Spaghetti with marinara sauce, and a slice of sourdough bread",true,3.89));dinerMenu->add(dessertMenu);dessertMenu->add(new MenuItem("Apple Pie","Apple pie with a flakey crust, topped with vanilla icecream",true,1.59));dessertMenu->add(new MenuItem("Cheesecake","Creamy New York cheesecake, with a chocolate graham crust",true,1.99));dessertMenu->add(new MenuItem("Sorbet","A scoop of raspberry and a scoop of lime",true,1.89));cafeMenu->add(new MenuItem("Veggie Burger and Air Fries","Veggie burger on a whole wheat bun, lettuce, tomato, and fries",true,3.99));cafeMenu->add(new MenuItem("Soup of the day","A cup of the soup of the day, with a side salad",false,3.69));cafeMenu->add(new MenuItem("Burrito","A large burrito, with whole pinto beans, salsa, guacamole",true,4.29));cafeMenu->add(coffeeMenu);coffeeMenu->add(new MenuItem("Coffee Cake","Crumbly cake topped with cinnamon and walnuts",true,1.59));coffeeMenu->add(new MenuItem("Bagel","Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",false,0.69));coffeeMenu->add(new MenuItem("Biscotti","Three almond or hazelnut biscotti cookies",true,0.89));Waitress *waitress = new Waitress(allMenus);waitress->printMenu();return 0;
}

5. 组合迭代器


《headfirst设计模式》读书笔记9-迭代器和组合模式相关推荐

  1. HeadFirst设计模式读书笔记

    简单的做下笔记,以后找起来方便.设计原则通用,不针对哪个模式. 1 策略模式 定义了算法族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化独立于使用算法的客户. 设计原则: 找出应用中可能需 ...

  2. 测试类图Head First 设计模式 (九) 迭代器与组合模式(Iterator Composite pattern) C++实现...

    在改章节中,我们主要介绍测试类图的内容,自我感觉有个不错的建议和大家分享下 迭代器模式供提一种方法序顺问访一个聚合对象中的各个素元,而又不露暴其部内的示表. 计设准则:   单一任责准则:一个类应当只 ...

  3. 设计模式学习笔记(十一)-组合模式

    树形结构在软件中随处可见,例如操作系统中的目录结构.应用软件中的菜单.办公系统中的公司组织结构等等,如何运用面向对象的方式来处理这种树形结构是组合模式需要解决的问题,组合模式通过一种巧妙的设计方案使得 ...

  4. HeadFirst设计模式读书笔记--观察者模式(2)(二)

    设计气象站(案例) 实现气象站 public interface Subject{/**这两个方法都需要观察者作为变量,该观察者是用来注册或被删除的*/public void registerObse ...

  5. Head First设计模式读书笔记四 简单工厂 工厂模式 抽象工厂模式

    本文示例代码材料源自Head First设计模式 以前整理自己整理的链接: 工厂模式 https://blog.csdn.net/u011109881/article/details/56541580 ...

  6. 大话设计模式读书笔记(十三) 状态模式

    状态模式: 状态模式定义: 状态模式(State):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像改变了其子类. 状态模式UMl类图: 状态模式Java代码实现 public class ...

  7. Head First设计模式读书笔记九 第十章 状态模式

    过去的笔记链接 https://blog.csdn.net/u011109881/article/details/60158137 状态模式实例 用Java设计糖果机吧 大致流程: 上图中,有四种状态 ...

  8. Head First设计模式读书笔记七 第八章 模板方法模式

    本文示例代码材料源自Head First设计模式 以前整理自己整理的链接: https://blog.csdn.net/u011109881/article/details/60594985 简介 模 ...

  9. android工厂模式和策略模式,android 源码设计模式读书笔记(四)工厂模式和策略模式...

    把这两个一起写 因为他们两个UML的代码接口非常的相似 工厂模式代码结构图 image.png 策略模式UML image.png 在我们看完结构图后 感觉位移不同的就是Factory个Context ...

  10. 大话设计模式读书笔记

    主题 概要 设计模式 大话设计模式读书笔记 编辑 时间 新建 20170423 序号 参考资料 1 大话设计模式 重新看了一遍设计模式,除了一些已经特别熟悉的模式,都自己敲了一遍代码,有些豁然开朗的感 ...

最新文章

  1. jvm 堆外内存_NIO效率高的原理之零拷贝与直接内存映射
  2. 如何提升微服务的幸福感
  3. 26进制(字母)转十进制算法
  4. OAF TABLE中添加序号列
  5. JAVASCRIPT C# 相互访问
  6. linux下51单片机开发解决方案
  7. POJ-1845 Sumdiv 逆元,特殊情况
  8. 【剑指offer】面试题15:二进制中1的个数(Java)
  9. 【从入门到放弃-ZooKeeper】ZooKeeper入门
  10. Centos系统普通用户开启sudo命令
  11. 程序员必备技能之单元测试
  12. 怎么做c语言的子程序,哪位师傅知道51单片机怎样编写子程序?C语言的。在主程序里调...
  13. [R时间序列]ARMA模型如何分辨拖尾与截尾
  14. python 2 版本中的input() 和 raw_input() 函数的比较
  15. Webpack+Babel+React环境搭建
  16. 将jpg格式转成PDF格式的转换器
  17. mac可装云服务器_转载一篇用苹果电脑mac系统配置阿里云服务器ecs的教程攻略
  18. 2022深圳中小学生学位补贴申报形式及流程
  19. 服务拆分理论和原理及方法
  20. [macOS]_[Shell]_[获取App的签名证书有效期]

热门文章

  1. 苹果和android充电线一根,非常实用!一根充电线,同时搞定苹果、安卓、Type-c,出门带它就够了...
  2. 已解决,软件V2报错 failed to read response header > websocket: close 1005 (no status)问题
  3. 系统平台运营热门店铺模式
  4. python一行输出多个数据_python 如何将一系列数字十个一行输出
  5. erp故障处理流程图_ERP仓储管理流程图
  6. n8_Visualizing Multivariate_sns_3D plot_matplotlib.dates_mpl_finance_aapl stock_EMA_RSI_Bollinger
  7. ros入门保姆级教程之召唤小乌龟
  8. Cesium加载GLB和GLTF模型文件踩坑实录
  9. C语言中的strstr函数
  10. Imitation Learning