Behavioral patterns - iterator pattern (b)

The project source address: https: //github.com/ggb2312/JavaNotes/tree/master/design-pattern (design patterns relevant code and notes)

1. Definitions

There is provided a method of sequentially accessing each element of a collection object, without exposing the internal representation of the object

2. Applicable scene

  • Access the contents of a collection of objects without having to expose its internal representation
  • To traverse different sets structure provides a unified interface

3. The class diagram and role

Class Diagram

  • Iterator role (Iterator): a method to traverse the elements needed to define, in general, there are so three methods: a method for obtaining the next element of the next () method of judging whether traversal hasNext end ()), out of the current object The method remove ()

  • Specific roles iterator (Concrete Iterator): implemented method iterator interface defined, set ITERATIVE

  • Role container (Aggregate): Usually an interface is to provide a iterator () method, for example, the interface in java Collection, List interfaces, Set interface

  • The specific role of the container (ConcreteAggregate): is the concrete realization of the abstract class container, such as an ordered list List interfaces to achieve ArrayList, List list implementation of the interface to achieve HashSet LinkList, Set hash list of interfaces, etc.

4. The relevant design patterns

Iterator pattern and visitor pattern

  • The same: both are iterative access to a collection of objects
  • Different: Visitor pattern extended open portion, acting on the objects. Iterative mode extended open portion, is set in kind.

Example 5. Mode

There is a class courses:

public class Course {
    private String name;

    public Course(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

There is also a CourseAggregate interfaces, the curriculum process:

public interface CourseAggregate {

    void addCourse(Course course);
    void removeCourse(Course course);
    /**获取迭代器 */
    CourseIterator getCourseIterator();
}

This is to deal with the interface implementation class:

public class CourseAggregateImpl implements CourseAggregate {

    private List courseList;

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

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

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

    @Override
    public CourseIterator getCourseIterator() {
        return new CourseIteratorImpl(courseList);
    }
}

This is the iterator interface:

public interface CourseIterator {
    Course nextCourse();
    boolean isLastCourse();

}

And this is the iterator interface implementation class:

public class CourseIteratorImpl implements CourseIterator {

    private List courseList;
    private int position;
    private Course course;
    public CourseIteratorImpl(List courseList){
        this.courseList=courseList;
    }

    @Override
    public Course nextCourse() {
        System.out.println("返回课程,位置是: "+position);
        course=(Course)courseList.get(position);
        position++;
        return course;
    }

    @Override
    public boolean isLastCourse(){
        if(position< courseList.size()){
            return false;
        }
        return true;
    }
}

Test Methods:

public class Test {

    public static void main(String[] args) {
        Course course1 = new Course("Java电商一期");
        Course course2 = new Course("Java电商二期");
        Course course3 = new Course("Java设计模式精讲");
        Course course4 = new Course("Python课程");
        Course course5 = new Course("算法课程");
        Course course6 = new Course("前端课程");
        
        CourseAggregate courseAggregate = new CourseAggregateImpl();

        courseAggregate.addCourse(course1);
        courseAggregate.addCourse(course2);
        courseAggregate.addCourse(course3);
        courseAggregate.addCourse(course4);
        courseAggregate.addCourse(course5);
        courseAggregate.addCourse(course6);

        System.out.println("-----课程列表-----");
        printCourses(courseAggregate);

        courseAggregate.removeCourse(course4);
        courseAggregate.removeCourse(course5);

        System.out.println("-----删除操作之后的课程列表-----");
        printCourses(courseAggregate);
    }
    
    public static void printCourses(CourseAggregate courseAggregate){
        CourseIterator courseIterator= courseAggregate.getCourseIterator();
        while(!courseIterator.isLastCourse()){
            Course course=courseIterator.nextCourse();
            System.out.println(course.getName());
        }
    }
}

Test Results:

Test Results

At this class diagram:

Class Diagram

6. pros and cons

advantage:

  • Isolated traversal behavior collection of objects

Disadvantages:

  • Increasing the number of pairs of class

7. Extended -JDK1.7 source and a frame mode Iterative

7.1 JDK

java.util.Iterator
java.util.ArrayList中的Itr

7.2 Mybatis

org.apache.ibatis.cursor.defaults.DefaultCursor的cursorIterator

Guess you like

Origin www.cnblogs.com/gj-blog/p/10929524.html