In software design, we often need to access the elements of aggregate objects. Because the traversal method may change. Therefore, the access method cannot be written directly in the aggregate object, because it will violate the "opening and closing principle".
Keep it simple. The beginning is, think about it Java Collection, List, Set, Map in. They all use iterator mode to access elements
Iterator pattern definition: provides an object to sequentially access a series of data in an aggregate object without exposing the internal representation of the aggregate object.
advantage:
- Access the contents of an aggregate object without exposing its internal representation.
- The traversal task is left to the iterator, which simplifies the aggregation class.
- It supports traversing an aggregate in different ways, and you can even customize the subclass of the iterator to support new traversal. Adding new aggregate classes and iterator classes is very convenient without modifying the original code.
- It has good encapsulation and provides a unified interface for traversing different aggregation structures.
Disadvantages:
- The number of classes is increased, which increases the complexity of the system to a certain extent.
In daily development, we hardly write iterators ourselves. Unless you need to customize an iterator corresponding to your own data structure.
Structure and implementation of iterator pattern
structure
- Abstract Aggregate role: defines interfaces for storing, adding, deleting Aggregate objects, and creating iterator objects.
- Concrete aggregate role: implement the abstract aggregate class and return an instance of a concrete iterator.
- Abstract Iterator role: defines the interface for accessing and traversing aggregation elements, usually including hasNext(), first(), next(), etc.
- Concrete iterator role: implement the methods defined in the abstract iterator interface, complete the traversal of aggregate objects, and record the current location of traversal.
realization
Implementation scenario: define a class class and use iterators to traverse the students in the class.
package com.wly.DesignPatterns; import java.util.ArrayList; import java.util.List; /** * @program: StudyDome * @author: yuanzhang * @create: 2020-12-29 15:17 **/ public class IteratorPattern { public static void main(String[] args) { ClassGrade classGrade = new ClassGradeImpl(); //Add ten children to the class for (int i = 1; i < 11; i++) { Student student = new Student("king"+i,i); classGrade.addStudent(student); } studentIterator studentIterator = classGrade.getStudentIterator(); System.out.println("Start counting"); while (studentIterator.hasNext()){ Student student = studentIterator.next(); System.out.println(String.format("I am %s ,No %d",student.getName(),student.getNumber())); } } } //Student entity class class Student{ private String name; private Integer number; public Student(String name, Integer number) { this.name = name; this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getNumber() { return number; } public void setNumber(Integer number) { this.number = number; } } /** * @Annotation:Abstract aggregation class: Class * @Author: yuanzhang * @Date: 15:24 */ interface ClassGrade{ void addStudent(Student student); void removeStudent(Student student); studentIterator getStudentIterator(); } /** * @Annotation:Specific aggregation class: class body * @Author: yuanzhang * @Date: 15:27 */ class ClassGradeImpl implements ClassGrade{ private List<Student> studentList; public ClassGradeImpl() { this.studentList = new ArrayList <Student>(); } @Override public void addStudent(Student student) { studentList.add(student); } @Override public void removeStudent(Student student) { studentList.remove(student); } @Override public studentIterator getStudentIterator() { return new StudentIteratorImpl(studentList); } } /** * @Annotation:Abstract iterators: Student iterators * @Author: yuanzhang * @Date: 15:30 */ interface studentIterator{ boolean hasNext(); Student next(); } /** * @Annotation:Concrete iterator: Student iterator * @Author: yuanzhang * @Date: 15:31 */ class StudentIteratorImpl implements studentIterator{ private List<Student> studentList; /**cursor*/ private int index=0; public StudentIteratorImpl(List <Student> studentList) { this.studentList = studentList; } @Override public boolean hasNext() { return studentList.size()>index; } @Override public Student next() { return studentList.get(index++); } }
output
Start counting I'm wang 1, number 1 I'm wang 2, number 2 I'm Wang 3, number 3 I'm Wang 4, number 4 I'm wang 5, number 5 I'm wang 6, number 6 I'm wang 7, number 7 I'm wang 8, number 8 I'm wang 9, number 9 I'm wang 10, number 10