After studying for so many years, I found a problem. It seems that teachers like to name a name very much, and even name a name has become the hobby of some teachers. If you don’t name a day, you won’t eat well and sleep well. I think It’s very strange. If your class is well taught, how could the classmates not come to listen to the class? Little did they know: “It is a crime to mislead people’s children!”
Okay, let’s take a look at how the teacher’s naming process is implemented:
1. As usual , let’s first define the Teacher interface class:
The code copy is as follows:
public interface Teacher {
public Iterator createIterator(); //Click it
}
2. The specific teacher (ConcreteTeacher) class is an implementation of the teacher (Teacher) interface:
The code copy is as follows:
public class ConcreteTeacher implements Teacher{
private Object[] present = {"Zhang San is here","Li Si is here","Wang Wu is not here"}; //Class attendance gathering
public Iterator createIterator(){
return new ConcreteIterator(this); //New name
}
public Object getElement(int index){ //Get the current attendance of the classmate
if(index<present.length){
return present[index];
}
else{
return null;
}
}
public int getSize(){
return present.length; //Get the size of the class attendance set, that is, you need to know how many people there are in the class.
}
}
3. Define the Iterator interface class:
The code copy is as follows:
public interface Iterator {
void first(); //The first
void next(); //Next
boolean isDone(); //Is the name completed
Object currentItem(); //Current attendance of classmates
}
4. The specific ConcreteIterator class is an implementation of the Iterator interface:
The code copy is as follows:
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(); //Get the number of classmates
index = 0;
}
public void first(){ //First
index = 0;
}
public void next(){ //Next
if(index<size){
index++;
}
}
public boolean isDone(){ //Is the name completed
return (index>=size);
}
public Object currentItem(){ //Current attendance of classmates
return teacher.getElement(index);
}
}
5. Write test classes :
The code copy is as follows:
public class Test {
private Iterator it;
private Teacher teacher = new ConcreteTeacher();
public void operation(){
it = teacher.createIterator(); //The teacher starts to call
while(!it.isDone()){ //If you haven't finished clicking
System.out.println(it.currentItem().toString()); //Get the situation where it is clicked to the classmate
it.next(); //Click next
}
}
public static void main(String agrs[]){
Test test = new Test();
test.operation();
}
}
6. Description :
A: Definition: The Iterator pattern can access an aggregated element in sequence without exposing the internal situation of the aggregate.
B: In this example, the teacher gives an interface to create an Iterator object, and the Iterator defines the interface required to traverse the attendance of students.
C: The advantage of the Iterator mode is that when there is a change in the (ConcreteTeacher) object, such as new students are added to the classmate attendance set, or fewer classmates are reduced, this change has no effect on the client.