(3) Behavior pattern: 4. Iterator Pattern (C++ example)

Table of contents

1. The meaning of Iterator Pattern

2. UML diagram learning of iterator pattern

3. Application scenarios of the iterator pattern

4. Advantages and Disadvantages of the Iterator Pattern

(1) Advantages

(2) Disadvantages

5. Examples of implementing the iterator pattern in C++


1. The meaning of Iterator Pattern

The Iterator pattern (Iterator) provides a method to sequentially access the individual elements of an aggregate object without exposing the internal representation of the object. 【DP】

By using the iterator pattern, the traversal algorithm can be decoupled from the collection object, so that the structure of the collection object and the traversal algorithm can change independently.

2. UML diagram learning of iterator pattern

 The main roles of the iterator pattern:

(1) Iterator : Defines an interface for accessing and traversing elements of a collection object, including methods such as obtaining the next element and determining whether there are still elements.

(2) Concrete Iterator : implements the iterator interface and performs traversal operations on specific collection objects.

(3) Aggregate : defines an interface for creating iterator objects, which can be an abstract class or interface.

(4) Concrete Aggregate : Implement the collection interface and create the corresponding specific iterator object.

3. Application scenarios of the iterator pattern

(1) Need to traverse an aggregate object without exposing its internal representation.

(2) It is necessary to provide multiple traversal methods for aggregate objects.

(3) A unified traversal interface needs to be provided so that client code can handle different types of collection objects in a unified way.

4. Advantages and Disadvantages of the Iterator Pattern

(1) Advantages

        1) Simplify the interface of the collection object: The iterator pattern encapsulates the responsibility of traversing the collection object into the iterator, making the interface of the collection object itself more concise.

        2) Support multiple traversal methods: By defining different iterators, different traversal methods can be supported, such as forward traversal, reverse traversal, etc.

        3) Provides a unified traversal interface: The iterator pattern provides a unified traversal interface so that client code can access different types of collection objects in a unified way.

(2) Disadvantages

        1) Increases the complexity of the system: The introduction of the iterator pattern will add additional classes and interfaces, increasing the complexity of the system.

        2) The collection object cannot be modified during the traversal process: When using an iterator to traverse the collection object, the collection object cannot be modified during the traversal process, otherwise the traversal result may be inaccurate.

5. Examples of implementing the iterator pattern in C++


#include <iostream>
#include <vector>

// 迭代器接口
class Iterator 
{
public:
    virtual int next() = 0;
    virtual bool hasNext() = 0;
};

// 具体迭代器
class ConcreteIterator : public Iterator 
{
private:
    std::vector<int> collection;
    int position;

public:
    ConcreteIterator(std::vector<int> coll) : collection(coll), position(0) {}

    int next() override 
    {
        return collection[position++];
    }

    bool hasNext() override 
    {
        return position < collection.size();
    }
};

// 集合接口
class Aggregate 
{
public:
    virtual Iterator* createIterator() = 0;
};

// 具体集合
class ConcreteAggregate : public Aggregate 
{
private:
    std::vector<int> collection;

public:
    ConcreteAggregate(std::vector<int> coll) : collection(coll) {}

    Iterator* createIterator() override 
    {
        return new ConcreteIterator(collection);
    }
};

int main() 
{
    std::vector<int> data = {1, 2, 3, 4, 5};
    Aggregate* aggregate = new ConcreteAggregate(data);
    Iterator* iterator = aggregate->createIterator();

    while (iterator->hasNext()) 
    {
        std::cout << iterator->next() << " ";
    }
    std::cout << std::endl;

    delete iterator;
    delete aggregate;

    return 0;
}

In the above example, we defined the iterator interface Iterator and the concrete iterator ConcreteIterator, as well as the collection interface Aggregate and the concrete collection ConcreteAggregate. By implementing these interfaces and classes, we can create a collection object containing integer elements and use an iterator to iterate over the elements in the collection.

Guess you like

Origin blog.csdn.net/bigger_belief/article/details/132334307