Article Directory
-
- look at a specific need
- Traditional design scheme (class diagram)
- Traditional way of problem analysis
- Basic introduction to iterator mode
- Principle class diagram of the iterator pattern
- Explanation of the principle class diagram - that is (roles and responsibilities of the iterator mode)
- Iterator pattern application example
- The old rules, debug to see
- Source Code Analysis of Iterator Mode in JDK
- Notes and Details of 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:
Traditional design scheme (class diagram)
Traditional way of problem analysis
-
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
-
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.
-
Solution: => Iterator pattern
Basic introduction to iterator mode
- Iterator Pattern (Iterator Pattern) is a commonly used design pattern, which belongs to behavioral pattern
- 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.
- 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
Explanation of the principle class diagram - that is (roles and responsibilities of the iterator mode)
- Iterator: iterator interface, provided by the system, meaning hasNext, next, remove
- ConcreteIterator : Concrete iterator class that manages iteration
- Aggregate: a unified aggregation interface that decouples the client from the specific aggregation
- ConcreteAggreage : Concrete aggregates hold collections of objects and provide a method that returns an iterator that correctly traverses the collection
- 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.
College class, college super class
ComputerCollege Classes, School of Computing
The major (Department) inside is stored in an array
Department class
InfoCollege, School of Information Engineering
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
InfoColleageIterator class, the specific iteration class of the School of Information Engineering, needs to implement the Iterator interface
OutPutImpl class, print information uniformly
Client class, client class
operation result
The old rules, debug to see
Here, a list containing the School of Computer Science and the School of Information Engineering is constructed and passed to the constructor of OutPutImpl
You can see that the object after outPutImpl is instantiated contains a list member variable
Enter the printCollege method
What is called here is that the Iterator in the list takes out each college and prints the name
college.createIterator() method
Here a concrete iterator is returned
Go back to the printDepartment method
The iterator here is a computer school iterator
So he'll walk our own implementation
then get a professional
output professional name
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
ArrayList's iterator method
Returns an Itr class
He is an internal class that implements iterator in ArrayList
His existing member variable elementData stores data
Then we look at linkedlist
His parent class AbstractSequentialList has an iterator method
What is actually called is the listIterator method in the parent class AbstractList of AbstractSequentialList
What is actually returned is the AbstractList internal class ListItr
Role Analysis and Explanation of Class Diagram
- The internal class Itr acts as a class that implements the iterator Iterator, as an ArrayList internal class, similar to ConcreteIterator
- List acts as an aggregate interface and contains an iterator() method that returns an iterator object, similar to Aggregate
- ArrayList is a subclass that implements the aggregation interface List and implements iterator(), similar to ConcreteAggreage
- 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
- Provide a unified method to traverse objects, clients no longer need to consider the type of aggregation, and use one method to traverse objects.
- 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.
- 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.
- 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
- 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