Design Patterns Iterator Pattern Notes

illustrate

Record the writing method of learning design pattern-iterator pattern. The JDK version used is version 1.8.

Iterator

Intent : Provide a way to sequentially access individual elements in an aggregate object without exposing the object's internal representation.
Structure :
Insert image description here
where:

  • Iterator (selector) defines the interface for accessing and traversing elements.
  • Concretelterator (concrete iterator) implements the selector interface: tracks the current position when traversing the aggregate.
  • Aggregate defines the interface for creating corresponding selector objects.
  • ConcreteAggregate (concrete aggregation) implements the interface that creates the corresponding selector. This operation returns an appropriate instance of Concretelterator.

applicability:

  • Access the contents of an aggregate object without exposing its internal representation.
  • Supports multiple traversals of aggregate objects.
  • Provides a unified interface for traversing different aggregate structures.

Table of contents

Insert image description here

Iterator pattern example class diagram

Insert image description here
Implement the Iterator pattern example with this UML class diagram.

Student category

package com.example.deesign_patterns.iterator;

//学生类
public class Student {
    
    

    private String name;
    private String number;

    public Student(String name, String number) {
    
    
        this.name = name;
        this.number = number;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public String getNumber() {
    
    
        return number;
    }

    public void setNumber(String number) {
    
    
        this.number = number;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", number='" + number + '\'' +
                '}';
    }
}

Abstract iterator role interface

package com.example.deesign_patterns.iterator;

//抽象迭代器角色接口
public interface StudentIterator {
    
    

    //判断是否还有元素
    boolean hasNext();

    //获取下一个元素
    Student next();
}

Concrete iterator role class

package com.example.deesign_patterns.iterator;

import java.util.List;

//具体迭代器角色类
public class StudentIteratorImpl implements StudentIterator{
    
    

    private List<Student> list;
    private int position=0;//用来记录遍历时的位置

    public StudentIteratorImpl(List<Student> list) {
    
    
        this.list = list;
    }

    @Override
    public boolean hasNext() {
    
    
        return position< list.size();
    }

    @Override
    public Student next() {
    
    
        //从集合中获取指定位置的元素
        Student currentStudent = list.get(position);
        position++;
        return currentStudent;
    }
}

Abstract aggregate role interface

package com.example.deesign_patterns.iterator;

//抽象聚合角色接口
public interface StudentAggregate {
    
    

    //添加学生功能
    void addStudent(Student stu);

    //删除学生功能
    void removeStudent(Student stu);

    //获取迭代器对象功能
    StudentIterator getStudentIterator();
}

Concrete aggregate role class

package com.example.deesign_patterns.iterator;

import java.util.ArrayList;
import java.util.List;

//具体聚合角色类
public class StudentAggregateImpl implements StudentAggregate{
    
    

    private List<Student> list=new ArrayList<Student>();

    @Override
    public void addStudent(Student stu) {
    
    
        list.add(stu);
    }

    @Override
    public void removeStudent(Student stu) {
    
    
        list.remove(stu);
    }

    //获取迭代器对象
    @Override
    public StudentIterator getStudentIterator() {
    
    
        return new StudentIteratorImpl(list);
    }
}

Test class

package com.example.deesign_patterns.iterator;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        //创建聚合对象
        StudentAggregateImpl aggregate=new StudentAggregateImpl();
        //添加元素
        aggregate.addStudent(new Student("张三","001"));
        aggregate.addStudent(new Student("李四","002"));
        aggregate.addStudent(new Student("王五","003"));
        aggregate.addStudent(new Student("赵六","004"));
        //遍历聚合对象
        //获取迭代器对象
        StudentIterator iterator=aggregate.getStudentIterator();
        //遍历
        while (iterator.hasNext()){
    
    
            //获取元素
            Student student=iterator.next();
            System.out.println(student.toString());
        }
    }
}

Insert image description here

benefit:

  • It supports traversing an aggregate object in different ways, and multiple traversal methods can be defined on the same aggregate object. In the iterator mode, you only need to replace the original iterator with a different iterator to change the traversal algorithm. We can also define a subclass of the iterator ourselves to support new traversal methods.
  • Selectors simplify aggregate classes. Due to the introduction of iterators, there is no need to provide data traversal and other methods in the original aggregate object, which can simplify the design of aggregate classes.
  • In the iterator pattern, due to the introduction of the abstraction layer, it is very convenient to add new aggregate classes and iterator classes without modifying the original code, which meets the requirements of the "opening and closing principle".

shortcoming:

  • The number of classes is increased, which increases the complexity of the system to a certain extent.

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/131365275
Recommended