[Design pattern] I finally understand the iterator pattern. . .

look at a specific need

Write a program to display the structure of a school's departments: the requirements are as follows
To display the composition of the school's departments on one page,
a school has multiple colleges, and
a college has multiple departments.

As shown in the picture:

insert image description here

Traditional design scheme (class diagram)

insert image description here

Traditional way of problem analysis

  1. Think of the college as a subcategory of the school, and the department as a subcategory of the college, so that the hierarchy is actually based on the size of the organization

  2. In fact, our requirement is to display the composition of the school's departments on one page. A school has multiple colleges, and a college has multiple departments. Therefore, this scheme cannot be well implemented for traversal operations.

  3. Solution: => Iterator pattern

Basic introduction to iterator mode

  1. Iterator Pattern (Iterator Pattern) is a commonly used design pattern, which belongs to behavioral pattern
  2. If our collection elements are implemented in different ways, such as arrays, java collection classes, or other methods, when the client wants to traverse these collection elements, it must use multiple traversal methods, and it will also expose the internal structure of the elements. You can consider using the iterator mode to solve it.
  3. The iterator mode provides a unified interface for traversing collection elements, traversing collection elements in a consistent way, without knowing the underlying representation of collection objects, that is, without exposing its internal structure.

Principle class diagram of the iterator pattern

insert image description here

Explanation of the principle class diagram - that is (roles and responsibilities of the iterator mode)

  1. Iterator: iterator interface, provided by the system, meaning hasNext, next, remove
  2. ConcreteIterator : Concrete iterator class that manages iteration
  3. Aggregate: a unified aggregation interface that decouples the client from the specific aggregation
  4. ConcreteAggreage : Concrete aggregates hold collections of objects and provide a method that returns an iterator that correctly traverses the collection
  5. Client : client, relying on subclasses through Iterator and Aggregate

Iterator pattern application example

Write a program to display the structure of a school's departments: the requirement is that
the composition of the school's departments should be displayed on one page,
a school has multiple colleges, and
a college has multiple departments.

insert image description here

College class, college super class

insert image description here

ComputerCollege Classes, School of Computing

insert image description here

The major (Department) inside is stored in an array

Department class

insert image description here

InfoCollege, School of Information Engineering

insert image description here
The majors (Department) are stored in a list

ComputerCollegeIterator class, the specific iterative class of the School of Computer Science, needs to implement the Iterator interface

insert image description here

InfoColleageIterator class, the specific iteration class of the School of Information Engineering, needs to implement the Iterator interface

insert image description here

OutPutImpl class, print information uniformly

insert image description here

Client class, client class

insert image description here

operation result

insert image description here

The old rules, debug to see

insert image description here

Here, a list containing the School of Computer Science and the School of Information Engineering is constructed and passed to the constructor of OutPutImpl

insert image description here
You can see that the object after outPutImpl is instantiated contains a list member variable

Enter the printCollege method

insert image description here

What is called here is that the Iterator in the list takes out each college and prints the name

insert image description here

college.createIterator() method

insert image description here
insert image description here

Here a concrete iterator is returned

insert image description here
insert image description here

Go back to the printDepartment method

insert image description here

The iterator here is a computer school iterator

insert image description here

So he'll walk our own implementation

insert image description here

then get a professional

insert image description here

output professional name

insert image description here

The same is true for the School of Information Engineering

In this way, the OutPutImpl class does not actually know what the specific iterator is, so it hides our internal details

Source Code Analysis of Iterator Mode in JDK

The iterator pattern is used in JDK's ArrayList collection

code analysis

ArrayList class

insert image description here

ArrayList's iterator method

insert image description here

Returns an Itr class

He is an internal class that implements iterator in ArrayList

insert image description here

His existing member variable elementData stores data

insert image description here

Then we look at linkedlist

insert image description here

His parent class AbstractSequentialList has an iterator method

insert image description here

What is actually called is the listIterator method in the parent class AbstractList of AbstractSequentialList

insert image description here

What is actually returned is the AbstractList internal class ListItr

insert image description here

Role Analysis and Explanation of Class Diagram

insert image description here

  1. The internal class Itr acts as a class that implements the iterator Iterator, as an ArrayList internal class, similar to ConcreteIterator
  2. List acts as an aggregate interface and contains an iterator() method that returns an iterator object, similar to Aggregate
  3. ArrayList is a subclass that implements the aggregation interface List and implements iterator(), similar to ConcreteAggreage
  4. The Iterator interface system provides

The iterator mode solves the unified traversal problem of different collections (ArrayList, LinkedList)

Notes and Details of the Iterator Pattern

advantage

  1. Provide a unified method to traverse objects, clients no longer need to consider the type of aggregation, and use one method to traverse objects.
  2. The internal structure of the aggregation is hidden. When the client wants to traverse the aggregation, it can only get the iterator, but will not know the specific composition of the aggregation.
  3. Provides a design idea that a class should have only one cause of change (called the single responsibility principle). In the aggregation class, we separate the iterator, that is, to separate the responsibility of managing the object collection and traversing the object collection, so that if the collection changes, only the aggregation object is affected. And if the traversal method is changed, only the iterator is affected.
  4. When you want to display a group of similar objects, or traverse a group of identical objects, it is suitable to use the iterator mode

shortcoming

  1. Each aggregated object requires an iterator, which will generate multiple iterators that are not easy to manage

Data reference: https://www.bilibili.com/video/BV1G4411c7N4

Code address: https://gitee.com/WangFuGui-Ma/design-pattern/tree/master/design

Guess you like

Origin blog.csdn.net/csdnerM/article/details/130459045