设计模式之迭代器模式

迭代器模式是一种行为型设计模式,它提供了一种方法来顺序访问一个集合对象中的元素,而不需要暴露该对象的内部表示。迭代器模式非常适合用于容器类的设计,使得客户端代码可以通过统一的接口访问不同类型的集合。

1. 主要组成部分

  • 迭代器(Iterator) :定义访问集合元素的接口,包括方法如 next(), hasNext(), remove() 等。
  • 聚合(Aggregate):定义创建迭代器的接口。
  • 具体迭代器(Concrete Iterator):实现迭代器接口,维护对当前元素的引用和对集合的引用。
  • 具体聚合(Concrete Aggregate):实现聚合接口,返回具体迭代器的实例。

2. UML 类图

lua 复制代码
+------------------+
|    Iterator      |
+------------------+
| +next()          |
| +hasNext()      |
+------------------+
        ^
        |
+------------------+
| ConcreteIterator  |
+------------------+
| -collection: Collection |
| -index: int            |
+------------------+
| +next()          |
| +hasNext()      |
+------------------+

+------------------+
|    Aggregate     |
+------------------+
| +createIterator()|
+------------------+
        ^
        |
+------------------+
| ConcreteAggregate|
+------------------+
| -items: List     |
+------------------+
| +createIterator()|
+------------------+

3. 示例代码

以下是一个简单的迭代器模式实现示例,使用 C++。

cpp 复制代码
#include <iostream>
#include <vector>

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

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

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

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

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

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

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

public:
    void addItem(int item) {
        items.push_back(item);
    }

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

// 客户端代码
int main() {
    ConcreteAggregate aggregate;
    aggregate.addItem(1);
    aggregate.addItem(2);
    aggregate.addItem(3);

    Iterator* iterator = aggregate.createIterator();
    while (iterator->hasNext()) {
        std::cout << iterator->next() << " ";
    }
    std::cout << std::endl;

    delete iterator;
    return 0;
}

4. 优点

  • 解耦:迭代器模式将集合的遍历与集合的实现分离,使得代码更清晰,易于维护。
  • 统一接口:不同的集合可以通过相同的接口进行遍历,增强了灵活性和扩展性。
  • 支持多种遍历方式:可以实现多种不同的迭代器,如正向、反向、递归等。

5. 缺点

  • 增加复杂性:引入了额外的类和接口,增加了系统的复杂性。
  • 性能开销:迭代器模式可能会导致性能开销,特别是在频繁创建和销毁迭代器的情况下。

总结

迭代器模式是一种强大的设计模式,特别适合用于集合类的设计。它通过提供统一的访问接口,使得客户端能够轻松遍历不同类型的集合,同时保持代码的清晰和灵活。

相关推荐
老蒋每日coding2 小时前
AI Agent 设计模式系列(十九)—— 评估和监控模式
人工智能·设计模式
会员果汁3 小时前
23.设计模式-解释器模式
设计模式·解释器模式
「QT(C++)开发工程师」10 小时前
C++设计模式
开发语言·c++·设计模式
茶本无香11 小时前
设计模式之七—装饰模式(Decorator Pattern)
java·设计模式·装饰器模式
漂洋过海的鱼儿1 天前
设计模式——EIT构型(三)
java·网络·设计模式
老蒋每日coding1 天前
AI Agent 设计模式系列(十八)—— 安全模式
人工智能·安全·设计模式
老蒋每日coding1 天前
AI Agent 设计模式系列(十六)—— 资源感知优化设计模式
人工智能·设计模式·langchain
老蒋每日coding1 天前
AI Agent 设计模式系列(十七)—— 推理设计模式
人工智能·设计模式
冷崖1 天前
桥模式-结构型
c++·设计模式
连山齐名1 天前
设计模式之一——堵塞队列
设计模式