常用設計模式——迭代器模式

迭代器模式

概念

提供一種方法順序訪問一個集合中的各個元素,而又不暴露其內部實現。

image-20190608130721598

示例

演示了迭代器模式,遍歷餐廳菜單的例子

/**
 * 菜單項
 * @author huangy on 2019-06-07
 */
public class MenuItem {

    private String name;

    private String description;

    private boolean vegetarian;

    private 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 void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public void setVegetarian(boolean vegetarian) {
        this.vegetarian = vegetarian;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "MenuItem{" +
                "name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", vegetarian=" + vegetarian +
                ", price=" + price +
                '}';
    }
}
/**
 * 餐廳菜單
 * @author huangy on 2019-06-07
 */
public class DinerMenu {

    private MenuItem[] menuItems;

    {
        menuItems = new MenuItem[3];
        menuItems[0] = new MenuItem("name1", "desc1", true, 1);
        menuItems[1] = new MenuItem("name2", "desc2", false, 2);
        menuItems[2] = new MenuItem("name3", "desc3", true, 3);
    }

    /**
     * 返回迭代器接口
     * (1)客戶不需要知道餐廳菜單是如何維護菜單項的,也不需要知道迭代器是如何實現的。
     * 客戶只需要使用這個迭代器遍歷元素就可以了。
     */
    public Iterator createIterator() {
        return new DinerMenuIterator(menuItems);
    }
}
/**
 * 菜單項迭代器
 * @author huangy on 2019-06-07
 */
public class DinerMenuIterator implements Iterator {

    MenuItem[] items;

    int 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() {
        return ((position < items.length) && (items[position] != null));
    }
}
/**
 * 迭代器接口
 * @author huangy on 2019-06-07
 */
public interface Iterator {

    boolean hasNext();

    Object next();
}
/**
 * 女招待員
 * @author huangy on 2019-06-08
 */
public class Waitress {

    DinerMenu dinerMenu;

    public Waitress(DinerMenu dinerMenu) {
        this.dinerMenu = dinerMenu;
    }

    public void printMenu() {
        Iterator iterator = dinerMenu.createIterator();

        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
/**
 * @author huangy on 2019-06-08
 */
public class DemoTest {

    public static void main(String[] args) {
        DinerMenu dinerMenu = new DinerMenu();
        Waitress waitress = new Waitress(dinerMenu);

        waitress.printMenu();
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章