Iterator pattern -Iterator

First, the definition

Iterative mode, there is provided a method of sequential access to the various elements of a polymeric object, but not exposed to the interior of the object representation.

Second, the structure

(1) the role of iterator (Iterator): a method to traverse the elements needed to define, in general, there are so three methods:

  • next (): Get the next element method,
  • hasNext (): method of determining whether the iteration is complete
  • remove (): out of the current object's method

(2) the specific role iterator (Concrete Iterator): Method iterator interface implementation defined, ITERATIVE collection.

(3) the role of the container (Aggregate): is a general interface that provides a iterator () method, for example, the interface in java Collection, List interfaces, Set interface

(4) the role of specific container (ConcreteAggregate): abstract class is a concrete realization of a container, such as an ordered list List interfaces to achieve ArrayList, List list implementation of the interface LinkList, hash list Set HashSet implements the interface and so on.

Third, examples

Abstract collection

/**
 * Abstract aggregation. (It can be understood as the Collection interface in Java)
 */
public interface Aggregate {

    // returns an iterator 
    Iterator iterator ();

}

Abstract iterator

/**
 * Abstract iterator
 */
public interface Iterator {

    // move to the first element 
    void First ();

    // Are there elements of 
    boolean hasNext ();

    // Returns next element 
    Object next ();

}

Specific collection

/**
 * Specific aggregation (can be understood as the realization of Java Collection classes)
 */
public class ConcreteAggregate implements Aggregate {

    private Object[] list = {"zhangsan", "lisi", "wangwu", "zhaoliu"};

    public Object getElement(int index) {
        if (index < list.length) {
            return list[index];
        }
        // cross-border 
        return  null ;
    }

    public int size() {
        return list.length;
    }

    /**
     * Returned by the specific aggregate current iterator
     */
    @Override
    public Iterator iterator() {
        return new ConcreteIterator(this);
    }
}

Specific iterator

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

    private ConcreteAggregate concreteAggregate;
    private int index;
    private int size;

    public ConcreteIterator(ConcreteAggregate concreteAggregate) {
        this.concreteAggregate = concreteAggregate;
        size = concreteAggregate.size();
        index = 0;
    }

    @Override
    public void first() {
        index = 0;
    }

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

    @Override
    public Object next() {
        return concreteAggregate.getElement(index++);
    }
}

Client code

public  class Customer {

    public static void main(String[] args) {
        Aggregate aggregate = new ConcreteAggregate();
        Iterator iterator = aggregate.iterator();
        while (iterator.hasNext()) {
            Object element = iterator.next();
            System.out.println(element);
        }
    }

}

Print Results:
    zhangsan
    lysis
    wangwu
    zhaoliu

 

Guess you like

Origin www.cnblogs.com/rouqinglangzi/p/11080083.html