1. 程式人生 > >【設計模式】迭代器模式(行為型)

【設計模式】迭代器模式(行為型)

迭代器模式

    給定一個語言,定義它的文法的一種表示,並定義一個直譯器,這個直譯器使用該表示來解釋語言中的句子。

 適用性

    1.訪問一個聚合物件的內容而無需暴露它的內部表示。
    2.支援對聚合物件的多種遍歷。
      3.為遍歷不同的聚合結構提供一個統一的介面(即,支援多型迭代)。
類結構圖
head_first結構圖:
系統結構圖:
--------------------------------------
具體迭代器
-----------------------------------------
public class BreakfastIterator implements Iterator {
	private BreakfastMenu breakfastMenu;
	private int index = 0;
	public BreakfastIterator(BreakfastMenu breakfastMenu) {
		this.breakfastMenu = breakfastMenu;
	}
	@Override
	public boolean hasnext() {
		// TODO Auto-generated method stub
		if (index < breakfastMenu.getMenuI().length) {
			if (this.breakfastMenu.getMenuI()[index] != null) {
				return true;
			}
		}
		return false;
	}
	@Override
	public Object next() {
		// TODO Auto-generated method stub
		Object object = this.breakfastMenu.getMenuI()[index];
		index = index+1;
		return object;
	}
}

public class DinnerIterator implements Iterator {
	private DinnerMenu dinnerMenu;
	private int index = 0;

	public DinnerIterator(DinnerMenu dinnerMenu) {
		this.dinnerMenu = dinnerMenu;
	}
	@Override
	public boolean hasnext() {
		// TODO Auto-generated method stub
		if (index < this.dinnerMenu.getMenuI().size()) {
			if (this.dinnerMenu.getMenuI().get(index) != null) {
				return true;
			}
		}
		return false;
	}
	@Override
	public Object next() {
		// TODO Auto-generated method stub
		Object object = this.dinnerMenu.getMenuI().get(index);
		index++;
		return object;
	}
}
--------------------------------------
抽象迭代器
--------------------------------------
public interface Iterator { public boolean hasnext(); public Object next(); }
-------------------------------------
集合
-----------------------------------------
public class BreakfastMenu {
	MenuItem[] menuI;
    private int index;
	public BreakfastMenu() {
		menuI = new MenuItem[100];
		MenuItem mt1 = new MenuItem("aa1", 10, "do the");
		MenuItem mt2 = new MenuItem("aa2", 10, "do the");
		MenuItem mt3 = new MenuItem("aa3", 10, "do the");
		MenuItem mt4 = new MenuItem("aa4", 10, "do the");
		menuI[index++] = mt1;
		menuI[index++] = mt2;
		menuI[index++] = mt3;
		menuI[index++] = mt4;
	}
	public void addMenuItem(String name, int cost, String desc) {
		MenuItem mt = new MenuItem(name, cost, desc);
		menuI[index++] = mt;
	}
	public MenuItem[] getMenuI() {
		return menuI;
	}
	public Iterator createIterator(){
		return new BreakfastIterator(new BreakfastMenu());
	}
}

public class DinnerMenu {
	ArrayList<MenuItem> menuI;	
	public DinnerMenu(){
		menuI=new ArrayList();
		MenuItem mt1=new MenuItem("aa5", 10, "do the");
		MenuItem mt2=new MenuItem("aa6", 10, "do the");
		MenuItem mt3=new MenuItem("aa7", 10, "do the");
		MenuItem mt4=new MenuItem("aa8", 10, "do the");
		menuI.add(mt1);
		menuI.add(mt2);
		menuI.add(mt3);
		menuI.add(mt4);
	}
	public void addMenuItem(String name,int cost,String desc){
		MenuItem mt=new MenuItem(name, cost, desc);
		this.menuI.add(mt);
	}
	public ArrayList<MenuItem> getMenuI() {
		return menuI;
	}
	
	public Iterator createIterator(){
		return new DinnerIterator(new DinnerMenu());
	}
}
-----------------------------------------
集合項
-------------------------------------------
public class MenuItem { private int cost; private String desc; private String name; public MenuItem(){ } public MenuItem(String name, int cost, String desc) { super(); this.name = name; this.cost = cost; this.desc = desc; } @Override public String toString() { return "MenuItem [name=" + name + ", cost=" + cost + ", desc=" + desc + "]"; } }
-----------------------------------------
客戶
-----------------------------------------
public class Waitress { private DinnerMenu dinnerMenu = null; private BreakfastMenu breakfastMenu = null; public Waitress(DinnerMenu dinnerMenu, BreakfastMenu breakfastMenu) { this.dinnerMenu = dinnerMenu; this.breakfastMenu = breakfastMenu; } public void printMenu() { Iterator ite = this.dinnerMenu.createIterator(); Iterator itee2=this.breakfastMenu.createIterator(); print(ite); System.out.println("-----------------------------"); print(itee2); } public void print(Iterator ite) { while (ite.hasnext()) { System.out.println(ite.next().toString()); } } }
-------------------------------
測試類
------------------------------
public class TestClass { public static void main(String[] args) { // TODO Auto-generated method stub BreakfastMenu breakfastMenu=new BreakfastMenu(); DinnerMenu dinnerMenu=new DinnerMenu(); Waitress waitress=new Waitress(dinnerMenu, breakfastMenu); waitress.printMenu(); } }
-------------------------------
結果
----------------------------------
MenuItem [name=aa5, cost=10, desc=do the]
MenuItem [name=aa6, cost=10, desc=do the]
MenuItem [name=aa7, cost=10, desc=do the]
MenuItem [name=aa8, cost=10, desc=do the]
-----------------------------
MenuItem [name=aa1, cost=10, desc=do the]
MenuItem [name=aa2, cost=10, desc=do the]
MenuItem [name=aa3, cost=10, desc=do the]
MenuItem [name=aa4, cost=10, desc=do the]