迭代器模式 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示
有两家餐厅,共有两个菜单,两家准备合并,每个餐厅都有一个销售系统,但是内部菜单的组成结构稍有不同,一个是用数组存储菜单对象,一个是用集合存储菜单对象
将两个菜单合并显示
数组菜单
package com.iteratorPattern.arr;
import com.iteratorPattern.vo.MenuItem;
/**
* @program: test
* @description: 数组菜单
* @author: Mr.Yang
* @create: 2019-01-01 15:22
**/
public class ArrMenu {
static final int MAX_ITEMS=3;
int items=0;
MenuItem[] menuItems;
public ArrMenu(){
menuItems=new MenuItem[MAX_ITEMS];
addItem("西红柿鸡蛋","有营养",10.00);
addItem("酸辣白菜","好吃",11.00);
addItem("酸辣土豆丝","特色菜",12.00);
}
private void addItem(String name,String description,Double price){
MenuItem menuItem = new MenuItem(name, description, price);
if(items>=MAX_ITEMS){
System.err.println("menu is full,Can't add item to menu");
}else{
menuItems[items]=menuItem;
items+=1;
}
}
public MenuItem[] getItems(){
return menuItems;
}
}
list集合菜单
package com.iteratorPattern.list;
import com.iteratorPattern.vo.MenuItem;
import java.util.ArrayList;
/**
* @program: test
* @description: list集合菜单
* @author: Mr.Yang
* @create: 2019-01-01 15:32
**/
public class ListMenu {
ArrayList<MenuItem> menuItems;
public ListMenu(){
menuItems = new ArrayList<MenuItem>();
addItem("紫菜鸡蛋汤","有营养的汤",6.00);
addItem("豆腐脑","早餐必备",7.00);
addItem("鱿鱼汤","补充大脑",8.00);
}
private void addItem(String name,String description,Double price){
MenuItem menuItem = new MenuItem(name, description, price);
menuItems.add(menuItem);
}
public ArrayList<MenuItem> getItems(){
return menuItems;
}
}
当前合并测试类
package com.iteratorPattern.test;
import com.iteratorPattern.arr.ArrMenu;
import com.iteratorPattern.list.ListMenu;
import com.iteratorPattern.vo.MenuItem;
import java.util.ArrayList;
/**
* @program: test
* @description: 测试1
* @author: Mr.Yang
* @create: 2019-01-01 15:38
**/
public class TestOne {
//打印菜单--菜类和汤类,需要遍历两次,如果再有一个会变成三次,四次…………
public static void main(String[] args) {
ArrMenu arrMenu = new ArrMenu();
MenuItem[] items = arrMenu.getItems();
for (int i = 0; i < items.length; i++) {
MenuItem item = items[i];
System.out.println("item name:"+item.getName());
System.out.println("item description:"+item.getDescription());
System.out.println("item price:"+item.getPrice());
}
ListMenu listMenu = new ListMenu();
ArrayList listMenuItems = listMenu.getItems();
for (int i = 0; i < listMenuItems.size(); i++) {
MenuItem item = (MenuItem) listMenuItems.get(i);
System.out.println("item name:"+item.getName());
System.out.println("item description:"+item.getDescription());
System.out.println("item price:"+item.getPrice());
}
}
}
创建迭代器
package com.iteratorPattern.Iterator;
/**
* @program: test
* @description: 迭代器接口
* @author: Mr.Yang
* @create: 2019-01-01 15:44
**/
public interface Iterator {
boolean hasNext();
Object next();
}
添加数组遍历方式的迭代器
package com.iteratorPattern.arr;
import com.iteratorPattern.Iterator.Iterator;
import com.iteratorPattern.vo.MenuItem;
/**
* @program: test
* @description: 数组遍历方式迭代器
* @author: Mr.Yang
* @create: 2019-01-01 15:46
**/
public class ArrMenuIterator implements Iterator {
MenuItem[] items;
int position=0;
public ArrMenuIterator(MenuItem[] items){
this.items=items;
}
public boolean hasNext() {
if(position>=items.length || items[position]==null){
return false;
}
return true;
}
public Object next() {
MenuItem item = items[position];
position+=1;
return item;
}
}
数组菜单添加创建迭代器的方法
package com.iteratorPattern.arr;
import com.iteratorPattern.Iterator.Iterator;
import com.iteratorPattern.vo.MenuItem;
/**
* @program: test
* @description: 数组菜单
* @author: Mr.Yang
* @create: 2019-01-01 15:22
**/
public class ArrMenu {
static final int MAX_ITEMS=3;
int items=0;
MenuItem[] menuItems;
public ArrMenu(){
menuItems=new MenuItem[MAX_ITEMS];
addItem("西红柿鸡蛋","有营养",10.00);
addItem("酸辣白菜","好吃",11.00);
addItem("酸辣土豆丝","特色菜",12.00);
}
private void addItem(String name,String description,Double price){
MenuItem menuItem = new MenuItem(name, description, price);
if(items>=MAX_ITEMS){
System.err.println("menu is full,Can't add item to menu");
}else{
menuItems[items]=menuItem;
items+=1;
}
}
public Iterator createIterator(){
return new ArrMenuIterator(menuItems);
}
}
创建list迭代器
package com.iteratorPattern.list;
import com.iteratorPattern.Iterator.Iterator;
import com.iteratorPattern.vo.MenuItem;
import java.util.ArrayList;
/**
* @program: test
* @description: list迭代器
* @author: Mr.Yang
* @create: 2019-01-01 16:12
**/
public class ListMenuIterator implements Iterator {
ArrayList listMenus;
int position=0;
public ListMenuIterator(ArrayList listMenus){
this.listMenus=listMenus;
}
public boolean hasNext() {
if(position>=listMenus.size() || listMenus.get(position)==null){
return false;
}
return true;
}
public Object next() {
MenuItem menuItem = (MenuItem) listMenus.get(position);
position+=1;
return menuItem;
}
}
list集合菜单添加创建迭代器的方法
package com.iteratorPattern.list;
import com.iteratorPattern.Iterator.Iterator;
import com.iteratorPattern.vo.MenuItem;
import java.util.ArrayList;
/**
* @program: test
* @description: list集合菜单
* @author: Mr.Yang
* @create: 2019-01-01 15:32
**/
public class ListMenu {
ArrayList<MenuItem> menuItems;
public ListMenu(){
menuItems = new ArrayList<MenuItem>();
addItem("紫菜鸡蛋汤","有营养的汤",6.00);
addItem("豆腐脑","早餐必备",7.00);
addItem("鱿鱼汤","补充大脑",8.00);
}
private void addItem(String name,String description,Double price){
MenuItem menuItem = new MenuItem(name, description, price);
menuItems.add(menuItem);
}
public Iterator createIterator(){
return new ListMenuIterator(menuItems);
}
}
测试
package com.iteratorPattern.test;
import com.iteratorPattern.Iterator.Iterator;
import com.iteratorPattern.arr.ArrMenu;
import com.iteratorPattern.list.ListMenu;
import com.iteratorPattern.vo.MenuItem;
/**
* @program: test
* @description: 测试2
* @author: Mr.Yang
* @create: 2019-01-01 16:17
**/
public class TestTwo {
public static void main(String[] args) {
ArrMenu arrMenu = new ArrMenu();
ListMenu listMenu = new ListMenu();
System.out.println("---------------数组 菜类---------------");
printMenu(arrMenu.createIterator());
System.out.println("---------------集合 汤类---------------");
printMenu(listMenu.createIterator());
}
private static void printMenu(Iterator iterator){
while (iterator.hasNext()){
MenuItem next = (MenuItem) iterator.next();
System.out.println("menu name is :"+next.getName());
System.out.println("menu description is :"+next.getDescription());
System.out.println("menu price is :"+next.getPrice());
}
}
}
测试结果
---------------数组 菜类---------------
menu name is :西红柿鸡蛋
menu description is :有营养
menu price is :10.0
menu name is :酸辣白菜
menu description is :好吃
menu price is :11.0
menu name is :酸辣土豆丝
menu description is :特色菜
menu price is :12.0
---------------集合 汤类---------------
menu name is :紫菜鸡蛋汤
menu description is :有营养的汤
menu price is :6.0
menu name is :豆腐脑
menu description is :早餐必备
menu price is :7.0
menu name is :鱿鱼汤
menu description is :补充大脑
menu price is :8.0
此处不用java.util下面的迭代器是为了更好的了解迭代器是如何工作的,java.util比我们所实现的迭代器多了一个remove方法,如果有其他需求,自定义的迭代器相比于java.util更易拓展。