Head First设计模式-迭代器

设计模式的代码仓库:
设计模式
在这里插入图片描述
一个类应该只有一个引起变化的原因
高内聚

优点:
OCP

缺点:
类成对增加

public interface Iterator{
    boolean hasNext();  //返回一个布尔值,判断是否还有更多的元素
    Object next();   //返回下一个元素
}
//实现迭代器接口  PancakeHouseMenu
public class PancakeHouseMenuIterator implements Iterator {
    ArrayList<MenuItem> items;
    int position = 0;   //position记录当前数组遍历的位置

    public PancakeHouseMenuIterator(ArrayList items) {  //构造器需要被传入一个菜单项的数组当作参数
        this.items = items;
    }

    @Override
    public Object next() {  //返回数组内的下一项,并递增其位置
        MenuItem menuItem = items.get(position);
        position = position + 1;
        return menuItem;
    }

    @Override
    public boolean hasNext() {
        if (position >= items.size() || items.get(position) == null) {
            return false;
        } else {
            return true;
        }
    }
}
public class DinerMenuIterator implements Iterator {
    MenuItem[] items;
    int position = 0;   //position记录当前数组遍历的位置

    public DinerMenuIterator(MenuItem[] items) {  //构造器需要被传入一个菜单项的数组当作参数
        this.items = items;
    }

    @Override
    public Object next() {  //返回数组内的下一项,并递增其位置
        MenuItem menuItem = items[position];
        position = position + 1;
        return menuItem;
    }

    @Override
    public boolean hasNext() {
    /*检查是否已经取得数组内所有的元素,如果还有元素待遍历则返回true;
     由于使用的是固定长度的数组,所以不但要检查是否超出了数组长度,也必须检查是否下一项是null,如果是null,就没有其他项了
    */
        if (position >= items.length || items[position] == null) {
            return false;
        } else {
            return true;
        }
    }
}
public class MenuItem {
    String name;
    String description;
    boolean vegetarian;
    double price;

    public MenuItem(String name, String description, boolean vegetarian, double price) {
        this.name = name;
        this.description = description;
        this.vegetarian = vegetarian;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public double getprice() {
        return price;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }
}
public class DinerMenu {
    static final int MAX_ITEMS = 6;
    int numberOfItems = 0;
    MenuItem[] menuItems;

    public DinerMenu() {
        menuItems = new MenuItem[MAX_ITEMS];

        addItem("Vegetarian BLT", "1111111111111", true, 2.99);
        addItem("BLT", "222222222222", false, 2.99);
        addItem("Soup of the day", "2222222222222", false, 3.29);
        addItem("Hotdog", "33333333333333", false, 3.05);
        //继续加入其他项
    }

    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);
    }
    //菜单的其他方法。。。。
}

public class PancakeHouseMenu {
    ArrayList menuItems;

    public PancakeHouseMenu() {
        menuItems = new ArrayList();
        addItem("K&B's Pancake Breakfast", "1111111111111", true, 2.99);
        addItem("Regular Pancake Breakfast", "222222222222", false, 2.99);
        addItem("Blueberry Pancakes", "2222222222222", true, 3.29);
        addItem("Waffles", "33333333333333", true, 3.05);
    }

    public void addItem(String name, String description, boolean vegetarian, double price) {
        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
        menuItems.add(menuItem);
    }

    public Iterator createIterator() {
        return new PancakeHouseMenuIterator(menuItems);
    }
    //其他方法
}

public class Waitress {
    PancakeHouseMenu pancakeHouseMenu;
    DinerMenu dinerMenu;

    //在构造器中,女招待照顾两个菜单
    public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {
        this.pancakeHouseMenu = pancakeHouseMenu;
        this.dinerMenu = dinerMenu;
    }

    public void printMenu() {
        Iterator pancakeIterator = pancakeHouseMenu.createIterator();
        Iterator dinerIterator = dinerMenu.createIterator();
        System.out.println("Menu\n-------------\nBREAKFAST");
        printMenu(pancakeIterator);
        System.out.println("\nLUNCH");
        printMenu(dinerIterator);
    }

    private void printMenu(Iterator iterator) { //重载printMenu方法,使用迭代器遍历菜单项并打印
        while (iterator.hasNext()) {
            MenuItem menuItem = (MenuItem) iterator.next();
            System.out.print(menuItem.getName() + ",");
            System.out.print(menuItem.getprice() + "--");
            System.out.println(menuItem.getDescription());
        }
    }
    //其他方法
}
public class MenuTestDriver {
    public static void main(String args[]) {
        PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
        DinerMenu dinerMenu = new DinerMenu();
        Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);
        waitress.printMenu();
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页