設計模式 - 組合模式(composite pattern) 迭代器(iterator) 詳解

來源:互聯網
上載者:User

標籤:mystra   設計模式   組合模式   迭代器   java   

組合模式(composite pattern) 迭代器(iterator) 詳解


本文地址: http://blog.csdn.net/caroline_wendy


參考組合模式(composite pattern): http://blog.csdn.net/caroline_wendy/article/details/36895627


在組合模式(composite pattern)添加迭代器功能, 遍曆每一個組合(composite)的項.


具體方法:

1. 抽象組件類(abstract component)添加建立迭代器的方法.

/** * @time 2014年7月4日 */package composite;import java.util.Iterator;/** * @author C.L.Wang * */public abstract class MenuComponent {public void add(MenuComponent menuComponent) {throw new UnsupportedOperationException(); //如果未提供, 則不能調用}public void remove(MenuComponent menuComponent) {throw new UnsupportedOperationException();}public MenuComponent getChild(int i) {throw new UnsupportedOperationException();}public String getName() {throw new UnsupportedOperationException();}public String getDescription() {throw new UnsupportedOperationException();}public double getPrice() {throw new UnsupportedOperationException();}public boolean isVegetarian() {throw new UnsupportedOperationException();}public void print() {throw new UnsupportedOperationException();}public Iterator<MenuComponent> createIterator() {throw new UnsupportedOperationException();}}

2. 組合迭代器類(composite iterator).

/** * @time 2014年7月4日 */package composite;import java.util.Iterator;import java.util.Stack;/** * @author C.L.Wang * */public class CompositeIterator implements Iterator<MenuComponent> {Stack<Iterator<MenuComponent>> stack = new Stack<Iterator<MenuComponent>>();/** *  */public CompositeIterator(Iterator<MenuComponent> iterator) {// TODO Auto-generated constructor stubstack.push(iterator);}public boolean hasNext() {if (stack.empty()) {return false;} else {Iterator<MenuComponent> iterator = (Iterator<MenuComponent>)stack.peek();if (!iterator.hasNext()) {stack.pop();return hasNext(); //遞迴調用} else {return true;}}}public MenuComponent next() {if (hasNext()) {Iterator<MenuComponent> iterator = (Iterator<MenuComponent>)stack.peek();MenuComponent component = (MenuComponent)iterator.next();if (component instanceof Menu) {stack.push(component.createIterator());}return component;} else {return null;}}public void remove() {throw new UnsupportedOperationException();}}

3. 組合類別(composite)實現建立迭代器的方法, 並建立組合迭代器(composite iterator).

/** * @time 2014年7月4日 */package composite;import java.util.ArrayList;import java.util.Iterator;/** * @author C.L.Wang * */public class Menu extends MenuComponent {ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>();String name;String description;/** *  */public Menu(String name, String description) {// TODO Auto-generated constructor stubthis.name = name;this.description = description;}public void add(MenuComponent menuComponent) {menuComponents.add(menuComponent);}public void remove(MenuComponent menuComponent) {menuComponents.remove(menuComponent);}public MenuComponent getChild(int i) {return (MenuComponent)menuComponents.get(i);}public String getName() {return name;}public String getDescription() {return description;}public void print() {System.out.print("\n" + getName());System.out.println(", " + getDescription());System.out.println("--------------------");Iterator<MenuComponent> iterator = menuComponents.iterator();while (iterator.hasNext()) {MenuComponent menuComponent = (MenuComponent)iterator.next();menuComponent.print();}}public Iterator<MenuComponent> createIterator() {return new CompositeIterator(menuComponents.iterator());}}

4. 葉子類(leaf)實現建立空迭代器(null iterator)的方法.

/** * @time 2014年7月4日 */package composite;import java.util.Iterator;/** * @author C.L.Wang * */public class NullIterator implements Iterator<MenuComponent> {public MenuComponent next() {return null;}public boolean hasNext() {return false;}public void remove() {throw new UnsupportedOperationException();}}/** * @time 2014年7月4日 */package composite;import java.util.Iterator;/** * @author C.L.Wang * */public class MenuItem extends MenuComponent {String name;String description;boolean vegetarian;double price;/** *  */public MenuItem(String name, String description,boolean vegetarian, double price) {// TODO Auto-generated constructor stubthis.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 void print() {System.out.print(" " + getName());if (isVegetarian()) {System.out.print("(v)");}System.out.println(", " + getPrice());System.out.println("    -- " + getDescription());}public Iterator<MenuComponent> createIterator() {return new NullIterator();}}

5. 客戶類.

/** * @time 2014年7月4日 */package composite;import java.util.Iterator;/** * @author C.L.Wang * */public class Waitress {MenuComponent allMenus;/** *  */public Waitress(MenuComponent allMenus) {// TODO Auto-generated constructor stubthis.allMenus = allMenus;}public void printMenu() {allMenus.print();}public void printVegetarianMenu() {Iterator<MenuComponent> iterator = allMenus.createIterator();System.out.println("\nVEGETARIAN MENU\n----");while (iterator.hasNext()) {MenuComponent menuComponent = (MenuComponent)iterator.next();try {if (menuComponent.isVegetarian())menuComponent.print();} catch (UnsupportedOperationException ex) {}}}}


6. 測試類別

/** * @time 2014年7月4日 */package composite;/** * @author C.L.Wang * */public class MenuTestDrive {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubMenuComponent pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");MenuComponent dinerMenu = new Menu("DINER MENU", "Lunch");MenuComponent cafeMenu = new Menu("CAFE MENU", "Dinner");MenuComponent dessertMenu = new Menu("DESSERT MENU", "Dessert course!");MenuComponent coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee");MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined");allMenus.add(pancakeHouseMenu);allMenus.add(dinerMenu);allMenus.add(cafeMenu);pancakeHouseMenu.add(new MenuItem("K&B‘s Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true,2.99));pancakeHouseMenu.add(new MenuItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false,2.99));pancakeHouseMenu.add(new MenuItem("Blueberry Pancakes","Pancakes made with fresh blueberries, and blueberry syrup",true,3.49));pancakeHouseMenu.add(new MenuItem("Waffles","Waffles, with your choice of blueberries or strawberries",true,3.59));dinerMenu.add(new MenuItem("Vegetarian BLT","(Fakin‘) Bacon with lettuce & tomato on whole wheat", true, 2.99));dinerMenu.add(new MenuItem("BLT","Bacon with lettuce & tomato on whole wheat", false, 2.99));dinerMenu.add(new MenuItem("Soup of the day","A bowl of the soup of the day, with a side of potato salad", false, 3.29));dinerMenu.add(new MenuItem("Hotdog","A hot dog, with saurkraut, relish, onions, topped with cheese",false, 3.05));dinerMenu.add(new MenuItem("Steamed Veggies and Brown Rice","Steamed vegetables over brown rice", true, 3.99)); dinerMenu.add(new MenuItem("Pasta","Spaghetti with Marinara Sauce, and a slice of sourdough bread",true, 3.89));   dinerMenu.add(dessertMenu);  dessertMenu.add(new MenuItem("Apple Pie","Apple pie with a flakey crust, topped with vanilla icecream",true,1.59));  dessertMenu.add(new MenuItem("Cheesecake","Creamy New York cheesecake, with a chocolate graham crust",true,1.99));dessertMenu.add(new MenuItem("Sorbet","A scoop of raspberry and a scoop of lime",true,1.89));cafeMenu.add(new MenuItem("Veggie Burger and Air Fries","Veggie burger on a whole wheat bun, lettuce, tomato, and fries",true, 3.99));cafeMenu.add(new MenuItem("Soup of the day","A cup of the soup of the day, with a side salad",false, 3.69));cafeMenu.add(new MenuItem("Burrito","A large burrito, with whole pinto beans, salsa, guacamole",true, 4.29));cafeMenu.add(coffeeMenu);coffeeMenu.add(new MenuItem("Coffee Cake","Crumbly cake topped with cinnamon and walnuts",true,1.59));coffeeMenu.add(new MenuItem("Bagel","Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",false,0.69));coffeeMenu.add(new MenuItem("Biscotti","Three almond or hazelnut biscotti cookies",true,0.89)); Waitress waitress = new Waitress(allMenus);   waitress.printVegetarianMenu();}}

7. 輸出.

VEGETARIAN MENU---- K&B‘s Pancake Breakfast(v), 2.99    -- Pancakes with scrambled eggs, and toast Blueberry Pancakes(v), 3.49    -- Pancakes made with fresh blueberries, and blueberry syrup Waffles(v), 3.59    -- Waffles, with your choice of blueberries or strawberries Vegetarian BLT(v), 2.99    -- (Fakin‘) Bacon with lettuce & tomato on whole wheat Steamed Veggies and Brown Rice(v), 3.99    -- Steamed vegetables over brown rice Pasta(v), 3.89    -- Spaghetti with Marinara Sauce, and a slice of sourdough bread Apple Pie(v), 1.59    -- Apple pie with a flakey crust, topped with vanilla icecream Cheesecake(v), 1.99    -- Creamy New York cheesecake, with a chocolate graham crust Sorbet(v), 1.89    -- A scoop of raspberry and a scoop of lime Apple Pie(v), 1.59    -- Apple pie with a flakey crust, topped with vanilla icecream Cheesecake(v), 1.99    -- Creamy New York cheesecake, with a chocolate graham crust Sorbet(v), 1.89    -- A scoop of raspberry and a scoop of lime Veggie Burger and Air Fries(v), 3.99    -- Veggie burger on a whole wheat bun, lettuce, tomato, and fries Burrito(v), 4.29    -- A large burrito, with whole pinto beans, salsa, guacamole Coffee Cake(v), 1.59    -- Crumbly cake topped with cinnamon and walnuts Biscotti(v), 0.89    -- Three almond or hazelnut biscotti cookies Coffee Cake(v), 1.59    -- Crumbly cake topped with cinnamon and walnuts Biscotti(v), 0.89    -- Three almond or hazelnut biscotti cookies






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.