An introduction to the Iterator pattern of Java design patterns

  • 2020-04-01 03:42:05
  • OfStack

For so many years, I found a problem, as if the teacher is very like name, even the name is some teacher's hobby, don't call the roll, a day on food is not sweet, sleep is not good, I will feel very strange, of course if you speak well, students how to listen, little imagine: "perpetuated this misunderstanding, but crime!" Ah.

Ok, now let's see how the teacher's roll call process works:
1. Old rules , we first define the Teacher interface class:


public interface Teacher {
    public Iterator createIterator(); //Named < br / > }

2. Specific teachers The (ConcreteTeacher) class is an implementation of the Teacher interface:

public class ConcreteTeacher implements Teacher{
    private Object[] present = {" Zhang SAN is coming "," The Li Silai "," Cathy didn't come "}; //Class attendance
    public Iterator createIterator(){
        return new ConcreteIterator(this); //New roll call
    }
    public Object getElement(int index){ //Get the current student attendance
        if(index<present.length){
            return present[index];
        }
        else{
            return null;
        }
    }
    public int getSize(){
        return present.length; //Get the size of the class attendance set, which means how many people are in the class
    }
}

3. Define roll call (Iterator) interface class:

public interface Iterator {
    void first(); //The first one is
    void next(); //Next
    boolean isDone(); //

    Object currentItem(); //Current student attendance
}

4. Specific roll call The (ConcreteIterator) class is an implementation of the named interface:

public class ConcreteIterator implements Iterator{
    private ConcreteTeacher teacher;
    private int index = 0;
    private int size = 0;
    public ConcreteIterator(ConcreteTeacher teacher){
        this.teacher = teacher;
        size = teacher.getSize(); //We get the number of students
        index = 0;
    }
    public void first(){ //The first one is
        index = 0;
    }
    public void next(){ //Next
        if(index<size){
            index++;
        }
    }
    public boolean isDone(){ //

        return (index>=size);
    }
    public Object currentItem(){ //Current student attendance
        return teacher.getElement(index);
    }
}

5. Write test classes :


public class Test {
    private Iterator it;
    private Teacher teacher = new ConcreteTeacher();
    public void operation(){
        it = teacher.createIterator(); //The teacher began to call the roll         while(!it.isDone()){ //If you haven't clicked
            System.out.println(it.currentItem().toString()); //
            it.next(); //Click the next
        }
    }
    public static void main(String agrs[]){
        Test test = new Test();
        test.operation();
    }
}

6, description, :

A: definition: the Iterator pattern allows sequential access to the elements of an aggregation without exposing the inner workings of the aggregation.
B: in this example, the Teacher gives the interface to create an Iterator object, which defines the interface needed to traverse the class attendance.
C: the advantage of the Iterator pattern is that when there is a change in the (ConcreteTeacher) object, such as the addition or reduction of students in the class attendance collection, the change has no effect on the client.


Related articles: