Behavioral mode - iterator mode

Catalogue of series articles

Design pattern - design principle

Create mode - singleton mode (I)
Creation mode - factory mode (II)
Creation mode - prototype mode (III)
Creative mode - builder mode (4)

Structural mode - adapter mode (I)
Structural mode - bridge mode (II)
Structural mode - decorator mode (III)
Structural mode - combined mode (IV)
Structural mode - appearance mode (V)
Structural mode - Xiangyuan mode (6)
Structural mode - agent mode (7)

Behavioral mode - template method mode (I)
Behavioral mode - command mode (II)
Behavioral model visitor model (III)
Behavioral mode iterator mode (4)
Behavioral model observer model (V)
Behavioral model - intermediary model (6)
Behavioral model - memorandum model (7)
Behavioral mode interpreter mode (8)
Behavioral mode - state mode (IX)
Behavioral model - Strategic Model (10)
Behavioral model - responsibility chain model (11)


preface

Code address

1, Iterator mode

1.1 introduction to iterator mode

  • Iterator mode:

    • Provide an object (iterator) to sequentially access a series of data in an aggregate object (iterative data), rather than exposing the internal representation of the aggregate object;

1.2 iterator pattern structure

  • Abstract Aggregate role:

    • Define interfaces for storing, adding, deleting aggregate objects and creating iterator objects;
  • Concrete Aggregate role:

    • Implement the abstract aggregation class and return an instance of a concrete iterator;
  • Abstract Iterator role:

    • Define interfaces for accessing and traversing aggregation elements, usually including methods such as hasNext(), first(), next();
  • Concrete Iterator role:

    • Implement the method defined in the abstract iterator interface to complete the traversal of the aggregate object and record the current position of the traversal;

2, Realize

2.1 iterator mode implementation

package com.dozezz.designpattern.iterator;

/**
 * Abstract iterator
 */
public interface Iterator{
    /**
     * Is there another one
     * @return
     */
    boolean hasNext();

    /**
     * Return to next
     * @return
     */
    Object next();

    /**
     * Return to first
     * @return
     */
    Object first();

    /**
     * Return to current
     * @return
     */
    Object current();

}
package com.dozezz.designpattern.iterator;

import java.util.List;

/**
 * Concrete iterator
 */
public class ConcreteIterator implements Iterator{

    /**
     * Defines how data is stored
     */
    private List<Object> list ;

    /**
     * Traversal position
     */
    private int index = 0;

    public ConcreteIterator(List<Object> list) {
        this.list = list;
    }

    @Override
    public boolean hasNext() {
        return index < list.size() ;
    }

    @Override
    public Object next() {
        return hasNext() ? list.get(index++) : null;
    }

    @Override
    public Object first() {
        return list.get(0);
    }

    @Override
    public Object current() {
        return list.get(index);
    }
}
package com.dozezz.designpattern.iterator;

/**
 * Abstract aggregation
 */
public interface Aggregate {

    void  add(Object obj);

    void  remove(Object obj);

    Iterator getIterator();

}
package com.dozezz.designpattern.iterator;

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

/**
 * Concrete aggregate class
 */
public class ConcreteAggregate implements Aggregate{

    private List<Object> list = new ArrayList<>();

    @Override
    public void add(Object obj) {
        list.add(obj);
    }

    @Override
    public void remove(Object obj) {
        list.remove(obj);
    }

    @Override
    public Iterator getIterator() {
        return new ConcreteIterator(list);
    }
}

3, Iterator pattern summary

3.1 iterator mode application scenario

  • Iterator definition of JDK container interface;
  • In real development, we hardly need to write iterators, and we already have iterators for linked lists, trees and graphs of basic data structures;
  • Unless you want to rewrite the iterative logic;

3.2 advantages and disadvantages of iterator mode

  • Provide a unified method to traverse objects. Customers can traverse objects using one method without considering the type of aggregation;
  • The internal mechanism of aggregation is hidden. When the client wants to traverse the aggregation, it can only get the iterator without knowing the specific composition of the aggregation;
  • It provides a design idea that a mine should have only one cause of change (single responsibility). In the aggregation class, we separate the iterator, that is, we separate the responsibility of managing the object set and traversing the object set. In this way, if the set changes, only the aggregation object will be affected, and if the traversal mode changes, only the iterator will be affected.
  • Each aggregate object needs an iterator, which will generate multiple iterators and difficult to manage classes;

4, References

  • http://c.biancheng.net/view/1376.html
  • https://www.cnblogs.com/noteless/p/10112965.html
  • https://www.bilibili.com/video/BV1G4411c7N4?p=54&spm_id_from=pageDriver

Keywords: Design Pattern

Added by ghgarcia on Mon, 27 Dec 2021 22:09:56 +0200