Design Pattern Notes 17 - Iterator Pattern (iterator)

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.

 

basic introduction

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 element, which can be solved by using the iterator mode.

3) The iterator mode provides a unified interface for traversing collection elements, and uses a consistent method to traverse collection elements without knowing the underlying representation of collection objects, that is, without exposing its internal structure.

 

 

Principle class diagram of iterator pattern

 

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 : a specific iterator class that manages iterations

3) Aggregate: a unified aggregation interface that decouples the client from the specific aggregation

 

Core code:

 

    //Judge whether there is a next element in the list
    @Override
    public boolean hasNext() {         // TODO Auto-generated method stub         if(index >= departmentList.size() - 1) {             return false;         } else {             index += 1;             return true;         }     }







    @Override
    public Object next() {
        // TODO Auto-generated method stub
        return departmentList.get(index);
    }

In fact, the access process is unified, and the same interface is used to access each element in the collection.

 

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 can traverse objects with one method.

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 the same 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

 

 

Guess you like

Origin blog.csdn.net/qq_22059611/article/details/103269315