{"id":1080,"date":"2024-05-29T17:14:19","date_gmt":"2024-05-29T09:14:19","guid":{"rendered":"http:\/\/www.spiceandwolf.com.cn\/?p=1080"},"modified":"2024-09-15T10:06:35","modified_gmt":"2024-09-15T02:06:35","slug":"design-pattern-iterator-pattern","status":"publish","type":"post","link":"http:\/\/www.spiceandwolf.com.cn\/index.php\/design-pattern-iterator-pattern\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f-\u8fed\u4ee3\u5668\u6a21\u5f0f"},"content":{"rendered":"\n<p>\u8fed\u4ee3\u5668\u6a21\u5f0f\u63d0\u4f9b\u4e00\u79cd\u65b9\u6cd5\u987a\u5e8f\u8bbf\u95ee\u4e00\u4e2a\u805a\u5408\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\uff0c\u800c\u53c8\u4e0d\u66b4\u9732\u5176\u5185\u90e8\u8868\u793a\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u6709\u9910\u5385\u548c\u714e\u997c\u5c4b\u5b9e\u4f53\uff0c\u5728\u7cfb\u7edf\u4e2d\u4ed6\u4eec\u7684\u6570\u636e\u5b58\u50a8\u7ed3\u6784\u4e0d\u540c\uff0c\u73b0\u5728\u8981\u540c\u65f6\u904d\u5386\u4ed6\u4eec\uff0c\u5982\u4f55\u624d\u80fd\u505a\u5230\u7b80\u5355\u660e\u4e86\u3002<\/p>\n\n\n\n<p>\u8fd9\u662f\u521d\u59cb\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MenuItem {\n    String name;\n    String description;\n    boolean vegetarian;\n    double price;\n\n    public MenuItem(String name, String description, boolean vegetarian, double price) {\n        this.name = name;\n        this.description = description;\n        this.vegetarian = vegetarian;\n        this.price = price;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public String getDescription() {\n        return description;\n    }\n\n    public double getPrice() {\n        return price;\n    }\n\n    public boolean isVegetarian() {\n        return vegetarian;\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class PancakeHouseMenu {\n    List&lt;MenuItem> menuItems = new ArrayList&lt;>();\n\n    public PancakeHouseMenu() {\n        menuItems = new ArrayList();\n\n        addItem(\"K&amp;B's Pancake Breakfast\",\n                \"Pancakes with scrambled eggs, and toast\",\n                true,\n                2.99);\n\n        addItem(\"Regular Pancake Breakfast\",\n                \"Pancakes with fried eggs, sausage\",\n                false,\n                2.99);\n\n        addItem(\"Blueberry Pancakes\",\n                \"Pancakes made with gresh blueberries\",\n                true,\n                3.49);\n\n        addItem(\"Waffles\",\n                \"Waffles, with your choice of blueberries or strawberries\",\n                true,\n                3.59);\n    }\n\n    public void addItem(String name,\n                        String description,\n                        boolean vegetarian,\n                        double price) {\n        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);\n        menuItems.add(menuItem);\n    }\n\n    public List&lt;MenuItem> getMenuItems() {\n        return menuItems;\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DinerMenu {\n    static final int MAX_ITEMS = 6;\n    int numberOfItems = 0;\n    MenuItem&#91;] menuItems;\n\n    public DinerMenu() {\n        menuItems = new MenuItem&#91;MAX_ITEMS];\n\n        addItem(\"Vegetarian BLT\",\n                \"(Faking') Bacon with lettuce &amp; tomato on whole wheat\",\n                true,\n                2.99);\n        addItem(\"BLT\",\n                \"Bacon with lettuce &amp; tomato on whole wheat\",\n                false,\n                3.29);\n        addItem(\"Hot dog\",\n                \"A hot dog, with sauerkraut, relish, onions, topped with cheese\",\n                false,\n                3.05);\n    }\n\n    public void addItem(String name,\n                        String description,\n                        boolean vegetarian,\n                        double price) {\n        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);\n        if (numberOfItems >= MAX_ITEMS) {\n            System.err.println(\"Sorry, menu is full! Can\u2018t add item to menu\");\n        } else {\n            menuItems&#91;numberOfItems] = menuItem;\n            numberOfItems = numberOfItems + 1;\n        }\n    }\n\n    public MenuItem&#91;] getMenuItems() {\n        return menuItems;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u5728\u904d\u5386\u7684\u65f6\u5019\u5c31\u9700\u8981\u540c\u65f6\u8003\u8651\u9910\u5385\u548c\u714e\u997c\u5c4b\u7684\u4e24\u79cd\u904d\u5386\u65b9\u5f0f\uff0c\u4e00\u4e2a\u662f\u5217\u8868\u904d\u5386\u4e00\u4e2a\u6570\u7ec4\u904d\u5386\uff0c\u6240\u4ee5\u4e3a\u4e86\u5c06\u904d\u5386\u7684\u8fc7\u7a0b\u4ece\u5404\u79cd\u96c6\u5408\u6216\u6570\u7ec4\u7ed3\u6784\u4e2d\u89e3\u8026\u51fa\u6765\uff0c\u91c7\u7528\u8fed\u4ee3\u5668\u6a21\u5f0f\u505a\u5982\u4e0b\u6539\u8fdb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface Iterator {\n    boolean hasNext();\n    Object next();\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MenuItem {\n    String name;\n    String description;\n    boolean vegetarian;\n    double price;\n\n    public MenuItem(String name, String description, boolean vegetarian, double price) {\n        this.name = name;\n        this.description = description;\n        this.vegetarian = vegetarian;\n        this.price = price;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public String getDescription() {\n        return description;\n    }\n\n    public double getPrice() {\n        return price;\n    }\n\n    public boolean isVegetarian() {\n        return vegetarian;\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DinerMenu {\n    static final int MAX_ITEMS = 6;\n    int numberOfItems = 0;\n    MenuItem&#91;] menuItems;\n\n    public DinerMenu() {\n        menuItems = new MenuItem&#91;MAX_ITEMS];\n\n        addItem(\"Vegetarian BLT\",\n                \"(Faking') Bacon with lettuce &amp; tomato on whole wheat\",\n                true,\n                2.99);\n        addItem(\"BLT\",\n                \"Bacon with lettuce &amp; tomato on whole wheat\",\n                false,\n                3.29);\n        addItem(\"Hot dog\",\n                \"A hot dog, with sauerkraut, relish, onions, topped with cheese\",\n                false,\n                3.05);\n    }\n\n    public void addItem(String name,\n                        String description,\n                        boolean vegetarian,\n                        double price) {\n        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);\n        if (numberOfItems >= MAX_ITEMS) {\n            System.err.println(\"Sorry, menu is full! Can\u2018t add item to menu\");\n        } else {\n            menuItems&#91;numberOfItems] = menuItem;\n            numberOfItems = numberOfItems + 1;\n        }\n    }\n\n    public Iterator createIterator() {\n        return new DinerMenuIterator(menuItems);\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DinerMenuIterator  implements Iterator {\n    MenuItem&#91;] items;\n    int position = 0;\n\n    public DinerMenuIterator(MenuItem&#91;] items) {\n        this.items = items;\n    }\n\n    @Override\n    public boolean hasNext() {\n        if (position >= items.length || items&#91;position] == null) {\n            return false;\n        } else {\n            return true;\n        }\n    }\n\n    @Override\n    public Object next() {\n        MenuItem menuItem = items&#91;position];\n        position = position + 1;\n        return menuItem;\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class PancakeHouseMenu {\n    List&lt;MenuItem> menuItems = new ArrayList&lt;>();\n\n    public PancakeHouseMenu() {\n        menuItems = new ArrayList&lt;>();\n\n        addItem(\"K&amp;B's Pancake Breakfast\",\n                \"Pancakes with scrambled eggs, and toast\",\n                true,\n                2.99);\n\n        addItem(\"Regular Pancake Breakfast\",\n                \"Pancakes with fried eggs, sausage\",\n                false,\n                2.99);\n\n        addItem(\"Blueberry Pancakes\",\n                \"Pancakes made with gresh blueberries\",\n                true,\n                3.49);\n\n        addItem(\"Waffles\",\n                \"Waffles, with your choice of blueberries or strawberries\",\n                true,\n                3.59);\n    }\n\n    public void addItem(String name,\n                        String description,\n                        boolean vegetarian,\n                        double price) {\n        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);\n        menuItems.add(menuItem);\n    }\n\n    public Iterator createIterator() {\n        return new PancakeHouseMenuIterator(menuItems);\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class PancakeHouseMenuIterator implements Iterator {\n    List&lt;MenuItem> items;\n    int index = 0;\n\n    public PancakeHouseMenuIterator(List&lt;MenuItem> items) {\n        this.items = items;\n    }\n    @Override\n    public boolean hasNext() {\n        return index &lt; items.size();\n    }\n\n    @Override\n    public Object next() {\n        return items.get(index++);\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Waitress {\n    PancakeHouseMenu pancakeHouseMenu;\n    DinerMenu dinerMenu;\n\n    public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {\n        this.pancakeHouseMenu = pancakeHouseMenu;\n        this.dinerMenu = dinerMenu;\n    }\n\n    public void printMenu() {\n        Iterator pancakeIterator = pancakeHouseMenu.createIterator();\n        Iterator dinerIterator = dinerMenu.createIterator();\n        System.out.println(\"MENU\\n----\\nBREAKFAST\");\n        printMenu(pancakeIterator);\n        System.out.println(\"\\nLUNCH\");\n        printMenu(dinerIterator);\n    }\n\n    private void printMenu(Iterator iterator) {\n        while (iterator.hasNext()) {\n            MenuItem menuItem = (MenuItem) iterator.next();\n            System.out.println(menuItem.getName() + \", \");\n            System.out.println(menuItem.getPrice() + \" -- \");\n            System.out.println(menuItem.getDescription());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4ee3\u7801\u6539\u8fdb\u540e\uff0c\u670d\u52a1\u5458\u5b9e\u4f53\u80fd\u7528\u901a\u7528\u7684\u8fed\u4ee3\u5668\u6a21\u5f0f\u904d\u5386\u5404\u79cd\u83dc\u5355\uff0c\u4f46\u662f\u670d\u52a1\u5458\u521d\u59cb\u5316\u4ee3\u7801\u8c8c\u4f3c\u4e0e\u5404\u7c7b\u83dc\u5355\u7c7b\u8026\u5408\u4e86\uff0c\u8fd9\u5e76\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\uff0c\u6240\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p>\u73b0\u5728\u62bd\u8c61\u51fa\u83dc\u5355\u63a5\u53e3\uff0c\u5e76\u5728\u670d\u52a1\u539f\u5b9e\u4f53\u4e2d\u5bf9\u5177\u4f53\u83dc\u5355\u8fdb\u884c\u89e3\u8026\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface Menu {\n    public Iterator createIterator();\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Waitress {\n    Menu pancakeHouseMenu;\n    Menu dinerMenu;\n\n    public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {\n        this.pancakeHouseMenu = pancakeHouseMenu;\n        this.dinerMenu = dinerMenu;\n    }\n\n    public void printMenu() {\n        Iterator pancakeIterator = pancakeHouseMenu.createIterator();\n        Iterator dinerIterator = dinerMenu.createIterator();\n        System.out.println(\"MENU\\n----\\nBREAKFAST\");\n        printMenu(pancakeIterator);\n        System.out.println(\"\\nLUNCH\");\n        printMenu(dinerIterator);\n    }\n\n    private void printMenu(Iterator iterator) {\n        while (iterator.hasNext()) {\n            MenuItem menuItem = (MenuItem) iterator.next();\n            System.out.println(menuItem.getName() + \", \");\n            System.out.println(menuItem.getPrice() + \" -- \");\n            System.out.println(menuItem.getDescription());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u4fee\u6539\u4ee3\u7801\u4e4b\u540e\uff0c\u8c8c\u4f3c\u548c\u5177\u4f53\u83dc\u5355\u7c7b\u8fdb\u884c\u4e86\u89e3\u8026\uff0c\u4f46\u662f\u6bcf\u6b21\u52a0\u5165\u65b0\u83dc\u5355\u65f6\uff0c\u4f9d\u7136\u8981\u4fee\u6539\u670d\u52a1\u5458\u5b9e\u4f53\u7c7b\uff0c\u5728\u6784\u9020\u51fd\u6570\u4e2d\u518d\u589e\u52a0\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd9\u5e76\u4e0d\u9075\u5faa<strong>\u5bf9\u4fee\u6539\u5173\u95ed<\/strong>\u539f\u5219\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4f18\u5316\u83dc\u5355\u904d\u5386\u8fc7\u7a0b\uff0c\u4f7f\u5176\u4e0d\u4f1a\u56e0\u4e3a\u589e\u52a0\u65b0\u7684\u83dc\u5355\u9879\u800c\u4fee\u6539\u539f\u6709\u7684\u670d\u52a1\u5458\u4ee3\u7801\u3002\u5e76\u4e14\u6211\u4eec\u5728\u8fd9\u5f15\u5165<strong>\u5355\u4e00\u8d23\u4efb<\/strong>\u8bbe\u8ba1\u539f\u5219\uff1a\u4e00\u4e2a\u7c7b\u5e94\u8be5\u53ea\u6709\u4e00\u4e2a\u5f15\u8d77\u53d8\u5316\u7684\u539f\u56e0\u3002\u670d\u52a1\u5458\u7c7b\u4e3b\u8981\u804c\u8d23\u662f\u7ba1\u7406\u83dc\u5355\u9879\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u83dc\u5355\u904d\u5386<\/li>\n\n\n\n<li>\u9075\u5faa\u5355\u4e00\u8bbe\u8ba1\u539f\u5219<\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u5c31\u9700\u8981<strong>\u7ec4\u5408\u6a21\u5f0f<\/strong>\u6765\u5b9e\u73b0\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fed\u4ee3\u5668\u6a21\u5f0f\u63d0\u4f9b\u4e00\u79cd\u65b9\u6cd5\u987a\u5e8f\u8bbf\u95ee\u4e00\u4e2a\u805a\u5408\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\uff0c\u800c\u53c8\u4e0d\u66b4\u9732\u5176\u5185\u90e8\u8868\u793a\u3002 \u5047\u8bbe\u6709\u9910\u5385\u548c\u714e\u997c\u5c4b\u5b9e\u4f53\uff0c\u5728\u7cfb\u7edf\u4e2d\u4ed6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,80],"tags":[],"class_list":["post-1080","post","type-post","status-publish","format-standard","hentry","category-code","category-design-pattern"],"_links":{"self":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1080"}],"version-history":[{"count":1,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1080\/revisions"}],"predecessor-version":[{"id":1081,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/1080\/revisions\/1081"}],"wp:attachment":[{"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1080"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.spiceandwolf.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}