JAVA设计模式---迭代器模式

1、定义: 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

2、实例:
1)需求:
菜单(煎饼屋菜单、餐厅菜单和咖啡菜单)采用不同的集合存取(ArrayList,String[],Hashtable),使用迭代器模式使服务员(waitress)能够不依赖于具体菜单而实现打印菜单操作。

2)代码实现:

  a)菜单接口及实体类:

public interface Menu {
public Iterator createIterator();
} public class MenuItem {
private String name;
private String desc;
private boolean vegetarian;
private double price; public MenuItem(String name, String desc, boolean vegetarian, double price) {
this.name = name;
this.desc = desc;
this.vegetarian = vegetarian;
this.price = price;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} 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;
}
}

  b) 菜单具体实现

public class PancakeHouseMenu implements Menu {
ArrayList menuItems; public PancakeHouseMenu() {
menuItems = new ArrayList();
addItem("kobe's pancake breakfast","pancakes with eggs",false,2.99);
addItem("lilei's pancake breakfast", "pancakes with toast", false, 3.59);
} public void addItem(String s, String s1, boolean b, double v) {
MenuItem menuItem = new MenuItem(s,s1,b,v);
menuItems.add(menuItem);
} public Iterator createIterator(){
return menuItems.iterator();
}
} public class DinnerMenu implements Menu {
private static final int MAX_SIZE = 6;
int numOfItems = 0;
MenuItem[] menuItems; public DinnerMenu() {
menuItems = new MenuItem[MAX_SIZE];
addItem("Vegetarian BLT","Bacon with tomato",true,2.99);
addItem("Hot dog","With onions and cheese",false,3.05);
} private void addItem(String s, String s1, boolean b, double v) {
MenuItem menuItem = new MenuItem(s,s1,b,v);
if(numOfItems >= MAX_SIZE){
System.err.println("sorry,menu is full!");
}else{
menuItems[numOfItems]=menuItem;
numOfItems = numOfItems + 1;
}
} @Override
public Iterator createIterator() {
return new DinerMenuIterator(menuItems);
}
} public class CoffeeMenu implements Menu {
Hashtable menuItems = new Hashtable(); public CoffeeMenu() {
addItem("Mocha","Han Meimei order on couple of Mocha",false,3.01);
} private void addItem(String s, String s1, boolean b, double v) {
MenuItem menuItem = new MenuItem(s,s1,b,v);
menuItems.put(menuItem.getName(),menuItem);
} @Override
public Iterator createIterator() {
return menuItems.values().iterator();
}
}

  c) 数组没有.iterator()方法,需要具体实现

public class DinerMenuIterator implements Iterator {
MenuItem[] list;
int position = 0; public DinerMenuIterator(MenuItem[] list) {
this.list = list;
} @Override
public boolean hasNext() {
if(position>=list.length || list[position] == null){
return false;
}else{
return true;
}
} @Override
public Object next() {
MenuItem menuItem = list[position];
position = position + 1;
return menuItem;
} @Override
public void remove() {
if(position <=0){
throw new IllegalStateException("now you can not remove an item");
}
if(list[position] != null){
for(int i=position-1;i<(list.length-1);i++){
list[i] = list[i+1];
}
list[list.length-1]=null;
}
}
}

  d) waitress实现

public class Waitress {
Menu pancakeHouseMenu;
Menu dinnerMenu;
Menu coffeeMenu; public Waitress(Menu pancakeHouseMenu, Menu dinnerMenu, Menu coffeeMenu) {
this.pancakeHouseMenu = pancakeHouseMenu;
this.dinnerMenu = dinnerMenu;
this.coffeeMenu = coffeeMenu;
} public void printMenu(){
Iterator pancakeHouseIterator = pancakeHouseMenu.createIterator();
Iterator dinnerIterator = dinnerMenu.createIterator();
Iterator coffeeIterator = coffeeMenu.createIterator();
System.out.println("Menu\n=====Breakfast==start======");
printMenu(pancakeHouseIterator);
System.out.println("Menu\n=====Breakfast===end=====");
System.out.println("Menu\n=====Lunch==start======");
printMenu(dinnerIterator);
System.out.println("Menu\n=====Lunch===end=====");
System.out.println("Menu\n=====Coffee==start======");
printMenu(coffeeIterator);
System.out.println("Menu\n=====Coffee===end=====");
} private void printMenu(Iterator iterator){
while (iterator.hasNext()){
MenuItem menuItem = (MenuItem)iterator.next();
System.out.println(menuItem.getName()+", ");
System.out.println(menuItem.getPrice()+", ");
System.out.println(menuItem.getDesc());
}
}
}

  e) 测试类

public class MenuTest {
public static void main(String[] args) {
PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();
DinnerMenu dinnerMenu = new DinnerMenu();
CoffeeMenu coffeeMenu = new CoffeeMenu(); Waitress waitress = new Waitress(pancakeHouseMenu,dinnerMenu,coffeeMenu);
waitress.printMenu();
}
}

  测试结果:

Menu
=====Breakfast==start======
kobe's pancake breakfast,
2.99,
pancakes with eggs
lilei's pancake breakfast,
3.59,
pancakes with toast
Menu
=====Breakfast===end=====
Menu
=====Lunch==start======
Vegetarian BLT,
2.99,
Bacon with tomato
Hot dog,
3.05,
With onions and cheese
Menu
=====Lunch===end=====
Menu
=====Coffee==start======
Mocha,
3.01,
Han Meimei order on couple of Mocha
Menu
=====Coffee===end=====

上一篇:C# WinForm 异步执行耗时操作并将过程显示在界面中


下一篇:(转载)【TP5.0】设置session有效时长+修改默认存储路径