设计模式行为迭代器_第1页
设计模式行为迭代器_第2页
设计模式行为迭代器_第3页
设计模式行为迭代器_第4页
设计模式行为迭代器_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

设计模式韩明

2015年11月02日3迭代器模式(Iterator)Provide

a

way

to

access

the

elements

of

an

aggregate

objectsequentially

without

exposing

its

underlying

representation.提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示。聚合:指一组对象的组合结构,比如:Java中的集合,数组等迭代器模式(Iterator)--概述迭代器模式(Iterator)--概述模式动机一个聚合对象,如一个列表(List)或者一个集合(Set),应该提供一种方法来让别人可以访问它的元素,而又不需要暴露它的内部结构。针对不同的需要,可能还要以不同的方式遍历整个聚合对象,但是我们并不希望在聚合对象的抽象层接口中充斥着各种不同遍历的操作。怎样遍历一个聚合对象,又不需要了解聚合对象的内部结构,还能够提供多种不同的遍历方式,这就是迭代器模式所要解

决的问题。迭代器模式(Iterator)--动机模式分析聚合是一个管理和组织数据对象的数据结构。聚合对象主要拥有两个职责:一是存储内部数据;二是遍历内部数据。存储数据是聚合对象最基本的职责。将遍历聚合对象中数据的行为提取出来,封装到一个迭代器中,通过专门的迭代器来遍历聚合对象的内部数据,这就是迭代器模式的本质。迭代器模式是“单一职责模式”的完美体现。迭代器模式(Iterator)--分析public

static

void

print(Collection

coll){Iterator

it

=coll.iterator();while(it.hasNext()){String

str

=

(String)it.next();System.out.println(str);}}这个方法的作用是循环打印一个字符串集合,里面就用到了迭代器模式,java语言已经完整

地实现了迭代器模式,Iterator翻译成汉语就是迭代器的意思。提到迭代器,首先它是与集合

相关的,集合也叫聚集、容器等,我们可以将集合看成是一个可以包容对象的容器,例如List,Set,Map,甚至数组都可以叫做集合,而迭代器的作用就是把容器中的对象一个一个地遍历

出来。迭代器模式(Iterator)--分析模式的结构中包括四种角色:迭代器(Iterator)角色:定义访问和遍历元素的接口,一般来说会有这么三个方法:取得第一个元素的方法first(),取得下一个元素的方法next(),判断是否遍历结束的

方法isDone()(或者叫hasNext()),移出当前对象的方法remove(),具体迭代器(ConcreteIterator)角色:具体的迭代器实现对象,实现对聚合对象的遍历,并跟踪遍历时的当前位置。聚合,即集合或容器类(AbstractAggregate)角色:定义创建相应迭代器对象的接口。一般是一个接口,提供一个iterator()方法,例如java中的Collection接口,List接口,Set接口等。具体聚合(Concrete

Aggregate)角色:就是抽象容器的具体实现类,比如List接口的有序列表实现ArrayList,List接口的链表实现LinkList,Set接口的哈希列表的实现

HashSet等。迭代器模式(Iterator)--角色描述迭代器模式(Iterator)--类图抽象容器接口public

interface

Aggregate

{public

void

add(Object

obj);public

void

remove(Object

obj);public

Iteratoriterator();}迭代器模式(Iterator)--源码1聚合实现类public

class

ConcreteAggregate

implements

Aggregate

{private

List

list

=

new

ArrayList();public

void

add(Object

obj)

{list.add(obj);}publicIterator

iterator()

{return

new

ConcreteIterator(list);}public

void

remove(Object

obj)

{list.remove(obj);}}迭代器模式(Iterator)--源码1遍历器接口public

interface

Iterator{public

Object

next();public

boolean

hasNext();}迭代器模式(Iterator)--源码1遍历器的实现类public

class

ConcreteIterator

implements

Iterator{private

List

list

=

new

ArrayList();privateint

cursor

=0;public

ConcreteIterator(List

list){this.list

=list;}public

boolean

hasNext(){if(cursor==list.size()){return

false;}return

true;}public

Object

next()

{Object

obj

=null;if(this.hasNext()){obj=

this.list.get(cursor++);}return

obj;}}迭代器模式(Iterator)--源码1client类public

class

Client

{publicstatic

void

main(String[]

args){Aggregate

ag

=

new

ConcreteAggregate();ag.add("小明");ag.add("小红");ag.add("小刚");Iterator

it

=ag.iterator();while(it.hasNext()){String

str

=

(String)it.next();System.out.println(str);}}}迭代器模式(Iterator)--源码1把书籍放(Book)书架(BookShelf)上,并依次输出书名迭代器模式(Iterator)--实例2名称说明Aggregate已聚合的接口Iterator执行递增、遍历的接口Book表示书籍的类BookShelf表示书架的类BookShelfIterator扫描书架的类迭代器模式(Iterator)--实例2,类接口清单迭代器模式(Iterator)--源码2抽象容器接口publicinterface

Aggregate{public

abstract

Iterator

iterator();}遍历某个聚合时,利用iterator方法可建立一个实现Iterator接口的类对象public

class

Book{

private

String

name=“”;Public

Book

(String

name){=name;}public

String

getName(){return

name;}}迭代器模式(Iterator)--源码2public

class

BookShelf

implements

Aggregate{private

Book[]

books;private

int

last

=

0;private

BookShelf(int

maxsize){this.books=new

Book[maxsize];}private

Book

getBookAt(int

index){returnbooks[index];}public

void

appendBook(){this.books[last]=book;last++;}public

intgetLength(){returnlast;}public

Iterator

iterator(){return

new

BookShelfIterator(this);}}迭代器模式(Iterator)--源码2public

interface

Iterator{public

abstract

boolean

hasNext();public

abstract

Object

next();}hasNext()方法检查有没有“下一个元素”

next()方法取得“下一个元素”迭代器模式(Iterator)--源码2public

class

BookShelfIterator

implementsIterator{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++;returnbook;}}迭代器模式(Iterator)--源码2bookShelf是BookShelfIterator所扫描的书架,而

index字段是指向当前书的下标是否有下一本书的判断标准根据index是否小于书架上书籍的数量next方法先把书籍保存到book变量,然后把index推到下一个位置迭代器模式(Iterator)--源码2public

class

Main{public

static

void

main(String[]args){BookShelf

bs=new

BookShelf(4);bs.appendBook(new

Book(“book1”));bs.appendBook(new

Book(“book1”));bs.appendBook(new

Book(“book1”));bs.appendBook(new

Book(“book1”));Iterator

it=

bs.iterator();while(it.hasNext()){Book

book=(Book)it.next();System.out.println(“”+book.getName());}}}迭代器模式(Iterator)--源码2迭代器模式(Iterator)--效果迭代器模式有三个重要的作用:它支持以不同的方式遍历一个聚合:复杂的聚合可用多种方式进行遍历。例如,代码生成和语义检查要遍历语法分析树。代码生成可以按中序或者按前序来遍历语法分析树。迭代器模式使得改变遍历算法变得很容易:仅需用一个不同的迭代器的实例代替原先的实例即可。你也可以自己定义迭代器的子类以支持新的遍历。迭代器简化了聚合的接口:有了迭代器的遍历接口,聚合本身就不再需要类似的遍历接口

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论