迭代器模式

时间:2024-01-22 16:40:23

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

情节

有两家餐厅,共有两个菜单,两家准备合并,每个餐厅都有一个销售系统,但是内部菜单的组成结构稍有不同,一个是用数组存储菜单对象,一个是用集合存储菜单对象

需求

将两个菜单合并显示

两个菜单代码

数组菜单

 1 package com.iteratorPattern.arr;
 2 
 3 import com.iteratorPattern.vo.MenuItem;
 4 
 5 /**
 6  * @program: test
 7  * @description: 数组菜单
 8  * @author: Mr.Yang
 9  * @create: 2019-01-01 15:22
10  **/
11 public class ArrMenu {
12     static final int MAX_ITEMS=3;
13     int items=0;
14     MenuItem[] menuItems;
15     public ArrMenu(){
16         menuItems=new MenuItem[MAX_ITEMS];
17         addItem("西红柿鸡蛋","有营养",10.00);
18         addItem("酸辣白菜","好吃",11.00);
19         addItem("酸辣土豆丝","特色菜",12.00);
20     }
21 
22     private void addItem(String name,String description,Double price){
23         MenuItem menuItem = new MenuItem(name, description, price);
24         if(items>=MAX_ITEMS){
25             System.err.println("menu is full,Can't add item to menu");
26         }else{
27             menuItems[items]=menuItem;
28             items+=1;
29         }
30     }
31 
32     public MenuItem[] getItems(){
33         return menuItems;
34     }
35 
36 }

 

 

list集合菜单

 1 package com.iteratorPattern.list;
 2 
 3 import com.iteratorPattern.vo.MenuItem;
 4 
 5 import java.util.ArrayList;
 6 
 7 /**
 8  * @program: test
 9  * @description: list集合菜单
10  * @author: Mr.Yang
11  * @create: 2019-01-01 15:32
12  **/
13 public class ListMenu {
14     ArrayList<MenuItem> menuItems;
15     public ListMenu(){
16         menuItems = new ArrayList<MenuItem>();
17         addItem("紫菜鸡蛋汤","有营养的汤",6.00);
18         addItem("豆腐脑","早餐必备",7.00);
19         addItem("鱿鱼汤","补充大脑",8.00);
20     }
21 
22 
23     private void addItem(String name,String description,Double price){
24         MenuItem menuItem = new MenuItem(name, description, price);
25         menuItems.add(menuItem);
26     }
27 
28     public ArrayList<MenuItem> getItems(){
29         return menuItems;
30     }
31 }

 

 

当前合并测试类

 1 package com.iteratorPattern.test;
 2 
 3 import com.iteratorPattern.arr.ArrMenu;
 4 import com.iteratorPattern.list.ListMenu;
 5 import com.iteratorPattern.vo.MenuItem;
 6 
 7 import java.util.ArrayList;
 8 
 9 /**
10  * @program: test
11  * @description: 测试1
12  * @author: Mr.Yang
13  * @create: 2019-01-01 15:38
14  **/
15 public class TestOne {
16     //打印菜单--菜类和汤类,需要遍历两次,如果再有一个会变成三次,四次…………
17     public static void main(String[] args) {
18         ArrMenu arrMenu = new ArrMenu();
19         MenuItem[] items = arrMenu.getItems();
20         for (int i = 0; i < items.length; i++) {
21             MenuItem item = items[i];
22             System.out.println("item name:"+item.getName());
23             System.out.println("item description:"+item.getDescription());
24             System.out.println("item price:"+item.getPrice());
25         }
26         ListMenu listMenu = new ListMenu();
27         ArrayList listMenuItems = listMenu.getItems();
28         for (int i = 0; i < listMenuItems.size(); i++) {
29             MenuItem item = (MenuItem) listMenuItems.get(i);
30             System.out.println("item name:"+item.getName());
31             System.out.println("item description:"+item.getDescription());
32             System.out.println("item price:"+item.getPrice());
33         }
34     }
35 }

 

 

整合后

创建迭代器

 1 package com.iteratorPattern.Iterator;
 2 
 3 /**
 4  * @program: test
 5  * @description: 迭代器接口
 6  * @author: Mr.Yang
 7  * @create: 2019-01-01 15:44
 8  **/
 9 public interface Iterator {
10     boolean hasNext();
11     Object next();
12 }

 

 

添加数组遍历方式的迭代器

 1 package com.iteratorPattern.arr;
 2 
 3 import com.iteratorPattern.Iterator.Iterator;
 4 import com.iteratorPattern.vo.MenuItem;
 5 
 6 /**
 7  * @program: test
 8  * @description: 数组遍历方式迭代器
 9  * @author: Mr.Yang
10  * @create: 2019-01-01 15:46
11  **/
12 public class ArrMenuIterator implements Iterator {
13     MenuItem[] items;
14     int position=0;
15     public ArrMenuIterator(MenuItem[] items){
16         this.items=items;
17     }
18 
19     public boolean hasNext() {
20         if(position>=items.length || items[position]==null){
21             return false;
22         }
23         return true;
24     }
25 
26     public Object next() {
27         MenuItem item = items[position];
28         position+=1;
29         return item;
30     }
31 }
 

数组菜单添加创建迭代器的方法

 1 package com.iteratorPattern.arr;
 2 
 3 import com.iteratorPattern.Iterator.Iterator;
 4 import com.iteratorPattern.vo.MenuItem;
 5 
 6 /**
 7  * @program: test
 8  * @description: 数组菜单
 9  * @author: Mr.Yang
10  * @create: 2019-01-01 15:22
11  **/
12 public class ArrMenu {
13     static final int MAX_ITEMS=3;
14     int items=0;
15     MenuItem[] menuItems;
16     public ArrMenu(){
17         menuItems=new MenuItem[MAX_ITEMS];
18         addItem("西红柿鸡蛋","有营养",10.00);
19         addItem("酸辣白菜","好吃",11.00);
20         addItem("酸辣土豆丝","特色菜",12.00);
21     }
22 
23     private void addItem(String name,String description,Double price){
24         MenuItem menuItem = new MenuItem(name, description, price);
25         if(items>=MAX_ITEMS){
26             System.err.println("menu is full,Can't add item to menu");
27         }else{
28             menuItems[items]=menuItem;
29             items+=1;
30         }
31     }
32 
33     public Iterator createIterator(){
34         return new ArrMenuIterator(menuItems);
35     }
36 
37 }

 

 

创建list迭代器

 1 package com.iteratorPattern.list;
 2 
 3 import com.iteratorPattern.Iterator.Iterator;
 4 import com.iteratorPattern.vo.MenuItem;
 5 
 6 import java.util.ArrayList;
 7 
 8 /**
 9  * @program: test
10  * @description: list迭代器
11  * @author: Mr.Yang
12  * @create: 2019-01-01 16:12
13  **/
14 public class ListMenuIterator implements Iterator {
15     ArrayList listMenus;
16     int position=0;
17     public ListMenuIterator(ArrayList listMenus){
18         this.listMenus=listMenus;
19     }
20 
21     public boolean hasNext() {
22         if(position>=listMenus.size() || listMenus.get(position)==null){
23             return false;
24         }
25         return true;
26     }
27 
28     public Object next() {
29         MenuItem menuItem = (MenuItem) listMenus.get(position);
30         position+=1;
31         return menuItem;
32     }
33 }

 

 

list集合菜单添加创建迭代器的方法

 1 package com.iteratorPattern.list;
 2 
 3 import com.iteratorPattern.Iterator.Iterator;
 4 import com.iteratorPattern.vo.MenuItem;
 5 
 6 import java.util.ArrayList;
 7 
 8 /**
 9  * @program: test
10  * @description: list集合菜单
11  * @author: Mr.Yang
12  * @create: 2019-01-01 15:32
13  **/
14 public class ListMenu {
15     ArrayList<MenuItem> menuItems;
16     public ListMenu(){
17         menuItems = new ArrayList<MenuItem>();
18         addItem("紫菜鸡蛋汤","有营养的汤",6.00);
19         addItem("豆腐脑","早餐必备",7.00);
20         addItem("鱿鱼汤","补充大脑",8.00);
21     }
22 
23 
24     private void addItem(String name,String description,Double price){
25         MenuItem menuItem = new MenuItem(name, description, price);
26         menuItems.add(menuItem);
27     }
28     public Iterator createIterator(){
29         return new ListMenuIterator(menuItems);
30     }
31 }

 

 

测试

 1 package com.iteratorPattern.test;
 2 
 3 import com.iteratorPattern.Iterator.Iterator;
 4 import com.iteratorPattern.arr.ArrMenu;
 5 import com.iteratorPattern.list.ListMenu;
 6 import com.iteratorPattern.vo.MenuItem;
 7 
 8 /**
 9  * @program: test
10  * @description: 测试2
11  * @author: Mr.Yang
12  * @create: 2019-01-01 16:17
13  **/
14 public class TestTwo {
15     public static void main(String[] args) {
16         ArrMenu arrMenu = new ArrMenu();
17         ListMenu listMenu = new ListMenu();
18         System.out.println("---------------数组 菜类---------------");
19         printMenu(arrMenu.createIterator());
20         System.out.println("---------------集合 汤类---------------");
21         printMenu(listMenu.createIterator());
22     }
23     private static void printMenu(Iterator iterator){
24         while (iterator.hasNext()){
25             MenuItem next = (MenuItem) iterator.next();
26             System.out.println("menu name is :"+next.getName());
27             System.out.println("menu description is :"+next.getDescription());
28             System.out.println("menu price is :"+next.getPrice());
29         }
30     }
31 }

 

 

测试结果

 1 ---------------数组 菜类---------------
 2 menu name is :西红柿鸡蛋
 3 menu description is :有营养
 4 menu price is :10.0
 5 menu name is :酸辣白菜
 6 menu description is :好吃
 7 menu price is :11.0
 8 menu name is :酸辣土豆丝
 9 menu description is :特色菜
10 menu price is :12.0
11 ---------------集合 汤类---------------
12 menu name is :紫菜鸡蛋汤
13 menu description is :有营养的汤
14 menu price is :6.0
15 menu name is :豆腐脑
16 menu description is :早餐必备
17 menu price is :7.0
18 menu name is :鱿鱼汤
19 menu description is :补充大脑
20 menu price is :8.0

说明

此处不用java.util下面的迭代器是为了更好的了解迭代器是如何工作的,java.util比我们所实现的迭代器多了一个remove方法,如果有其他需求,自定义的迭代器相比于java.util更易拓展。

祝福