23 行为型模式-迭代器模式

1 迭代器模式介绍

迭代器模式是我们学习一个设计时很少用到的、但编码实现时却经常使用到的行为型设计模式。在绝大多数编程语言中,迭代器已经成为一个基础的类库,直接用来遍历集合对象。在平时开发中,我们更多的是直接使用它,很少会从零去实现一个迭代器。
迭代器模式(Iterator pattern)又叫游标(Cursor)模式,它的原始定义是:迭代器提供一种对容器对象中的各个元素进行访问的方法,而又不需要暴露该对象的内部细节。

在软件系统中,容器对象拥有两个职责: 一是存储数据,而是遍历数据.从依赖性上看,前者是聚合对象的基本职责.而后者是可变化的,又是可分离的.因此可以将遍历数据的行为从容器中抽取出来,封装到迭代器对象中,由迭代器来提供遍历数据

的行为,这将简化聚合对象的设计,更加符合单一职责原则。

2 迭代器模式原理

迭代器模式结构图

迭代器模式主要包含以下角色:

3 迭代器模式实现
java 复制代码
/**
 * 迭代器接口
 **/
public interface Iterator<E> {

    //判断集合中是否有下一个元素
    boolean hasNext();

    //将有游标后移一位
    void next();

    //返回当前游标指定的元素
    E currentItem();
}
java 复制代码
/**
 * 具体的迭代器
 **/
public class ConcreteIterator<E> implements Iterator<E> {

    private int cursor; //游标

    private ArrayList<E> arrayList;  //容器

    public ConcreteIterator(ArrayList<E> arrayList) {
        this.cursor = 0;
        this.arrayList = arrayList;
    }

    @Override
    public boolean hasNext() {
        return cursor != arrayList.size();
    }

    @Override
    public void next() {
        cursor++;
    }

    @Override
    public E currentItem() {
        if(cursor >= arrayList.size()){
            throw new NoSuchElementException();
        }
        return arrayList.get(cursor);
    }
}
java 复制代码
public class Test01 {

    public static void main(String[] args) {

        ArrayList<String> names = new ArrayList<>();
        names.add("lisi");
        names.add("zhangsan");
        names.add("wangwu");

//        Iterator<String> iterator = new ConcreteIterator<>(names);
//        while(iterator.hasNext()){
//            System.out.println(iterator.currentItem());
//            iterator.next();
//        }

        java.util.Iterator<String> iterator1 = names.iterator();
        while(iterator1.hasNext()){
            System.out.println(iterator1.next());
        }
    }
}
4 迭代器模式应用实例
java 复制代码
/**
 * 抽象迭代器
 **/
public interface IteratorIterator<E> {

    void reset();  //重置为第一个元素

    E next(); //获取下一个元素

    E currentItem();  //检索当前元素

    boolean hasNext(); //判断集合中是否还有下一个元素
}
java 复制代码
/**
 * 抽象集合
 **/
public interface ListList<E> {

    //获取迭代器对象的抽象方法
    IteratorIterator<E> iterator();
}
java 复制代码
/**
 * 主题类
 **/
public class Topic {

    private String name;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
java 复制代码
/**
 * 具体迭代器
 **/
public class TopicIterator implements IteratorIterator<Topic> {

    //Topic数组
    private Topic[] topics;

    //记录存储位置的有游标
    private int position;

    public TopicIterator(Topic[] topics) {
        this.topics = topics;
        position = 0;
    }

    @Override
    public void reset() {
        position = 0;
    }

    @Override
    public Topic next() {
        return topics[position++];
    }

    @Override
    public Topic currentItem() {
        return topics[position];
    }

    @Override
    public boolean hasNext() {
        if(position >= topics.length){
            return false;
        }
        return true;
    }
}
java 复制代码
/**
 * 具体集合类
 **/
public class TopicList implements ListList<Topic>{

    private Topic[] topics;

    public TopicList(Topic[] topics) {
        this.topics = topics;
    }

    @Override
    public IteratorIterator<Topic> iterator() {
        return new TopicIterator(topics);
    }
}
java 复制代码
public class Client {

    public static void main(String[] args) {
        Topic[] topics = new Topic[4];
        topics[0] = new Topic("t1");
        topics[1] = new Topic("t2");
        topics[2] = new Topic("t3");
        topics[3] = new Topic("t4");

        TopicList topicList = new TopicList(topics);
        IteratorIterator<Topic> iterator = topicList.iterator();

        while(iterator.hasNext()){
            Topic topic = iterator.next();
            System.out.println(topic.getName());
        }
    }
}
5 迭代器模式总结


3) 使用场景

相关推荐
yangzhi_emo2 分钟前
ES6笔记2
开发语言·前端·javascript
界面开发小八哥9 分钟前
「Java EE开发指南」如何用MyEclipse创建一个WEB项目?(三)
java·ide·java-ee·myeclipse
idolyXyz35 分钟前
[java: Cleaner]-一文述之
java
一碗谦谦粉1 小时前
Maven 依赖调解的两大原则
java·maven
emplace_back1 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk1 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶1 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
netyeaxi1 小时前
Java:使用spring-boot + mybatis如何打印SQL日志?
java·spring·mybatis
xiaolang_8616_wjl1 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源