设计模式之迭代器模式


前言

大家好,我是练习两年半的Java练习生,最近阅读了《深入浅出设计模式(中文版)》,学习了各种设计模式,所以想出一个专栏和大家分享一下!
如果大家觉得文章还可以,欢迎关注点赞!后续还会陆续更新!!


一、定义

提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示。

二、类图

image.png

三、应用

3.1 需求

如果有两个不同的数据结构,要使用相同的遍历方式,那么就可以使用迭代器

3.2 分析

在使用迭代器模式时,需要实现以下几个角色:

Iterator(迭代器):定义访问和遍历元素的接口,包括 hasNext() 方法和 next() 方法。
ConcreteIterator(具体迭代器):实现 Iterator 接口,负责遍历集合中的元素。
Aggregate(聚合对象):定义创建迭代器的接口,即 createIterator() 方法。
ConcreteAggregate(具体聚合对象):实现 Aggregate 接口,创建并返回 ConcreteIterator 对象。

3.3 实现

下面是具体的代码:

public interface Iterator<T> {
    boolean hasNext();
    T next();
}

public interface Aggregate<T> {
    Iterator<T> createIterator();
}

public class Book {
    private String name;
    private String author;

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

    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }
}

public class BookShelf implements Aggregate<Book> {
    private List<Book> books;

    public BookShelf() {
        this.books = new ArrayList<>();
    }

    public void addBook(Book book) {
        this.books.add(book);
    }

    public Book getBookAt(int index) {
        return this.books.get(index);
    }

    public int getSize() {
        return this.books.size();
    }

    @Override
    public Iterator<Book> createIterator() {
        return new BookShelfIterator(this);
    }
}

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

    public BookShelfIterator(BookShelf bookShelf) {
        this.bookShelf = bookShelf;
        this.index = 0;
    }

    @Override
    public boolean hasNext() {
        return this.index < this.bookShelf.getSize();
    }

    @Override
    public Book next() {
        Book book = this.bookShelf.getBookAt(this.index);
        this.index++;
        return book;
    }
}

public class Main {
    public static void main(String[] args) {
        BookShelf bookShelf = new BookShelf();
        bookShelf.addBook(new Book("Design Patterns", "Gamma, Helm, Johnson, and Vlissides"));
        bookShelf.addBook(new Book("Clean Code", "Robert C. Martin"));
        bookShelf.addBook(new Book("Code Complete", "Steve McConnell"));

        Iterator<Book> iterator = bookShelf.createIterator();
        while (iterator.hasNext()) {
            Book book = iterator.next();
            System.out.println("Name: " + book.getName() + ", Author: " + book.getAuthor());
        }
    }
}

在上面的示例中,Book 类代表聚合对象中的元素,BookShelf 类是具体的聚合对象,它实现了 createIterator() 方法,用于创建迭代器对象。BookShelfIterator 类是具体的迭代器对象,它实现了 hasNext() 方法和 next() 方法,用于遍历 BookShelf 对象

四、问题

迭代器在java源码中的使用?

在Java集合框架中,几乎所有的集合类都实现了一个Iterator接口,用于提供迭代器模式的支持。通过使用迭代器模式,Java集合类可以在不暴露其内部实现的情况下,让外部代码遍历集合中的元素。

以下是Java中常见集合类的迭代器实现方式:

  • ArrayList、LinkedList:实现了List接口和RandomAccess接口,其中List接口继承了Collection接口,而Collection接口继承了Iterable接口,所以ArrayList和LinkedList都实现了Iterable接口和Iterator接口。
  • HashSet、LinkedHashSet:实现了Set接口,Set接口继承了Collection接口,Collection接口继承了Iterable接口,所以HashSet和LinkedHashSet都实现了Iterable接口和Iterator接口。
  • HashMap、LinkedHashMap、TreeMap:实现了Map接口,Map接口继承了Iterable接口,所以这些类都实现了Iterable接口。同时,这些类还提供了对Entry集合的迭代器,即实现了Map.Entry接口和Iterator接口。


五、总结

以上就是今天要讲的内容,本文介绍了设计模式中迭代器模式,主要作用就是遍历不同的集合对象,方便用户使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值