`
pan_java
  • 浏览: 286189 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

hand first 设计模式 -迭代器模式

阅读更多
迭代器模式 : 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示.


A食物
public class AFood {

	private String name;

	private double price;
	
	public AFood(String name,double price){
		this.name= name;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

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

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Name:" + name + " ,Price" + price;
	}

}



A菜单
public class AMenu implements Menu {

	//利用List
         private static List<AFood> foodList = new ArrayList<AFood>();

	static {
		foodList.add(new AFood("1food", 20.00));
		foodList.add(new AFood("2food", 10.00));
		foodList.add(new AFood("3food", 15.0));
	}

	@Override
	public Iterator createIterator() {
		// TODO Auto-generated method stub
		return foodList.iterator();
	}

}



B食物
public class BFood {
	private String name;

	private double price;

	private boolean isVaggltable;

	public BFood(String name, double price, boolean isVaggltable) {
		this.name = name;
		this.price = price;
		this.isVaggltable = isVaggltable;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

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

	public boolean isVaggltable() {
		return isVaggltable;
	}

	public void setVaggltable(boolean isVaggltable) {
		this.isVaggltable = isVaggltable;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Name:" + name + " Price:" + price + " isVaggltable:"
				+ isVaggltable;
	}

}



B菜单
public class BMenu implements Menu {

	//利用数组封装
         private static BFood[] BFoodArray = null;

	static {
		BFoodArray = new BFood[] { new BFood("1bfood", 63, true),
				new BFood("2bfood", 36, true), new BFood("3bfood", 25, false) };
	}

	@Override
	public Iterator createIterator() {
		// TODO Auto-generated method stub
		ArrayIterator<BFood> ai = new ArrayIterator<BFood>(BFoodArray);
		return ai;
	}

}


所有菜单接口
public interface Menu {
	
	 //创建迭代器
          public Iterator createIterator();

}



数组迭代器
public class ArrayIterator<T> implements Iterator {

	private T[] arr;

	private int index = -1;

	public ArrayIterator(T[] arr) {
		this.arr = arr;
	}

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		index++;
		if(index>arr.length-1)
			return false;
		return arr[index] != null;

	}

	@Override
	public T next() {
		// TODO Auto-generated method stub
		return arr[index];
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException();

	}

}


测试类--打印所有菜单
public class Test {

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test test = new Test();
		
		Menu a = new AMenu();
		
		Menu b = new BMenu();
		
		test.printMenu(a,b);

	}
	
	private void printMenu(Menu...menus){
		for(Menu menu:menus){
			Iterator iterator = menu.createIterator();
			while(iterator.hasNext()){
				System.out.println(iterator.next());
			}
		}
		
	}

}
分享到:
评论
2 楼 pan_java 2009-07-04  
whatwhat 写道
代码太多,说明太少
写了给自己看吧


谢谢建议,以后改善!
1 楼 whatwhat 2009-07-03  
代码太多,说明太少
写了给自己看吧

相关推荐

Global site tag (gtag.js) - Google Analytics