设计模式一:迭代器(Iterator)模式

一、什么是迭代器模式

  说白了就是一种遍历集合中元素的一种设计模式,我们赶紧先来看一下例子

二、实现举例

  这里我们举一个例子,是将书(Book)放置到书架中(BookShelf),一个迭代器,我们总共要写2个接口,分别是一个集合接口(Agreegate),一个迭代器接口(Iterator),两个接口对应的实现类,以及要被遍历的对象对应的类

三、迭代器模式(Iterator)的实现步骤:

1.我们需要有一个集合接口,在这个接口里面定义一个Iterator方法,用于生成遍历集合的迭代器(Iterator)

1 package com.Maxwell07.IteratorMode.bookshelf;
2
3 /**
4  * 1.定义一个集合接口,用于生成遍历集合的Iterator
5  */
6
7 public interface Aggregate {
8     public abstract Iterator iterator();
9 }

interface Aggregate

2.写一个Iterator接口,Iterator接口包含hasNext和next的抽象方法

1 package com.Maxwell07.IteratorMode.bookshelf;
2 /**
3  * 2.写一个Iterator接口,Iterator接口包含hasNext和next的抽象方法
4  */
5
6 public interface Iterator {
7     public abstract boolean hasNext();
8     public abstract Object next();
9 }

interface Iterator

3.写一个要被遍历的类,这里我们写一个Book

package com.Maxwell07.IteratorMode.bookshelf;

public class Book {
    private String name;

    public Book(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

class Book

4.写一个集合的实现类,这里我们写一个实现了Aggreegate接口的BookShelf,它可以看做是Book的一个集合类

 1 package com.Maxwell07.IteratorMode.bookshelf;
 2
 3 public class BookShelf implements Aggregate {
 4     private Book[] books;
 5     private int last = 0;
 6
 7     public BookShelf(int maxSize){
 8         this.books = new Book[maxSize];
 9     }
10     public Book getBookAt(int index){
11         return books[index];
12     }
13
14     public void appendBook(Book book){
15         this.books[last] =book;
16         last++;
17     }
18
19     public int getLength(){
20         return last;
21     }
22     //实现接口中的迭代器
23     @Override
24     public Iterator iterator() {
25         return new BookShelfIterator(this);
26     }
27 }

class BookShelf implements Aggregate

5.最后我们再写一个Iterator接口的实现类,也就是集合的迭代器,在这里我们把它命名为BookShelfIterator

package com.Maxwell07.IteratorMode.bookshelf;

public class BookShelfIterator implements Iterator {
    private BookShelf bookShelf;
    private int index;

    public BookShelfIterator(BookShelf bookShelf) {
        this.bookShelf = bookShelf;
        this.index = 0;
    }
    //判断如果当前的index小于数组的长度,则返回true,否则返回false
    @Override
    public boolean hasNext() {
        if (index < bookShelf.getLength()) {
            return true;
        } else {
            return false;
        }
    }
    //返回当前索引对应的对象
    @Override
    public Object next() {
        Book book = bookShelf.getBookAt(index);
        index++;
        return book;
    }
}

6.我们验证一下迭代器

package com.Maxwell07.IteratorMode.bookshelf;

public class Main {
    public static void main(String[] args){
        BookShelf bookShelf = new BookShelf(4);
        bookShelf.appendBook(new Book("三国演义"));
        bookShelf.appendBook(new Book("水浒传"));
        bookShelf.appendBook(new Book("西游记"));
        bookShelf.appendBook(new Book("红楼梦"));
        Iterator iterator = new BookShelfIterator(bookShelf);
        while (iterator.hasNext()) {
            Book book = (Book) iterator.next();
            System.out.println("书架上有一本:"+book.getName());
        }
    }
}

测试类

  文中的代码链接:https://github.com/Maxwell07/DesignMode/tree/master/src/com/Maxwell07/IteratorMode/bookshelf

四、为什么我们要用迭代器模式?

为了遍历集合,我们引入了一种这么复杂的设计模式,并且在写的时候容易出错,但是我们仔细想一下,引入迭代器以后,实际上我们把实现和遍历分开了,遍历并不会再依赖于实现。正如上面的例子,我们把数组换成集合,main函数中遍历的代买还是不需要再做修改

五、源码中的迭代器模式

//todo 待更新

原文地址:https://www.cnblogs.com/MarcoLin/p/9346169.html

时间: 2024-08-29 08:52:53

设计模式一:迭代器(Iterator)模式的相关文章

【设计模式大法】Iterator模式

Iterator模式 --一个一个遍历 在Java中的for语句中 i++的作用是让 i 的值在每次循环后自增1,这样就可以访问数组中的下一个元素.下下一个元素.再下下一个元素,也就实现了从头至尾逐一遍历数组元素的功能. 将这里的循环变量 i的作用抽象化.通用化后形成的模式,在设计模式中称为 Iterator 模式. 示例程序 Aggregate接口 Aggregate接口是索要遍历的集合的接口.实现了该接口的类将称为一个可以保持多个元素的集合. public interface Aggrega

Java 实现迭代器(Iterator)模式

类图 /** * 自定义集合接口, 类似java.util.Collection * 用于数据存储 * @author stone * */ public interface ICollection<T> { IIterator<T> iterator(); //返回迭代器 void add(T t); T get(int index); } /** * 自定义迭代器接口 类似于java.util.Iterator * 用于遍历集合类ICollection的数据 * @author

设计模式—迭代器Iterator模式

首先我们先模仿集合中ArrayList和LinkedList的实现.一个是基于数组的实现.一个是基于链表的实现,实现方式各有不同, 为了减少代码的耦合度,面向接口编程.定义Collection接口定义API规范. 可是在遍历集合中的元素时,由于数组和链表的遍历方式不一样,能不能统一处理呢? 再定义一个Iterator接口,让每一个子类集合都实现接口中的方法. 1.接口Collection.java public interface Collection<E> { public void add

设计模式——迭代器(Iterator)模式

概述 迭代器模式简单的说(按我目前的理解)就是一个类提供一个对外迭代的接口,方面调用者迭代.这个迭代接口至少包括两个方法:hasNext()--用于判断是否还有下一个,next()--用于取出下一个对象(或值).而外部使用这个类(取出这个类中的对象或值)时,不用关心这个类存储对象或数据的具体数据结构,即使这个类的存储数据结构临时发生改变,调用者不作任何代码修改仍然可以正常工作.从而实现代码的可重用性和低耦合性.下面以实例说明. 实例 假设有两个书架(BookShelf),一个书架用数组Array

设计模式 - 迭代器模式(iterator pattern) Java 迭代器(Iterator) 详解

迭代器模式(iterator pattern) Java 迭代器(Iterator) 详解 本文地址: http://blog.csdn.net/caroline_wendy 参考迭代器模式(iterator pattern): http://blog.csdn.net/caroline_wendy/article/details/35254643 Java的标准库(util)中包含迭代器接口(iterator interface), import java.util.Iterator; 继承(

设计模式之迭代器模式--- Pattern Iterator

模式的定义 迭代器模式定义: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 提供一种方法访问一个容器对象中各个元素,而又不需要暴露对象的内部细节. 类型 行为类 模式的使用场景 方便遍历访问容器内的元素 优点 面向对象设计原则中的单一职责原则,对于不同的功能,我们要尽可能的把这个功能分解出单一的职责,不

设计模式 - 组合模式(composite pattern) 迭代器(iterator) 详解

组合模式(composite pattern) 迭代器(iterator) 详解 本文地址: http://blog.csdn.net/caroline_wendy 参考组合模式(composite pattern): http://blog.csdn.net/caroline_wendy/article/details/36895627 在组合模式(composite pattern)添加迭代器功能, 遍历每一个组合(composite)的项. 具体方法: 1. 抽象组件类(abstract

设计模式17:Iterator 迭代器模式(行为型模式)

Iterator 迭代器模式(行为型模式) 动机(Motivation) 在软件构建过程中,集合对象内部结构常常变化各异.但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码可以透明地访问其包含的元素:同时这种“透明变量”也为“同一种算法在多种集合对象上进行操作”提供了可能. 使用面向对象技术使这种遍历机制抽象为“迭代器对象”为“应对变化中的集合对象”提供了一种优雅的方式. 意图(Intent)提供一种方法顺序访问一个聚合对象中各个元素 , 而又不需暴露该对象的内部表示.——

设计模式之迭代器模式(Iterator)

1.定义 迭代器模式提供一种方法访问一个容器对象中的各个元素,而又不需暴露该对象的内部细节. 基本上没有人会单独写一个迭代器,除非是产品性质的开发. 2.通用类图 Iterator抽象迭代器:抽象迭代器负责定义访问和遍历元素的接口,而且基本上是有固定的3个方法:first()获得第一个元素:next()访问下一个元素:isDone()是否已经访问到底部(Java 叫做hasNext()方法). ConcreteIterator具体迭代器:具体迭代器角色要实现迭代器接口,完成容器元素的遍历. Ag