迭代器模式
提供一種方法順序訪問一個聚合對象中的各個元素,而又不暴露其內部的表示java
有兩家餐廳,共有兩個菜單,兩家準備合併,每一個餐廳都有一個銷售系統,可是內部菜單的組成結構稍有不一樣,一個是用數組存儲菜單對象,一個是用集合存儲菜單對象數組
將兩個菜單合併顯示測試
數組菜單this
1 package com.iteratorPattern.arr; 2 3 import com.iteratorPattern.vo.MenuItem; 4 5 /** 6 * @program: test 7 * @description: 數組菜單 8 * @author: Mr.Yang 9 * @create: 2019-01-01 15:22 10 **/ 11 public class ArrMenu { 12 static final int MAX_ITEMS=3; 13 int items=0; 14 MenuItem[] menuItems; 15 public ArrMenu(){ 16 menuItems=new MenuItem[MAX_ITEMS]; 17 addItem("西紅柿雞蛋","有養分",10.00); 18 addItem("酸辣白菜","好吃",11.00); 19 addItem("酸辣土豆絲","特點菜",12.00); 20 } 21 22 private void addItem(String name,String description,Double price){ 23 MenuItem menuItem = new MenuItem(name, description, price); 24 if(items>=MAX_ITEMS){ 25 System.err.println("menu is full,Can't add item to menu"); 26 }else{ 27 menuItems[items]=menuItem; 28 items+=1; 29 } 30 } 31 32 public MenuItem[] getItems(){ 33 return menuItems; 34 } 35 36 }
list集合菜單spa
1 package com.iteratorPattern.list; 2 3 import com.iteratorPattern.vo.MenuItem; 4 5 import java.util.ArrayList; 6 7 /** 8 * @program: test 9 * @description: list集合菜單 10 * @author: Mr.Yang 11 * @create: 2019-01-01 15:32 12 **/ 13 public class ListMenu { 14 ArrayList<MenuItem> menuItems; 15 public ListMenu(){ 16 menuItems = new ArrayList<MenuItem>(); 17 addItem("紫菜雞蛋湯","有養分的湯",6.00); 18 addItem("豆腐腦","早餐必備",7.00); 19 addItem("魷魚湯","補充大腦",8.00); 20 } 21 22 23 private void addItem(String name,String description,Double price){ 24 MenuItem menuItem = new MenuItem(name, description, price); 25 menuItems.add(menuItem); 26 } 27 28 public ArrayList<MenuItem> getItems(){ 29 return menuItems; 30 } 31 }
當前合併測試類code
1 package com.iteratorPattern.test; 2 3 import com.iteratorPattern.arr.ArrMenu; 4 import com.iteratorPattern.list.ListMenu; 5 import com.iteratorPattern.vo.MenuItem; 6 7 import java.util.ArrayList; 8 9 /** 10 * @program: test 11 * @description: 測試1 12 * @author: Mr.Yang 13 * @create: 2019-01-01 15:38 14 **/ 15 public class TestOne { 16 //打印菜單--菜類和湯類,須要遍歷兩次,若是再有一個會變成三次,四次………… 17 public static void main(String[] args) { 18 ArrMenu arrMenu = new ArrMenu(); 19 MenuItem[] items = arrMenu.getItems(); 20 for (int i = 0; i < items.length; i++) { 21 MenuItem item = items[i]; 22 System.out.println("item name:"+item.getName()); 23 System.out.println("item description:"+item.getDescription()); 24 System.out.println("item price:"+item.getPrice()); 25 } 26 ListMenu listMenu = new ListMenu(); 27 ArrayList listMenuItems = listMenu.getItems(); 28 for (int i = 0; i < listMenuItems.size(); i++) { 29 MenuItem item = (MenuItem) listMenuItems.get(i); 30 System.out.println("item name:"+item.getName()); 31 System.out.println("item description:"+item.getDescription()); 32 System.out.println("item price:"+item.getPrice()); 33 } 34 } 35 }
建立迭代器對象
1 package com.iteratorPattern.Iterator; 2 3 /** 4 * @program: test 5 * @description: 迭代器接口 6 * @author: Mr.Yang 7 * @create: 2019-01-01 15:44 8 **/ 9 public interface Iterator { 10 boolean hasNext(); 11 Object next(); 12 }
添加數組遍歷方式的迭代器blog
1 package com.iteratorPattern.arr; 2 3 import com.iteratorPattern.Iterator.Iterator; 4 import com.iteratorPattern.vo.MenuItem; 5 6 /** 7 * @program: test 8 * @description: 數組遍歷方式迭代器 9 * @author: Mr.Yang 10 * @create: 2019-01-01 15:46 11 **/ 12 public class ArrMenuIterator implements Iterator { 13 MenuItem[] items; 14 int position=0; 15 public ArrMenuIterator(MenuItem[] items){ 16 this.items=items; 17 } 18 19 public boolean hasNext() { 20 if(position>=items.length || items[position]==null){ 21 return false; 22 } 23 return true; 24 } 25 26 public Object next() { 27 MenuItem item = items[position]; 28 position+=1; 29 return item; 30 } 31 }
數組菜單添加建立迭代器的方法接口
1 package com.iteratorPattern.arr; 2 3 import com.iteratorPattern.Iterator.Iterator; 4 import com.iteratorPattern.vo.MenuItem; 5 6 /** 7 * @program: test 8 * @description: 數組菜單 9 * @author: Mr.Yang 10 * @create: 2019-01-01 15:22 11 **/ 12 public class ArrMenu { 13 static final int MAX_ITEMS=3; 14 int items=0; 15 MenuItem[] menuItems; 16 public ArrMenu(){ 17 menuItems=new MenuItem[MAX_ITEMS]; 18 addItem("西紅柿雞蛋","有養分",10.00); 19 addItem("酸辣白菜","好吃",11.00); 20 addItem("酸辣土豆絲","特點菜",12.00); 21 } 22 23 private void addItem(String name,String description,Double price){ 24 MenuItem menuItem = new MenuItem(name, description, price); 25 if(items>=MAX_ITEMS){ 26 System.err.println("menu is full,Can't add item to menu"); 27 }else{ 28 menuItems[items]=menuItem; 29 items+=1; 30 } 31 } 32 33 public Iterator createIterator(){ 34 return new ArrMenuIterator(menuItems); 35 } 36 37 }
建立list迭代器ip
1 package com.iteratorPattern.list; 2 3 import com.iteratorPattern.Iterator.Iterator; 4 import com.iteratorPattern.vo.MenuItem; 5 6 import java.util.ArrayList; 7 8 /** 9 * @program: test 10 * @description: list迭代器 11 * @author: Mr.Yang 12 * @create: 2019-01-01 16:12 13 **/ 14 public class ListMenuIterator implements Iterator { 15 ArrayList listMenus; 16 int position=0; 17 public ListMenuIterator(ArrayList listMenus){ 18 this.listMenus=listMenus; 19 } 20 21 public boolean hasNext() { 22 if(position>=listMenus.size() || listMenus.get(position)==null){ 23 return false; 24 } 25 return true; 26 } 27 28 public Object next() { 29 MenuItem menuItem = (MenuItem) listMenus.get(position); 30 position+=1; 31 return menuItem; 32 } 33 }
list集合菜單添加建立迭代器的方法
1 package com.iteratorPattern.list; 2 3 import com.iteratorPattern.Iterator.Iterator; 4 import com.iteratorPattern.vo.MenuItem; 5 6 import java.util.ArrayList; 7 8 /** 9 * @program: test 10 * @description: list集合菜單 11 * @author: Mr.Yang 12 * @create: 2019-01-01 15:32 13 **/ 14 public class ListMenu { 15 ArrayList<MenuItem> menuItems; 16 public ListMenu(){ 17 menuItems = new ArrayList<MenuItem>(); 18 addItem("紫菜雞蛋湯","有養分的湯",6.00); 19 addItem("豆腐腦","早餐必備",7.00); 20 addItem("魷魚湯","補充大腦",8.00); 21 } 22 23 24 private void addItem(String name,String description,Double price){ 25 MenuItem menuItem = new MenuItem(name, description, price); 26 menuItems.add(menuItem); 27 } 28 public Iterator createIterator(){ 29 return new ListMenuIterator(menuItems); 30 } 31 }
測試
1 package com.iteratorPattern.test; 2 3 import com.iteratorPattern.Iterator.Iterator; 4 import com.iteratorPattern.arr.ArrMenu; 5 import com.iteratorPattern.list.ListMenu; 6 import com.iteratorPattern.vo.MenuItem; 7 8 /** 9 * @program: test 10 * @description: 測試2 11 * @author: Mr.Yang 12 * @create: 2019-01-01 16:17 13 **/ 14 public class TestTwo { 15 public static void main(String[] args) { 16 ArrMenu arrMenu = new ArrMenu(); 17 ListMenu listMenu = new ListMenu(); 18 System.out.println("---------------數組 菜類---------------"); 19 printMenu(arrMenu.createIterator()); 20 System.out.println("---------------集合 湯類---------------"); 21 printMenu(listMenu.createIterator()); 22 } 23 private static void printMenu(Iterator iterator){ 24 while (iterator.hasNext()){ 25 MenuItem next = (MenuItem) iterator.next(); 26 System.out.println("menu name is :"+next.getName()); 27 System.out.println("menu description is :"+next.getDescription()); 28 System.out.println("menu price is :"+next.getPrice()); 29 } 30 } 31 }
測試結果
1 ---------------數組 菜類--------------- 2 menu name is :西紅柿雞蛋 3 menu description is :有養分 4 menu price is :10.0 5 menu name is :酸辣白菜 6 menu description is :好吃 7 menu price is :11.0 8 menu name is :酸辣土豆絲 9 menu description is :特點菜 10 menu price is :12.0 11 ---------------集合 湯類--------------- 12 menu name is :紫菜雞蛋湯 13 menu description is :有養分的湯 14 menu price is :6.0 15 menu name is :豆腐腦 16 menu description is :早餐必備 17 menu price is :7.0 18 menu name is :魷魚湯 19 menu description is :補充大腦 20 menu price is :8.0
此處不用java.util下面的迭代器是爲了更好的瞭解迭代器是如何工做的,java.util比咱們所實現的迭代器多了一個remove方法,若是有其餘需求,自定義的迭代器相比於java.util更易拓展。