java design pattern iterator pattern

Design pattern iterator pattern
    Provides a way to access elements of an aggregate object in sequence, without exposing the internal representation of the object
    Nature: elements that control access to aggregate objects, transparent access elements

In fact, we are familiar with this pattern, because there are iterators in java, and we use a lot of them.

Previous class diagram:

Use the java List iterator to explain the class diagram.

Aggregate is an aggregate abstract class, corresponding to List

ConcreteAggregate concrete aggregate class, corresponding to ArrayList

Iterator iterative abstract class corresponds to java iterator class

ConcreteIterator specific iteration class, which corresponds to the class corresponding to the object obtained by list.iterator(). According to the source code, it is the internal class of ArrayList class, Itr()


Let's see how to use it first:

         System.out.println("java List Using Iterators ");
        List<String> dataList = new ArrayList<>();
        Iterator iterator = dataList.iterator();
        while (iterator.hasNext()){

The results are as follows:


As a matter of fact, you already know what an iterator is, and the iterator pattern is not very common.

Continue to see the use of custom iterators.

Entity class:

public class Item {
    private String id;
    private String name;

    public Item(String id,String name){ = id; = name;

    public String toString() {
        return "Item{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
    //getter and setter omitted


Aggregate class

It's a set. If this writes generics, it will be more general

 * item Aggregate class
public class ItemAggregate {
    private List<Item> dataList;

    public ItemAggregate(){
        dataList = new ArrayList<>();
    //getter and setter

    public List<Item> getDataList() {
        return dataList;

    public void setDataList(List<Item> dataList) {
        this.dataList = dataList;

Custom iterator class:

import java.util.Iterator;
import java.util.function.Consumer;

public class ItemIterator implements Iterator<Item> {

    private ItemAggregate itemAggregate;
    private int index = 0;

    public ItemIterator( ItemAggregate itemAggregate){
        this.itemAggregate = itemAggregate;

    public boolean hasNext() {
        return itemAggregate.getDataList().size() > index;

    public Item next() {
        return itemAggregate.getDataList().get(index++);

    public void remove() {

    public void forEachRemaining(Consumer<? super Item> action) {


        System.out.println("Custom iterator");
        ItemAggregate itemAggregate = new ItemAggregate();
        itemAggregate.getDataList().add(new Item("1","name1"));
        itemAggregate.getDataList().add(new Item("2","name2"));
        itemAggregate.getDataList().add(new Item("3","name3"));
        ItemIterator itemIterator = new ItemIterator(itemAggregate);
        while (itemIterator.hasNext()){

Test results:

Item{id='1', name='name1'}
Item{id='2', name='name2'}
Item{id='3', name='name3'}


It can be seen that the use method is still the same, which is very simple.

Published 56 original articles, won praise 6, visited 20000+
Private letter follow

Tags: Java

Posted on Thu, 05 Mar 2020 01:12:40 -0800 by kodstationen