设计模式之迭代器模式

例题:两个菜馆合并后的菜单问题

蛋糕店与餐厅合并后,怎么统一管理菜单项目
问题:一个用ArrayList管理菜单,一个用数组管理

迭代器模式的原理和设计

这里写图片描述

关键代码:

1. 实现迭代器额接口
public class CakeHouseMenu
{
	private ArrayList<MenuItem> menuItems;
	
	public CakeHouseMenu()
	{
		menuItems=new ArrayList<MenuItem>();
		
		addItem("KFC Cake Breakfast", "boiled eggs & cabbage", true, 3.99);
		addItem("MDL Cake Breakfast","fried eggs&toast",false,3.59f);
		addItem("Stawberry Cake","fresh stawberry",true,3.29f);
		addItem("Regular Cake Breakfast","toast&sausage",true,2.59f);
	}
	
	private void addItem(String name,String description,boolean isVegetable,double price)
	{
		MenuItem menuItem=new MenuItem(name, description, isVegetable, price);
		menuItems.add(menuItem);
	}
	
	public Iterator getIterator()
	{
		return new CakeHouseIterator();
	}
	
	class CakeHouseIterator implements Iterator
	{
		private int position=0;
		
		public CakeHouseIterator()
		{
			position=0;
		}
		
		@Override
		public boolean hasNext()
		{
			if(position<menuItems.size())
			{
				return true;
			}
			return false;
		}

		@Override
		public Object next()
		{
			MenuItem menuItem=menuItems.get(position);
			position++;
			return menuItem;
		}	
	}
}
2. 统一使用迭代器
public class Waitress
{
	private ArrayList<Iterator> iterators=new ArrayList<Iterator>();
	
	public Waitress()
	{
		// TODO Auto-generated constructor stub
	}
	
	public void addIterator(Iterator iterator)
	{
		iterators.add(iterator);
	}
	
	public void printMenu()
	{
		Iterator iterator;
		MenuItem menuItem;
		for (int i = 0; i < iterators.size(); i++)
		{
			iterator=iterators.get(i);
			while (iterator.hasNext())
			{
				MenuItem item = (MenuItem) iterator.next();
				System.out.println(item.getName()+"  "+item.getDescription()+"  "+item.isVegetable()+"  "+
				item.getPrice());
			}
		}
	}
}

Java 内置的迭代器

java.util.iterator.
ArrayList<>等数据结构都有getIterator()

单一责任原则:

一个类只有一个引起变换的原因。

Address:

http://download.csdn.net/detail/kaikai_sk/9873796

二叉树先序遍历迭代器

public class TreeNode {
  int val = 0;
  TreeNode left = null;
  TreeNode right = null;
  public TreeNode(int val) {
    this.val = val;
  }
}
public class BinaryTree {
    private TreeNode root;

    public BinaryTree() {
        root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.right.left = new TreeNode(5);
    }

    public Iterator<TreeNode> getIterator() {
        return new PreOrderIterator(root);
    }
}
public interface Iterator<T> {
    boolean hasNext();
    T next();
}

public class PreOrderIterator implements Iterator {
    private Stack<TreeNode> nodeStack;
    private TreeNode currentNode;

    public PreOrderIterator(TreeNode root) {
        nodeStack = new Stack<>();
        currentNode = root;
    }

    @Override
    public boolean hasNext() {
        return (currentNode != null) || !nodeStack.empty();
    }

    @Override
    public Object next() {
        TreeNode nextNode = null;
        if (currentNode != null) {
            nodeStack.push(currentNode);
        }
        if (!nodeStack.isEmpty()) {
            nextNode = nodeStack.pop();
            if (nextNode.right != null) {
                nodeStack.push(nextNode.right);
            }
            currentNode = nextNode.left;
        }
        return nextNode;
    }
}
public class Client {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        Iterator<TreeNode> iterator = binaryTree.getIterator();
        while (iterator.hasNext()) {
            TreeNode node = iterator.next();
            System.out.println(node.val);
        }
    }
}

参考材料

[1] https://blog.csdn.net/sima1989/article/details/39853683

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值