前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >迭代器模式

迭代器模式

作者头像
杨小杰
发布2019-07-04 16:30:17
5340
发布2019-07-04 16:30:17
举报
文章被收录于专栏:JAVA知识总结与分享

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

情节

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

需求

将两个菜单合并显示

两个菜单代码

数组菜单

代码语言:javascript
复制
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集合菜单

代码语言:javascript
复制
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;
    }
}

当前合并测试类

代码语言:javascript
复制
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());
        }
    }
}

整合后

创建迭代器

代码语言:javascript
复制
package com.iteratorPattern.Iterator;

/**
 * @program: test
 * @description: 迭代器接口
 * @author: Mr.Yang
 * @create: 2019-01-01 15:44
 **/
public interface Iterator {
    boolean hasNext();
    Object next();
}

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

代码语言:javascript
复制
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;
    }
}

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

代码语言:javascript
复制
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迭代器

代码语言:javascript
复制
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集合菜单添加创建迭代器的方法

代码语言:javascript
复制
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);
    }
}

测试

代码语言:javascript
复制
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());
        }
    }
}

测试结果

代码语言:javascript
复制
---------------数组 菜类---------------
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更易拓展。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 JAVA知识总结与分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 情节
  • 需求
  • 两个菜单代码
  • 整合后
  • 说明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档