【设计模式】迭代器模式

定义:迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

 1 package cn.sp.test1;
 2 
 3 
 4 /**
 5  * 菜单类
 6  * Created by 2YSP on 2017/7/14.
 7  */
 8 public class MenuItem {
 9     private String name;
10     private String description;
11     private boolean vegetarian;
12     private double price;
13     public MenuItem(String name,String description,boolean vegetarian,double price){
14         this.name = name;
15         this.description = description;
16         this.vegetarian = vegetarian;
17         this.price = price;
18     }
19 
20     public String getName() {
21         return name;
22     }
23 
24     public void setName(String name) {
25         this.name = name;
26     }
27 
28     public String getDescription() {
29         return description;
30     }
31 
32     public void setDescription(String description) {
33         this.description = description;
34     }
35 
36     public boolean isVegetarian() {
37         return vegetarian;
38     }
39 
40     public void setVegetarian(boolean vegetarian) {
41         this.vegetarian = vegetarian;
42     }
43 
44     public double getPrice() {
45         return price;
46     }
47 
48     public void setPrice(double price) {
49         this.price = price;
50     }
51 
52     @Override
53     public String toString() {
54         return "MenuItem{" +
55                 "name='" + name + '\'' +
56                 ", description='" + description + '\'' +
57                 ", vegetarian=" + vegetarian +
58                 ", price=" + price +
59                 '}';
60     }
61 }
 1 package cn.sp.test1;
 2 
 3 import java.util.Iterator;
 4 
 5 /**
 6  * Created by 2YSP on 2017/7/14.
 7  */
 8 public class DinnerMenu {
 9     static final  int MAX_ITEMS = 6;
10     int numberOfItems = 0;
11     MenuItem[] menuItems;
12 
13     public DinnerMenu(){
14         menuItems = new MenuItem[MAX_ITEMS];
15         addItem("Vegetarian BLT","bacon ",true,2.99);
16         addItem("BLT","Bacon with lettuce & tomato on whole wheat",false,2.99);
17         addItem("Soup of the day","with a side of potato salad",false,3.29);
18     }
19 
20     public void addItem(String name,String description,boolean vegetarian,double price){
21         MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
22         if (numberOfItems >= MAX_ITEMS){
23             System.err.println("Sorry,menu is full!Can't add item to menu");
24         }else{
25             menuItems[numberOfItems] = menuItem;
26             numberOfItems = numberOfItems + 1;
27         }
28     }
29 
30 //    public MenuItem[] getMenuItems(){不再需要
31 //        return menuItems;
32 //    }
33 
34     public Iterator createIterator(){
35         return  new DinnerMenuIterator(menuItems);
36     }
37 }
 1 package cn.sp.test1;
 2 
 3 import java.util.Iterator;
 4 
 5 /**
 6  * Created by 2YSP on 2017/7/14.
 7  */
 8 public class DinnerMenuIterator implements Iterator {
 9     MenuItem[] menuItems;
10     int position = 0;//记录当前位置
11 
12     public DinnerMenuIterator(MenuItem[] items){
13         this.menuItems = items;
14     }
15 
16     public Object next(){
17         MenuItem menuItem = menuItems[position];
18         position = position + 1;
19         return  menuItem;
20     }
21 
22     public boolean hasNext(){
23         if(position > menuItems.length || menuItems[position] == null){//不但要检查是否超过数组长度,还要检查下一项是否为Null
24             return  false;
25         }else {
26             return  true;
27         }
28     }
29 
30     public void remove(){
31         if (position <= 0){
32             throw  new IllegalStateException("you can't remove an item util you have done at least one next");
33         }
34 
35         if (menuItems[position-1] != null ){
36             for(int i = position-1;i < (menuItems.length -1);i++){
37                 menuItems[i] = menuItems[i+1];
38             }
39             menuItems[menuItems.length-1] = null;
40         }
41     }
42 }
 1 package cn.sp.test1;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 
 6 /**
 7  * Created by 2YSP on 2017/7/14.
 8  */
 9 public class PancakeHouseMenu {
10     ArrayList menuItems;
11 
12     public PancakeHouseMenu(){
13         menuItems = new ArrayList();
14         addItem("ddf","Pancakes with scrambled eggs,and toast",true,2.99);
15         addItem("Regular ","Pancakes with fried eggs,sausage",false,2.99);
16         addItem("Blueberry","Pancakes made with fresh blueberries",true,3.49);
17     }
18 
19     public void addItem(String name,String description,boolean vegetarian,double price){
20         MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
21         menuItems.add(menuItem);
22     }
23 
24 //    public ArrayList getMenuItems(){
25 //        return menuItems;
26 //    }
27 
28     public Iterator createIterator(){
29 //        return  new PancakeHouseMenuIterator(menuItems);
30         return  menuItems.iterator();//ArrayList有迭代器
31     }
32 }
 1 package cn.sp.test1;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 
 6 /**
 7  * Created by 2YSP on 2017/7/14.
 8  */
 9 public class PancakeHouseMenuIterator implements Iterator {
10     ArrayList menuItems;
11     int position = 0;
12 
13     public  PancakeHouseMenuIterator(ArrayList items){
14         menuItems = items;
15     }
16 
17     public Object next(){
18         Object obj = menuItems.get(position);
19         position = position + 1;
20         return obj;
21     }
22 
23     public boolean hasNext(){
24         if(position >= menuItems.size() || menuItems.get(position) == null){
25             return  false;
26         }else{
27             return  true;
28         }
29     }
30 }
 1 package cn.sp.test1;
 2 
 3 import java.util.Iterator;
 4 
 5 /**
 6  * Created by 2YSP on 2017/7/14.
 7  */
 8 public class Test {
 9     public static void main(String[] args) {
10         PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
11         DinnerMenu dinnerMenu = new DinnerMenu();
12         Iterator it1 = pancakeHouseMenu.createIterator();
13         while (it1.hasNext()){
14             System.out.println(((MenuItem)it1.next()).toString());
15         }
16         System.out.println("===================");
17         Iterator it2 = dinnerMenu.createIterator();
18         while (it2.hasNext()){
19             System.out.println(((MenuItem)it2.next()).toString());
20         }
21     }
22 }

运行结果:

MenuItem{name='ddf', description='Pancakes with scrambled eggs,and toast', vegetarian=true, price=2.99}
MenuItem{name='Regular ', description='Pancakes with fried eggs,sausage', vegetarian=false, price=2.99}
MenuItem{name='Blueberry', description='Pancakes made with fresh blueberries', vegetarian=true, price=3.49}
===================
MenuItem{name='Vegetarian BLT', description='bacon ', vegetarian=true, price=2.99}
MenuItem{name='BLT', description='Bacon with lettuce & tomato on whole wheat', vegetarian=false, price=2.99}
MenuItem{name='Soup of the day', description='with a side of potato salad', vegetarian=false, price=3.29}

posted @ 2017-07-14 11:18  烟味i  阅读(175)  评论(0编辑  收藏  举报