[Design Patterns] - 16, iterator pattern

(16) Iterator mode

The Iterator Pattern works by providing a way to access collections/containers in order without exposing the collection's internal representation. It can provide uniform traversal behavior for different containers, regardless of the construction of the elements inside the container.

1. Design principles of iterator pattern

1. Abstract Iterator: The abstract iterator defines the interface for accessing and traversing elements;

2. ConcreateIterator: Provides specific traversal behavior;

3. Abstract container (IAggregate): define an interface that provides specific iterators;

4, Concrete container (ConcreateAggregate): Create a concrete iterator.

2. Simple example

Write an iterator to iterate over course information.

public interface Iterator<E> {
    
    
    E next();
    boolean hasNext();
}
public class IteratorImpl<E> implements Iterator<E> {
    
    
//    实现迭代器内部维护一个list,element,index
    private List<E> list;
    private int cursor;
    private E element;

    public IteratorImpl(List<E> list) {
    
    
        this.list = list;
    }

//    获取下一个元素
    @Override
    public E next() {
    
    
        System.out.print("当前位置: "+cursor);
        element = list.get(cursor);
        cursor++;
        return element;
    }

//    判断下一个位置是否有元素
    @Override
    public boolean hasNext() {
    
    
        return cursor > list.size()-1 ? false : true;
    }
}
public interface CourseAggregate {
    
    
    void add(Course course);
    void remove(Course course);
    Iterator<Course> iterator();
}
public class CourseAggregateImpl implements CourseAggregate {
    
    

    private List courseList;

    public CourseAggregateImpl() {
    
    
        this.courseList = new ArrayList();
    }

    @Override
    public void add(Course course) {
    
    
        courseList.add(course);
    }

    @Override
    public void remove(Course course) {
    
    
        courseList.remove(course);
    }

    @Override
    public Iterator<Course> iterator() {
    
    
        return new IteratorImpl<>(courseList);
    }
}
public class Course {
    
    
    private String name;

    public Course(String name) {
    
    
        this.name = name;
    }
	//get,set
}
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Course java = new Course("java");
        Course python = new Course("python");
        Course ai = new Course("ai");

        CourseAggregateImpl courseAggregate = new CourseAggregateImpl();

        courseAggregate.add(java);
        courseAggregate.add(python);
        courseAggregate.add(ai);

        print(courseAggregate);

    }

    public static void print(CourseAggregate courseAggregate) {
    
    
        Iterator<Course> iterator = courseAggregate.iterator();
        while (iterator.hasNext()) {
    
    
            Course course = iterator.next();
            System.out.println(course.getName());
        }
    }
}

image-20210316204627703

3. Comments on iterator mode

The iterator pattern abstracts the element iteration interface provided by the collection object itself to the iterator, so that the collection object does not need to care about the specific iteration behavior. It is essentially an encapsulation. Generally, the iterator mode can be used to iterate the self-built data structure, and the iterator that comes with the framework can be used for others.

Guess you like

Origin blog.csdn.net/qq_40589204/article/details/118343162
Recommended