组合迭代器

package menu;

import java.util.ArrayList;
import java.util.Iterator;

public class Menu extends MenuComponent {

	ArrayList<MenuComponent> menuComponents=new ArrayList<MenuComponent>();
	String name;
	String description;
	public Menu(String name,String description){
		this.name=name;
		this.description=description;
		
	}
	@Override
	public void add(MenuComponent menuComponent) {
		menuComponents.add(menuComponent);
	}
	@Override
	public void remove(MenuComponent menuComponent) {
		menuComponents.remove(menuComponent);
	}
	@Override
	public MenuComponent getChild(int i) {
		return menuComponents.get(i);
	}
	
	@Override
	public void print() {
		System.out.print("\n"+name);
		System.out.println(", "+description);
		System.out.println("----------------------");
		Iterator<MenuComponent> iterator=menuComponents.iterator();
		while(iterator.hasNext()){
			MenuComponent  component=iterator.next();
			component.print();
			
		}
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	@Override
	public Iterator<MenuComponent> createIterator() {
		return menuComponents.iterator();
	}
	
	
}
 package menu; import java.util.Iterator; 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(); } }

 

package menu;

public class MenuItem extends MenuComponent {

	String name;
	String description;
	boolean vegetarian;
	double price;
	public MenuItem(String name,String description,boolean vegetarian,double price) {
		this.name=name;
		this.description=description;
		this.vegetarian=vegetarian;
		this.price=price;
	}
	@Override
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	@Override
	public boolean isVegetarian() {
		return vegetarian;
	}
	public void setVegetarian(boolean vegetarian) {
		this.vegetarian = vegetarian;
	}
	@Override
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public void print() {
	    System.out.print("  "+getName());
	    if(isVegetarian()){
	    	System.out.println("(v)");
	    }
	    System.out.println(", "+getPrice());
	    System.out.println("     -- "+getDescription());
	}
	
}

 

package iterator;

import java.util.Iterator;
import java.util.Stack;

import menu.Menu;
import menu.MenuComponent;

public class CompositeIterator implements Iterator<MenuComponent>{

	Stack<Iterator<MenuComponent>> stack=new Stack<Iterator<MenuComponent>>();
	public CompositeIterator(Iterator<MenuComponent> iterator) {
		stack.push(iterator);
	}
	@Override
	public boolean hasNext() {
		if(stack.isEmpty()){
			return false;
			
		}else{
			Iterator<MenuComponent> iterator=stack.peek();//取栈顶
			if(!iterator.hasNext()){//如果没有下个元素,就将其出栈
				stack.pop();
				return hasNext();
				
			}else{
				return true;
			}
		}
		
	}

	@Override
	public MenuComponent next() {
		if(hasNext()){
			Iterator<MenuComponent> iterator=stack.peek();
			MenuComponent menuComponent=iterator.next();
			if(menuComponent instanceof Menu){//如果不是叶结点就将当前节点入栈
				stack.push(menuComponent.createIterator());				
			}
			return menuComponent;
			
		}else{
			return null;
		}
		
	}
	@Override
	public void remove() {
		throw new UnsupportedOperationException();
		
	}

	

	
}

 

你可能感兴趣的:(迭代器)