当前位置:编程学习 > JAVA >>

Head First设计模式-迭代器模式

一、整体代码
       Iterator.java
[java]  
public inte易做图ce Iterator {  
    boolean hasNext();  
    Object next();  
}  
 
      DinerMenuIterator.java
[java]  
public class DinerMenuIterator implements Iterator {  
    MenuItem[] items;  
    int position = 0;  
   
    public DinerMenuIterator(MenuItem[] items) {  
        this.items = items;  
    }  
   
    public Object next() {  
        MenuItem menuItem = items[position];  
        position = position + 1;  
        return menuItem;  
    }  
   
    public boolean hasNext() {  
        if (position >= items.length || items[position] == null) {  
            return false;  
        } else {  
            return true;  
        }  
    }  
}  
 
       PancakeHouseMenuIterator.java
[java]  
import java.util.ArrayList;  
  
public class PancakeHouseMenuIterator implements Iterator {  
    private ArrayList items;  
    private int position = 0;  
   
    public PancakeHouseMenuIterator(ArrayList items) {  
        this.items = items;  
    }  
   
    public Object next() {  
        Object object = items.get(position);  
        position = position + 1;  
        return object;  
    }  
   
    public boolean hasNext() {  
        if (position >= items.size()) {  
            return false;  
        } else {  
            return true;  
        }  
    }  
}  
 
      DinerMenu.java
[java]  
public class DinerMenu  {  
    private static final int MAX_ITEMS = 6;  
    private int numberOfItems = 0;  
    private MenuItem[] menuItems;  
    
    public DinerMenu() {  
        menuItems = new MenuItem[MAX_ITEMS];  
   
        addItem("Vegetarian BLT",  
            "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);  
        addItem("BLT",  
            "Bacon with lettuce & tomato on whole wheat", false, 2.99);  
        addItem("Soup of the day",  
            "Soup of the day, with a side of potato salad", false, 3.29);  
        addItem("Hotdog",  
            "A hot dog, with saurkraut, relish, onions, topped with cheese",  
            false, 3.05);  
        addItem("Steamed Veggies and Brown Rice",  
            "Steamed vegetables over brown rice", true, 3.99);  
        addItem("Pasta",  
            "Spaghetti with Marinara Sauce, and a slice of sourdough bread",  
            true, 3.89);  
    }  
    
    public void addItem(String name, String description,   
                         boolean vegetarian, double price)   
    {  
        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);  
        if (numberOfItems >= MAX_ITEMS) {  
            System.err.println("Sorry, menu is full!  Can't add item to menu");  
        } else {  
            menuItems[numberOfItems] = menuItem;  
            numberOfItems = numberOfItems + 1;  
        }  
    }  
   
    
    public Iterator createIterator() {  
        return new DinerMenuIterator(menuItems);  
    }  
   
    // other menu methods here  
}  
 
      PancakeHouseMenu.java
[java] 
import java.util.ArrayList;  
  
public class PancakeHouseMenu {  
    private ArrayList menuItems;  
   
    public PancakeHouseMenu() {  
        menuItems = new ArrayList();  
      
        addItem("K&B's Pancake Breakfast",   
            "Pancakes with scrambled eggs, and toast",   
            true,  
            2.99);  
   
        addItem("Regular Pancake Breakfast",   
            "Pancakes with fried eggs, sausage",   
            false,  
            2.99);  
   
        addItem("Blueberry Pancakes",  
            "Pancakes made with fresh blueberries",  
            true,  
            3.49);  
   
        addItem("Waffles",  
            "Waffles, with your choice of blue
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,