例题:两个菜馆合并后的菜单问题
蛋糕店与餐厅合并后,怎么统一管理菜单项目
问题:一个用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