[JAVA] Design pattern ~ Iterator ~

1.First of all

Here is a summary of the ** Iterator pattern ** in the GoF design pattern.

2. What is the Iterator 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 **.

3. Sample class diagram

Iterator.PNG

4. Sample program

It is a program that puts students in a class (classroom) and displays the names of the students in order.

4-1. Iterator interface

An interface that scans elements in sequence.

Iterator.java


public interface Iterator {
	public abstract boolean hasNext();
	public abstract Object next();
}

4-2. Aggregate interface

An interface that creates an Iterator. In the sample, it is called "aggregate".

Aggregate.java


public interface Aggregate {
	public abstract Iterator iterator();
}

4-3. Student class

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;
	}
}

4-4. ClassRoom Class

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);
	}
}

4-5. ClassRoomIterator class

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;
	}
}

4-6. Main class

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());
		}
	}
}

4-7. Execution result

Tanaka
Yamada
Suzuki
Sato

5. Benefits

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.

  1. GitHub

7. List of design patterns

-** GoF design pattern summary **

8. Reference

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

Design pattern ~ Iterator ~
Iterator pattern
Iterator Pattern
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Design pattern ~ Proxy ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
Design pattern ~ Command ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
Design pattern ~ Factory Method ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
Introduction to Design Patterns (Iterator)
Ruby design pattern template method pattern memo
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Memento Pattern
Mediator pattern
Composite pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Builder Pattern
Strategy pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
I tried to implement the Iterator pattern
Singleton pattern
Prototype Pattern
[Design pattern] Common logic with Template Method
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern