Iterator Pattern 学习笔记

学习提速专享!AI工具全家桶免费用 限时购周边加赠Coding Plan Lite,解锁20+主流AI工具,写代码、查资料快人一步! 阅读详情

 Iterator Pattern 是指依序遍历并处理多个数字或变量.iterator为迭代器.
Aggregate接口
是一个执行递增的"聚合".实现此接口的类就变成类似数组的"多个数字或者变量等的聚合".
Aggregate 表示已聚合的接口
Iterator  执行递增,遍历的接口
Book      表示书籍的类
BookShelf 表示书架的类
BookShelfIterator 扫描书架的类
Main      测试用类

为什么要使用Iterator模式,为什么不用for循环进行处理?
因为利用Iterator可以跟实现分开,单独进行递增.
在迭代的过程总,只用到了hasNext()和next()方法,而跟BookShelf无关,如果以后把BookShelf利用java.util.Vector实现,也不会影响程序运行。

package com.sw.IteratorPattern;
/**
 * 迭代器参与者
 * 定义访问和遍历元素的接口。
 * @author sw
 *
 */
public interface Iterator {
 public abstract boolean hasNext();
 public abstract Object next();
}

package com.sw.IteratorPattern;
/**
 * 实现Iterator接口即可将BookShelfIterator视为Iterator进行处理。
 * BookShelf字段为BookShelfIterator所扫描的书架,而index字段则是指向目前该书的下标
 * 具体迭代器,实际上实现Iterator所定义的接口。
 * @author sw
 *
 */
public class BookShelfIterator implements Iterator{
 private BookShelf bookShelf ;
 private int index ;
 public BookShelfIterator(BookShelf bookShelf){
  this.bookShelf = bookShelf ;
  this.index = 0;
 }
 public boolean hasNext(){
  if(index <bookShelf.getLength()){
   return true ;
  }else{
   return false ;
  }
 }
 public Object next(){
  Book book = bookShelf.getBookAt(index);
  index++ ;
  return book ;
 }
}

package com.sw.IteratorPattern;
/**
 * Aggregate(聚合)参与者
 * 定义建立Iterator参与者的接口。
 * @author sw
 *
 */
public interface Aggregate {
 public abstract Iterator iterator();
}

package com.sw.IteratorPattern;
/**
 * BookShelf类
 * 实现了Aggregate接口。
 * 实现其迭代器操作。
 * ConcreteAggregate(具体聚合)参与者
 * @author sw
 *
 */
public class BookShelf implements Aggregate{
 private Book[] books ;
 private int last = 0;
 public BookShelf(int maxsize){
  this.books = new Book[maxsize];
 }
 public Book getBookAt(int index){
  return books[index];
 }
 public void appendBook(Book book){
  if(this.last >= this.books.length){
   System.out.println("该书架不能容纳超过["+this.books.length+"]本书!");
   System.out.println("需要加入的名字为["+book.getName()+"]的书未加入书架!");
  }else{
   this.books[last++] = book ;
  }
 }
 public int getLength(){
  return last ;
 }
 public Iterator iterator(){
  return new BookShelfIterator(this);
 }
}

package com.sw.IteratorPattern;
/**
 * Book类
 * @author sw
 *
 */
public class Book {
 private String name ="";
 public Book(String name){
  this.name = name ;
 }
 public String getName(){
  return name ;
 }
}

package com.sw.IteratorPattern;
/**
 * 测试程序
 * @author sw
 *
 */
public class main {
 public static void main(String[] args){
  BookShelf bs = new BookShelf(4);
  bs.appendBook(new Book("哈里波特"));
  bs.appendBook(new Book("指环王"));
  bs.appendBook(new Book("蜘蛛侠1"));
  bs.appendBook(new Book("蜘蛛侠2"));
  bs.appendBook(new Book("蜘蛛侠3"));
  System.out.println("输出书架所有的书籍:");
  Iterator it = bs.iterator();
  while(it.hasNext()){
   Book bk = (Book)it.next();
   System.out.println(""+bk.getName());
  }
 }
}

 

迭代器模式(Interator Pattern 一、模式动机 迭代器模式(Iterator Pattern)是一种使用频率非常高的行为型设计模式,迭代器用于对一个聚合对象进行遍历。通过引入迭代器可以将数据的遍历功能从聚合对象中分离出来,聚合对象只负责存储数据,而遍历数据由迭代器来完成,简化了聚合对象的设计,更符合“单一职责原则”的要求。Java语言提供了对迭代器模式的完美支持,通常我们不需要自己定义新的迭代器,直接使用Java提供的迭代器即可... 阅读详情

相关推荐

7.8 迭代器模式 (Iterator pattern)

迭代器模式

NorthStar的博客 465

设计模式之迭代器模式详解(Iterator Pattern

认识迭代器模式 在系统开发中,集合对象内部表示各不相同。底层构造也尽不相同。对于这些对象,我们希望在不暴露其底层和内部表示的同时,可以使外部客户访问其中元素。 迭代器模式就为这一需求提供了极其优雅的实现,接下来就让我们来认识认识迭代器模式: 迭代器模式,提供一种遍历集合元素的统一接口,用一致的方法遍历集合元素,不需要知道集合对象的底层表示,即:不暴露其内部的结构。 迭代器模式(Iterato...

Superman_peng的博客 531

Java设计模式——迭代器模式【Iterator Pattern

Java设计模式、迭代器模式、Iterator Pattern

scorpio的博客 495

迭代器模式-Iterator Pattern

在现实生活以及程序设计中,经常要访问一个聚合对象中的各个元素,通常的做法是将链表的创建和遍历都放在同一个类中,但这种方式不利于程序的扩展,如果要更换遍历方法就必须修改程序源代码,这违背了 “开闭原则”。 在软件开发中,我们经常需要使用聚合对象来存储一系列数据。聚合对象拥有两个职责:一 是存储数据;二是遍历数据。从依赖性来看,前者是聚合对象的基本职责;而后者既是可变 化的,又是可分离的。因此,可以...

心猿意碼 4419

Design Pattern学习笔记之迭代器模式和复合模式(the Iterator and Composite Pattern

Design Pattern学习笔记之迭代器模式和复合模式(the Iterator and Composite Pattern) 1.    引子--Whois? 我们有很多种将对象塞入集合的方式,可以用array、stack、list、hashtable等等,以上的每一种都各有优缺点。但是当我们考虑遍历这些集合的问题时,你是否打算向外部暴露你如何实现集合的细节(array or stack

wqh807020的专栏 738

设计模式学习笔记--Iterator Pattern迭代者模式

所谓迭代者模式就是:提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。[GOF 《设计模式》] 在软件的构建过程中,集合对象(也就是聚集:是一组数据集或者对象集,它可以通过循环来访问 )内部结构常常变化各异。但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码透明地访问其中包含的元素;同时这种“透明遍历”也为“同一种算法在多种集合对象上...

weixin_30587025的博客 70

07/11/2014 J2EE 学习笔记 Design Pattern Observer,Singleton et Iterator

Observeur  N Observable 1 LibData: Observable AfficheCourant: AfficheStat: AffichePrev: Chaque Abonne a son Editeur. Pattern Singleton: 有创建出多个实例的风险。所以需要syn

qiriga的专栏 707

[分享]java.util包的学习笔记

[分享]java.util包的学习笔记一学习了很多东西后,不得不回头再次重温基础内容,开始从util包学习开始,我会陆续的把学习笔记贴上来跟大家分享,同时也是对自己的监督。由于工作的原因,学习进度无法得到保证,可能不能及时更新还请见谅..1.Random,ObservableRandom:实现接口:serializable子类:SecureRandom如果用相同的s

为无为,事无事,味无味。 979

学习笔记【23种设计模式-第十六节:Iterator Pattern 迭代器模式】

Iterator Pattern 迭代器模式 简述: 迭代器模式是常用的设计模式,属于行为型模式 如果我们的集合元素使用不同的方式实现的,有数组,还有java的集合类,或者还有其他方式,当客户端要遍历这些集合元素的时候就要使用多种遍历方式,而且还会暴露元素的内部结构,可以考虑使用迭代器模式解决。 迭代器模式,提供一种遍历集合元素的统一接口,用一只的方法遍历集合元素,不需要知道集合对象的底层表示,即:不暴露其内部的结构。 角色 Iterator:迭代器接口,是系统提供的 ConcreteIterator

qq_53273362的博客 138

设计模式学习笔记---迭代器模式iterator pattern(Java版)

一、实质 提供一种可以遍历聚合对象的方式。又称游标模式 聚合对象:存储数据 迭代器:遍历数据 二、示例

吃果冻不吐果冻皮 1232

design pattern——iterator

design pattern——iterator

mardax的专栏 358

迭代器模式(Iterator Pattern

迭代器模式(Iterator Pattern)提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示。   简单的说,使用迭代器的遍历方法遍历目标对象,而不是从对象直接获取。 获取迭代器的方法有两种(也许有更多),如JDK中集合对象可以直接获取内部迭代器;也可以采用将对象以参数的形式传入到迭代器对象中。下面看代码: 1,聚合对象 /** * 内部封...

136

GoF Design PatternIterator

Introduces the application of the Iterator Pattern in Java, demonstrating its implementation through a book management system example. The Iterator Pattern is used to traverse elements in a collection without exposing its internal structure.

2301_76333240的博客 923

C#设计模式:迭代器模式(Iterator Pattern

一,什么是迭代器模式(Iterator Pattern) 提供一种方法顺序访问一个容器对象中的各个元素,而又不需要暴露该对象的内部表示 二,看下面例子: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading....

dcujcivw769565的博客 164

Iterator Pattern迭代器模式

Iterator Pattern迭代器模式:首先了解两个接口:1. Iterable接口:实现该接口的集合对象是可迭代遍历的public interface Iterable&lt;T&gt; {    ...    Iterator&lt;T&gt; iterator();}2.Iterator接口:迭代器public interface Iterator&lt;E&gt; {    boole...

zqxhit1234的博客 287

python:Iterator Pattern

本文介绍了Python实现的迭代器模式(Iterator Pattern),包含以下核心组件: Jewel珠宝实体类封装珠宝属性 Iterator抽象迭代器定义核心方法 JewelryIterator具体迭代器实现遍历逻辑 Aggregate抽象聚合类定义创建迭代器方法 JewelryBox具体聚合类管理珠宝集合 实现特点: 支持多种遍历方式(for循环、生成器表达式、手动迭代) 隐藏集合内部实现细节 符合Python迭代器协议(iter__和__next) 提供完整珠宝属性展示功能 该模式使客户端代码能统

Geovin Du Dream Park 214

设计模式:迭代器模式(Iterator Pattern

/** * 迭代器模式。 * @author Bright Lee */ public class IteratorPattern { public static void main(String[] args) { MenuItem[] menuItems = new MenuItem[] { new MenuItem(&quot;红烧肉&quot;), new MenuItem(&quot;鱼...

look4liming的专栏 248
上一篇: 漂在北京
下一篇: Factory Method Pattern 工厂模式 学习笔记
anyoneking
博客等级 码龄19年 406粉丝 87原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值