This is the 14th day of my participation in Gwen Challenge

Iterator pattern

The iterator pattern, also known as the cursor pattern, is one of the behavioral design patterns and is a way of accessing containers. Accessing container objects necessarily involves traversal, which is in the form of iterators. It inserts iterators between the access and the container, isolating the container from the iteration function. The container class only needs to maintain its own internal data and does not care about the traversal operation method.

In actual combat

Iterator stands for Iterator interface. There are two methods: Next to get the next internal member of the container. HasNext determines whether there are any more internal members. ConcreteIterator is specific iterator, have a cursor subscript and list as traversed according to the current cursor for the members of a container object; Aggregate is the container interface. Add can add internal members, remove can remove internal members, and obtain the iterator function. ConcreteAggregate is a concrete container class that implements an interface method, primarily to get iterators. When iterators are obtained when traversal operations are needed, concrete iterator classes have been implemented internally. When next is called, a specific member of the container can be obtained by subscript.


// Iterator interface
public interface Iterator<T>{
    T next(a);
    boolean hasNext(a);
}
/// concrete iterator
public class ConcreteIterator<T> implements Iterator<T>{
    private List<T> list = new ArrayList<T>();
    private int cursor = 0;
    
    public ConcreteIterator(List<T> list){
        this.list = list;
    }
    @override
    boolean hasNext(a){
        returncursor ! = list.size(); }@override
    public T next(a){
        T obj = null;
        if(this.hasNext()){
            obj = this.list.get(cursor ++);
        }
        returnobj; }}// Container interface
public interface Aggregate<T>{
    void add(T obj);
    void remove(T obj);
    Iterator<T> iterator(a);
};

/// Specify the container class
public class ConcreteAggregate<T> implements Aggregate<T>{
    private List<T> list = new ArrayList<T>();
    
    @override
    public void add(T obj){
        list.add(obj);
    }
    
    @override
    public void remove(T obj){
        list.remove(obj);
    }
    
    @override
    public Iterator<T> iterator(a){
        return newConcreteIterator<T>(list); }}/ / / implementation
Aggregate<String> a = new ConcreteAggregate<>();
a.add("1");
a.add("2");
a.add("3");
a.add("4");

Iterator<String> i = a.iterator();
while(i.hasNext()){
    ....
}
Copy the code

conclusion

Advantages of iterator pattern: Supports different ways to traverse a container object, weakening the relationship between container and traversal algorithm.

Disadvantages of the iterator pattern: The design pattern is common, increasing the number of class files.

In fact, their container classes have the corresponding iterator function in the development language, and the high-level language basically has built-in implementation, for developers more understanding and understanding how to use, there is no need to develop their own implementation of the iterator. Developers are more concerned about which iterators are more appropriate and efficient to use in a particular business scenario.

reference

  • Iterator pattern