Iterator 迭代器模式.ppt
Iterator 迭代器,特大喜讯,Pancake House(薄烤饼屋)早餐店和Diner(用餐者)午餐店合并了,您现在可以在一个地方享受到美味的早餐和午餐。,哥们!我想用ArrayList列我的Pancake House为早餐菜单,Diner为你的午餐菜单,如何?,靠!我不干,我喜欢用Array,两份菜列表,实现很简单三个类搞定,MenuItem类Public calss MenuItemString name;String description;Boolean vegetarian;/是否素食的double price;Public MenuItem(String name,String description,boolean vegetarian,double price)/所有属性有get方法,PancakeHouse的菜单Public class PancakeHouseMenuArrayList menuItems=new ArrayList();Public PancakeHouse()addItem(“K,增加菜单项,菜单项增加到列表中,返回菜单列表,DinerMenu菜单Public class DinerMenu(static final int MAX_ITEMS=6;int numberOfItems=0;MenuItem menuItems=new MenuItemMAX_ITEMS;Public DinerMenu()addItem(“Vegetarian BLT”,”Bacon with lettuce,如果你是服务员,你是如何展现这两份不同的菜单?并且还能说明菜单项是否是蔬食类。产生一个服务员类,其方法如下:printMenu():打印菜单上的每一列printBreakfastMenu():打印早餐列printLunchMenu():打印午餐列printVegetarianMenu():打印所有蔬食类列isItemVegetarian(name):根据名字得到其是否是蔬食类的列,可以列出菜单了一、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);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();现在我们不得不执行两次不同的循坏,如果我们创建一个对象,把每一列数据存入这个对象中,然后通过循环这个对取得每一个列,是不是可以呢?用早餐菜单做个列子,如下:Iterator iterator=breakfastMenu.createIterator();While(iterator.hasNext()MenuItem menuItem=(MenuItem)iterator.next();,形成一个接口:Iterator 有两个方法如下:hasNext():判断集合是否有下一个元素Next():返回集合里的下一个对象一但有了此接口,我们就能执行各种容器对象:arrays,lists,hashtable.爽,类关系图,具体执行循环的类,初始化数据等功能,DinerMenuIterator类Public class DinerMenuIterator implments IteratorMenuItem 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(position=items.length|itemsposition=null)return false;else return true;,DineMenu类Public class DineMenustatic final int MAX_ITEMS=6;int numberOfItems=0;MenuItem menuItems;。public MenuItem getMenuItems()return menuItems;Public Iterator createIterator()return new DineMenuIterator(menuItems);,删除不用了,返回Iterator接口,客户端不需要知道DinerMenuIterator是如何执行的,Waitress类Public class WaitressPancakeHouseMenu pancakeHouseMenu;DinerMenu dinerMenu;public Waitress(PancakeHouseMenu;pancakeHouseMenu,DinerMenu dinerMenu)this.pancakeHouseMenu=pancakeHouseMenu;this.dinerMenu=dinerMenu;Public 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)while(iterator.hasNext()MenuItem menuItem=(MenuItem)iterator.next();System.out.println(menuItem.getName();如何使用?如下图,什么是迭代模式:迭代模式可以顺序访问一个聚集中的元素而不必暴露聚集的内部表象。多个对象聚在一起形成的总体称之为聚集,聚集对象是能够包容一组对象的容器对象。迭代模式将迭代逻辑封装到一个独立的子对象中,从而与聚集本身隔开。迭代子模式简化了聚集的界面。每一个聚集对象都可以有一个或一个以上的迭代子对象,每一个迭代子的迭代状态可以是彼此独立的。迭代算法可以独立于聚集角色变化。,看看上面的类图,是否有可改之处?如果增加一个晚餐(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 objects uniformly.允许你组合对象到树形结构中,可以用整体或部分层次表现。组合物让客户交涉单独的对象和对象的成份,复合类图如下,Client使用component接口去操作复合对象,Component在所有对象(复合和叶子结点)中定义,叶子定义的复合元素行为,它通过执行operator方法达到复合的支持,复合的角色是定义具有孩子和存贮孩子结点的行为,采用复合模式构思我们的采单,MenuComponent类Public abstract class MenuComponentpublic void add(MenuComponent menuComponent)throw 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 new UnsupporteOperationExcetion();public boolean isVegetarian()throw new UnsupporteOperationExcetion();public void print()throw new UnsupporteOperationExcetion();,Operation方法,MenuItemPublic class MenuItem extends MenuComponentstring name;String description;boolean vegetarian;double price;Public 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()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;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 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.out.print(“”+getName();System.out.print(“-”+getDescription();System.out.print(“-“);while(iterator.hasNext()MenuComponent menuComponent=(MenuComponent)iterator.next();menuComponent.print();,Waitress类Public class WaitressMenuComponent allMenus;public Waitress(MenuComponent allMenus)this.allMenus=allMenus;public void printMenu()allMenus.print();,测试类,