Here is a summary of the ** Iterator pattern ** in the GoF design pattern.
--The English word Iterate means ** repeat something **. --The Iterator pattern is a ** method for sequentially accessing the elements of an aggregate **. --The GoF design patterns are classified as ** behavioral design patterns **.
It is a program that puts students in a class (classroom) and displays the names of the students in order.
An interface that scans elements in sequence.
Iterator.java
public interface Iterator {
public abstract boolean hasNext();
public abstract Object next();
}
An interface that creates an Iterator. In the sample, it is called "aggregate".
Aggregate.java
public interface Aggregate {
public abstract Iterator iterator();
}
A class that is an element of an aggregate. In the sample, it is "student".
Student.java
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
A class that implements the interface defined by Aggregate. In the sample, it is called "classroom".
ClassRoom.java
public class ClassRoom implements Aggregate {
private Student[] students;
private int last = 0;
public ClassRoom(int maxsize) {
this.students = new Student[maxsize];
}
public Student getStudentAt(int index) {
return students[index];
}
public void appendStudent(Student student) {
this.students[last] = student;
last++;
}
public int getLength() {
return last;
}
public Iterator iterator() {
return new ClassRoomIterator(this);
}
}
A class that implements the interface defined by Iterator.
ClassRoomIterator.java
public class ClassRoomIterator implements Iterator {
private ClassRoom classRoom;
private int index;
public ClassRoomIterator(ClassRoom classRoom) {
this.classRoom = classRoom;
this.index = 0;
}
public boolean hasNext() {
if (index < classRoom.getLength()) {
return true;
} else {
return false;
}
}
public Object next() {
Student student = classRoom.getStudentAt(index);
index++;
return student;
}
}
This class performs the main processing.
Main.java
public class Main {
public static void main(String[] args) {
ClassRoom classRoom = new ClassRoom(4);
classRoom.appendStudent(new Student("Tanaka"));
classRoom.appendStudent(new Student("Yamada"));
classRoom.appendStudent(new Student("Suzuki"));
classRoom.appendStudent(new Student("Sato"));
Iterator iterator= classRoom.iterator();
while (iterator.hasNext()) {
Student student = (Student)iterator.next();
System.out.println(student.getName());
}
}
}
Tanaka
Yamada
Suzuki
Sato
The advantage of the Iterator pattern is that it can be counted separately from the implementation. Design patterns allow classes to be used as parts and promote reusability. The only Iterator methods used in the ** Main class ** of the sample program are ** hasNext () and next () **. In other words, the implementation does not depend on the ** ClassRoom class **, so you do not have to worry about the size of the array.
-** GoF design pattern summary **
This article and sample program were created based on the following books.
-** Introduction to design patterns learned in Java language **
It was very easy to understand and I learned a lot. Thank you. The detailed explanations of the design patterns and sample programs are written, so please take a look at the books as well.
Recommended Posts