java设计模式——迭代器模式
来源:互联网 发布:中国大数据信息 编辑:程序博客网 时间:2024/05/14 06:56
迭代器模式
迭代器模式的定义:
提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。
何为“迭代”:指的是每一次对过程的重复,迭代器就是遍历集合,直到集合中的元素被遍历一遍
迭代器模式的参与者:
(1)迭代器接口(iteator):它包含一些抽象方法,利用这些方法可以在集合元素之间游走
(2)具体迭代器(concreteiterator):实现迭代器接口的抽象方法,负责实现迭代操作
(3)聚合(Aggregate ):聚合定义创建相应迭代器的接口
(4)具体聚合(ConcreteAggregate):实现创建相应迭代器的接口,该操作返回ConcreteIterator的一个适当的实例
(5)客户(client):依赖于迭代器接口和具体聚合,他使用迭代器对具体聚合类中的集合进行一些操作
以下是关于迭代器模式的一个代码示例:
public class menuitem { //dinermenu和pancakemenu共用的菜单 String name; String description; boolean vegetarian; double price; public menuitem(String name,String description,boolean vegetarian,double price) { this.name=name; //食物的名字 this.description=description; //对食物进行描述 this.vegetarian=vegetarian; //判断是否为素食 this.price=price; } public String getname() { return this.name; } public String getdescription() { return this.description; } public double getprice() { return this.price; } public boolean isvegetarian() { return vegetarian; }}public interface iterator { //创建一个迭代器接口,利用它来遍历集合内的每个对象 boolean hasnext(); //该方法用于判断集合中是否还有元素 Object next(); //该方法返回一个Oject(所有类都继承自Object)对象}public class dinermenuiterator implements iterator{ //创建一个具体迭代器类,该类负责遍历dinermenu中数组中的各项 menuitem[] items; //声明一个共同菜单数组,由于diner菜单是用数组存放菜单中的各项的,所以在他的迭代器中也是用数组调用菜单中的各项 int position=0; //记录当前数组遍历的位置 public dinermenuiterator(menuitem[] items) { this.items=items; } public boolean hasnext() { if(position>=items.length||items[position]==null) { return false; } else return true; } public Object next() { menuitem menuitem1=items[position]; position++; return menuitem1; //返回数组内的下一项,并且递增其值 }}public class pancakemenuiterator implements iterator{ //创建pancakemenu的迭代器类,该迭代器类负责遍历pancakemenu类的元素集合中的各项 ArrayList arraylists; //由于pancake菜单是用arraylist实现的,所以在他的迭代器中也同样使用arraylist来调用菜单中的各项 int position=0; public pancakemenuiterator(ArrayList arraylists) //传递进来一个arraylist(在本项目中其实是要传递进来一个menuitem数组) { this.arraylists=arraylists; } public boolean hasnext() { if(arraylists.size()<=position) return false; else return true; } public Object next() //该方法是依次取出菜单中的各项 { menuitem menuitem1=(menuitem)arraylists.get(position); //将arraylist中的项转换为menuitem对象 position++; return menuitem1; //将当前项取出 }}public class dinermenu { //这是diner菜单的存储方式(具体聚合,含有一个createiterator方法:返回一个遍历dinermenu集合中各项的迭代器) static final int max_items=6; int numberofitems=0; menuitem[] menuitems; //利用数组存储菜单中的各项 public dinermenu() { menuitems=new menuitem[max_items]; //向菜单中添加四项 additem("豆腐皮","芝麻酱加豆腐皮,是一种素食",true,10); additem("芹菜炒豆腐","芹菜加上豆腐,是一种素食",true,15); additem("红绕肉","红烧猪肉老子最爱,是一种肉食",false,20); additem("绝味鸭脖","鸭脖子加各种调料,是一种肉食",false,25); } public void additem(String name,String description,boolean vegetarian,double price) //这个方法将菜单中的项加到指定数组中 { menuitem menuitem1=new menuitem(name,description,vegetarian,price); //实例化一个菜单对象 if(numberofitems>max_items) { System.err.println("对不起,您不能点这么多东西"); //如果菜单中项数超过了最大项数约束,则输出错误提示 } else { menuitems[numberofitems]=menuitem1; //将菜单中的项加到menuitems数组中 numberofitems++; } } public iterator createiterator() { return new dinermenuiterator(menuitems); //类似于工厂方法模式,产生一个迭代器,将数组当作参数传递给它的迭代器,一遍它的迭代器能够调用集合中的各项 }}public class pancakehousemenu { //具体聚合,含有一个方法:createiterator:该方法返回一个遍历pancakemenu中集合元素的迭代器对象 ArrayList menuitems; public pancakehousemenu() { menuitems=new ArrayList(); //用arraylist存储菜单中的成员 additem("西红柿炒鸡蛋","西红柿搭配鸡蛋的餐点,是一种素食",true,10); additem("麻婆豆腐","豆腐加上各种辣椒,是一种素食",true,15); additem("金酱肉丝","猪肉加各种特色调料,是一种肉食",false,20); additem("孜然羊肉","羊肉加孜然,是一种肉食",false,25); } public void additem(String name,String description,boolean vegetarian,double price) //这个方法为arraylist中添加成员 { menuitem menuitem1=new menuitem(name,description,vegetarian,price); //实例化一个公共菜单对象,利用公共菜单储存菜单中的项目 menuitems.add(menuitem1); //向menuitems中添加上面的菜单对象 } public iterator createiterator() { return new pancakemenuiterator(menuitems); //将集合作为参数传递给它的迭代器,以便于它的迭代器调用集合中的各项 }}public class waitress { //客户类,真正利用迭代器操作集合中元素的类 pancakehousemenu pancakehousemenu1; dinermenu dinermenu1; //这是两个不同菜单的对象引用,后面用各自的迭代器输出菜单中的各项 public waitress(pancakehousemenu pancakehousemenu1,dinermenu dinermenu1) { this.pancakehousemenu1=pancakehousemenu1; this.dinermenu1=dinermenu1; } public void printmenu() //将两个菜单依次输出 { iterator pancakeiterator=pancakehousemenu1.createiterator(); iterator dineriterator=dinermenu1.createiterator(); //这个printmenu方法为每一个菜单创建一个迭代器 System.out.println("MENU\n————\nbreakfast"); printmenu(pancakeiterator); //将pancake迭代器当做参数,输出pancakehousemenu中的各项 System.out.println("\nlunch"); printmenu(dineriterator); } public void printmenu(iterator iterator1) //利用迭代器对集合中的元素进行操作 { while(iterator1.hasnext()) { menuitem menuitem=(menuitem)iterator1.next(); //创建一个公共菜单对象,利用该公用菜单输出两个菜单中的每项值 System.out.print(menuitem.getname()+","); System.out.print(menuitem.getprice()+"----"); System.out.println(menuitem.getdescription()); } }}public class test { public static void main(String[] args) { // TODO code application logic here pancakehousemenu pancakehousemenu1=new pancakehousemenu(); dinermenu dinermenu1=new dinermenu(); waitress waitress1=new waitress(pancakehousemenu1,dinermenu1); waitress1.printmenu(); } }
关于迭代器模式的个人理解:
(1)迭代器模式中的核心是“迭代器类”,它的作用是实现对集合的遍历,迭代器类中的方法控制对集合的遍历。比如上述代码中pancakemenuiterator(迭代器)类中的next方法,控制对类中arraylist集合中元素的遍历,而具体对集合中每一个元素的操作却是在client(客户类)中实现(即迭代器对集合中的元素进行遍历,客户取出集合中的元素进行操作)
(2)迭代器类的构造函数的参数列表中传递进来一个集合作为参数,然后在构造函数体内对其进行初始化。以使迭代器知道他将要对哪种集合进行遍历(如上述代码中两个迭代器分别对数组集合和arraylist集合进行操作)。
(3)迭代器类中一般有两个必不可少的方法:一个方法用于遍历集合中的元素(上述代码中的next方法),一个方法用于控制什么时候完成对集合的遍历操作(上述代码示例中的hasnext方法)。其实我的个人理解:这两方法就类似于for循环。
(4)聚合中往往含有一个createiterator方法,该方法返回一个迭代器对象,利用该迭代器对象,遍历聚合中的集合。(我感觉这一点神似于工厂方法)
(5)迭代器类和具体聚合类中都需要有一个集合(数组或者arraylist等),具体聚合利用createiterator方法,返回一个迭代器对象(并且该迭代器对象以集合为参数,以使该迭代器对象知道他将遍历的是哪一个集合,如:return new pancakemenuiterator(menuitems);)
(6)java设计者考虑得很周到,他想到了用户以后会对集合arraylist进行遍历,所以它直接为arraylist集合设计了一个专用迭代器(先import java.util.Iterator,然后就可以直接用arraylist.iterator()遍历该arraylist集合了)
- JAVA设计模式——迭代器模式
- java设计模式——迭代器模式
- JAVA设计模式——迭代器模式
- JAVA设计模式——迭代器模式
- Java设计模式——迭代器模式
- java设计模式——迭代器模式
- JAVA设计模式—迭代器模式(Iterator)
- java设计模式—迭代器模式
- JAVA设计模式——组合模式+迭代器模式
- Java设计模式—解释器模式&迭代器模式简介
- Java设计模式—工厂设计模式
- java设计模式——代理模式
- JAVA设计模式——工厂模式
- Java设计模式——观察者模式
- Java设计模式——组合模式
- Java设计模式——命令模式
- JAVA设计模式——命令模式
- JAVA设计模式——适配器模式
- 怎样实现同一个账户同一时间只能在一个终端登录
- 点击按钮 “加载更多”效果
- There's no Qt version assigned to this project for platform x64
- Xutils简单介绍
- 网络基础之协议-1
- java设计模式——迭代器模式
- 工厂方法模式
- 所谓IIS500内部服务器错误
- MyBatis动态SQL<choose>标签的使用
- Unity Leap Motion学习笔记
- Eclipse中Ctrl+Shift+F代码格式化快捷键失效的解决方法
- Java配置maven+jenkins+git(svn)+tomcat自动编译和部署(持续集成)
- 一步一步写miscdevice的驱动模块
- getchar()无用