Iterator 迭代器模式.ppt

上传人:来看看 文档编号:5019461 上传时间:2020-01-29 格式:PPT 页数:34 大小:2.22MB
返回 下载 相关 举报
Iterator 迭代器模式.ppt_第1页
第1页 / 共34页
Iterator 迭代器模式.ppt_第2页
第2页 / 共34页
Iterator 迭代器模式.ppt_第3页
第3页 / 共34页
Iterator 迭代器模式.ppt_第4页
第4页 / 共34页
Iterator 迭代器模式.ppt_第5页
第5页 / 共34页
点击查看更多>>
资源描述

《Iterator 迭代器模式.ppt》由会员分享,可在线阅读,更多相关《Iterator 迭代器模式.ppt(34页珍藏版)》请在三一文库上搜索。

1、Iterator 迭代器,特大喜讯,Pancake House(薄烤饼屋)早餐店和Diner(用餐者)午餐店合并了,您现在可以在一个地方享受到美味的早餐和午餐。,哥们!我想用ArrayList列我的Pancake House为早餐菜单, Diner为你的午餐菜单,如何?,靠!我不干,我喜欢用Array,两份菜列表,实现很简单三个类搞定,MenuItem类 Public calss MenuItem String name; String description; Boolean vegetarian;/是否素食的 double price; Public MenuItem(String nam

2、e,String description,boolean vegetarian,double price) /所有属性有get方法 ,PancakeHouse的菜单 Public class PancakeHouseMenu ArrayList menuItems = new ArrayList(); Public PancakeHouse() addItem(“K ,增加菜单项,菜单项增加到列表中,返回菜单列表,DinerMenu菜单 Public class DinerMenu( static final int MAX_ITEMS=6; int numberOfItems = 0; Me

3、nuItem menuItems = new MenuItemMAX_ITEMS; Public DinerMenu() addItem(“Vegetarian BLT”,”Bacon with lettuce ,如果你是服务员,你是如何展现这两份不同的菜单?并且还能说明菜单项是否是蔬食类。 产生一个服务员类,其方法如下: printMenu():打印菜单上的每一列 printBreakfastMenu():打印早餐列 printLunchMenu():打印午餐列 printVegetarianMenu():打印所有蔬食类列 isItemVegetarian(name):根据名字得到其是否是蔬

4、食类的列,可以列出菜单了 一、 PancakeHouseMenu pancake=new pancakeHouseMenu(); ArrayList breakfastItems=pancake.getMenuItems(); DinerMenu dine = new DineerMenu(); MenuItem lunchItems=dinerMenu.getMenuItems(); 注意:它返回的类型不同,二、 For(int i=0; ibreakfastItems.size(); i+) MenuItem menuItem=(MenuItem)breakfastItems.get(i)

5、; System.out.print(menuItem.getName() + “ ” + MenuItem.getPrice() + “ ” +MenuItem.getDescription(); For(int i=0; ilunchItems.size(); i+) MenuItem menuItem=lunchItemsi; System.out.print(menuItem.getName() + “ ” + MenuItem.getPrice() + “ ” +MenuItem.getDescription(); 现在我们不得不执行两次不同的循坏,如果我们创建一个对象,把每一列数据

6、存 入这个对象中,然后通过循环这个对取得 每一个列,是不是可以呢? 用早餐菜单做个列子,如下: Iterator iterator = breakfastMenu.createIterator(); While(iterator.hasNext() MenuItem menuItem = (MenuItem)iterator.next(); ,形成一个接口: Iterator 有两个方法如下: hasNext():判断集合是否有下一个元素 Next():返回集合里的下一个对象 一但有了此接口,我们就能执行各种容器 对象:arrays,lists,hashtable. 爽,类关系图,具体执行循环

7、的类,初始化数据等功能,DinerMenuIterator类 Public class DinerMenuIterator implments Iterator MenuItem items; Int position =0; Public DinerMenuIterator(MenuItem items) this.items = items; Public Object next() MenuItem menuItem = itemsposition; position = position + 1; return menuItem; Public boolean hasNext() if

8、(position = items.length | itemsposition = null) return false; else return true; ,DineMenu类 Public class DineMenu static final int MAX_ITEMS = 6; int numberOfItems = 0 ; MenuItem menuItems; 。 public MenuItem getMenuItems() return menuItems; Public Iterator createIterator() return new DineMenuIterato

9、r(menuItems); ,删除不用了,返回Iterator接口,客户端不需要知道DinerMenuIterator是如何执行的,Waitress类 Public class Waitress PancakeHouseMenu pancakeHouseMenu; DinerMenu dinerMenu; public Waitress(PancakeHouseMenu; pancakeHouseMenu,DinerMenu dinerMenu) this. pancakeHouseMenu = pancakeHouseMenu; this.dinerMenu = dinerMenu; Pub

10、lic void printMenu() Iterator pancakeIterator = pancakeHouseMenu.createIterator(); Iterator dinerMenuIterator = dinerMenu.createIterator(); System.out.println(“Menun-nBreakfast”); printMenu(PancakeIterator); System.out.println(“Lunch”); printMenu(dinerMenu); ,Private void printMenu(Iterator iterator

11、) while(iterator.hasNext() MenuItem menuItem = (MenuItem)iterator.next(); System.out.println(menuItem.getName(); 如何使用?如下图,什么是迭代模式: 迭代模式可以顺序访问一个聚集中的元素而不必暴露聚集的内部表象。多个对象聚在一起形成的总体称之为聚集,聚集对象是能够包容一组对象的容器对象。迭代模式将迭代逻辑封装到一个独立的子对象中,从而与聚集本身隔开。迭代子模式简化了聚集的界面。每一个聚集对象都可以有一个或一个以上的迭代子对象,每一个迭代子的迭代状态可以是彼此独立的。迭代算法可以独立于

12、聚集角色变化。,看看上面的类图,是否有可改之处? 如果增加一个晚餐(supper),就需要重改waitress的构造函数,是不是我们可以重构此构造函数?,有相的方法 可以提出一个接口,如果客人在午餐后需要小甜点,这时我们的菜单该是什么样子的?,Composite Pattern(复合模式) Allows you to compose object into tree structures to represent part-whole hierarchies. Composite lets clients treat individual object and compositions of

13、objects uniformly. 允许你组合对象到树形结构中,可以用整体或部分层次表现。组合物让客户交涉单独的对象和对象的成份,复合类图如下,Client使用component接口去操作复合对象,Component在所有对象(复合和叶子结点)中定义,叶子定义的复合元素行为,它通过执行operator方法达到复合的支持,复合的角色是定义具有孩子和存贮孩子结点的行为,采用复合模式构思我们的采单,MenuComponent类 Public abstract class MenuComponent public void add(MenuComponent menuComponent) throw

14、 new UnsupporteOperationExcetion(); public void remove(MenuComponent menuComponent) throw new UnsupporteOperationExcetion(); public MenuComponent getChild(int i) throw new UnsupporteOperationExcetion(); public String getName() throw new UnsupporteOperationExcetion(); public double getPrice() throw n

15、ew UnsupporteOperationExcetion(); public boolean isVegetarian() throw new UnsupporteOperationExcetion(); public void print() throw new UnsupporteOperationExcetion();,Operation方法,MenuItem Public class MenuItem extends MenuComponent string name; String description; boolean vegetarian; double price; Pu

16、blic MenuItem(String name,String description,boolean vegetarian,double price) this.name = name; this.description = description; this.price = price; Public String getName()return name; Public String getDescription()return description; Public double getPrice()return price; Public boolean isVegtarian()

17、return vegetarian;,Public void print() System.out.print(“ ” + getName(); if(isVegetarian() System.out.print(“ (v)”); System.out.print(“ ” + getPrice(); System.out.print(“ - ” + getDescription(); ,Menu类 Public class meun extends MenuComponent() ArrayList menuComponents = new ArrayList(); String name;

18、 String description; public Menu(String name,String descrption) this.name=name; this.description = description; Public void add(MenuComponent menuComponent) menuComponents .add(menuComponent); Public void remove(MenuComponent menuComponent) menuComponents.remove(menuComponent); Public MenuComponent

19、getChild(int i) return (MenuComponents) menuComponents.get(i); Public String getName()return name; Public String getDescription()return description; Public void print() System.out.print(“ ” + getName(); System.out.print(“ - ” + getDescription(); ,Menu类的print方法好象没什么特别 如果我们做如下处理 Public print() System.

20、out.print(“ ” + getName(); System.out.print(“ - ” + getDescription(); System.out.print(“ -“); while(iterator.hasNext() MenuComponent menuComponent = (MenuComponent)iterator.next(); menuComponent.print(); ,Waitress类 Public class Waitress MenuComponent allMenus; public Waitress(MenuComponent allMenus) this.allMenus = allMenus; public void printMenu() allMenus.print(); ,测试类,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 研究报告 > 商业贸易


经营许可证编号:宁ICP备18001539号-1