On Iterator pattern of design pattern

Iterative model of design pattern

Iterative pattern is a relatively simple design pattern among design patterns

preface

Tip: Here you can add the general contents to be recorded in this article:
For example, with the continuous development of artificial intelligence, machine learning technology is becoming more and more important. Many people have started learning machine learning. This paper introduces the basic content of machine learning.

Tip: the following is the main content of this article. The following cases can be used for reference

1, What is the iterator pattern

Iterator pattern: in the java for loop, we often use loop variables to traverse the array, such as the frequently used variable i. i will increase or decrease after each loop, so as to realize the traversal of the array. We abstract and generalize the function of cyclic variable i. The pattern formed after generalization is called iterator pattern.


1. Iterator: it is responsible for defining an API that traverses elements one by one in order.
2. Concreteiterator (concrete Iterator): it is responsible for defining and implementing the API defined by the Iterator role.
3. Aggregate (set): it is responsible for defining the API for creating the Iterator role.
4. Concreteaggregate (concrete Collection): responsible for implementing the API defined by Aggregate.

2, Specific examples

We use the relationship between book and bookshelf to explore the iterative pattern.
The specific process is to put the book on the shelf and then take the book.

1. Define Book class

The code is as follows:

package com.wwh.iterator_model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
    private String name;
}

2. Define the iterator and aggregate interfaces

Iterator:

package com.wwh.iterator_model;

public interface Iterator {
    public abstract boolean hasNext();
    public abstract Object next();
}

Aggregate:

package com.wwh.iterator_model;

public interface Aggregate {
    public abstract Iterator iterator();
}

3. Define the BookShelf class (equivalent to ConcreteAggregate)

The code is as follows:

package com.wwh.iterator_model;

import java.util.ArrayList;
import java.util.List;

public class BookShelf implements Aggregate{

    List<Book> books = null;

    BookShelf(int initialsize){
        books = new ArrayList<>(initialsize);

    }

    public Book getBookAt(int index){


        return (Book)books.get(index);
    }


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

    public int getLength(){
        return books.size();
    }

    @Override
    public Iterator iterator() {
        return new BookShelfIterator(this);
    }
}

4. Define bookshelf iterator (equivalent to ConcreteIterator)

The code is as follows:

package com.wwh.iterator_model;

import java.util.ArrayList;
import java.util.List;

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

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

    @Override
    public boolean hasNext() {
        if (index < bookList.size()){
            return true;
        }
        return false;
    }

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

3, Case analysis


As shown in the figure above, BookShelf is a specific collection, which can complete book retrieval, book placement and total number query; BookShelfIterator is a specific iterator used to traverse BookShelf. The hasNext and next methods are the methods to complete the iteration. Judge whether there are still books through hasNext, and use next to get the next book.

Result + test

package com.wwh.iterator_model;

public class Main {
    public static void main(String[] args) {
        BookShelf bookShelf = new BookShelf(4);
        bookShelf.appendBook(new Book("Andersen's Fairy Tales"));
        bookShelf.appendBook(new Book("Aesop's Fables"));
        bookShelf.appendBook(new Book("Cinderella"));
        bookShelf.appendBook(new Book("Snow White"));
        Iterator iterator = bookShelf.iterator();
        while (iterator.hasNext()){
            System.out.println(((Book)iterator.next()).getName());
        }
        System.out.println("--------------------------------------");
        System.out.println(((Book)bookShelf.getBookAt(2)).getName());
    }
}

Tags: Java Design Pattern

Posted on Mon, 06 Sep 2021 23:32:36 -0400 by geus