设计模式之迭代器模式

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

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. 缺点

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

总结

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

相关推荐
Meya11271 小时前
U位资产管理系统:数据中心“最后一公里“的精细化治理
设计模式·开源·交互
回忆2012初秋14 小时前
工厂方法模式完整实现:GPS转换
设计模式·工厂方法模式
胡志辉的博客17 小时前
多智能体协作,不是多开几个 Agent:从中介者模式看 OpenClaw 和 Hermes Agent
人工智能·设计模式·ai·agent·中介者模式·openclaw·herman
shark222222217 小时前
能懂!基于Springboot的用户增删查改(三层设计模式)
spring boot·后端·设计模式
014-code19 小时前
日志规范:怎么写才不算写废话
java·开发语言·设计模式·日志
楼田莉子19 小时前
同步/异步日志系统:日志落地模块\日志器模块\异步日志模块
linux·服务器·c++·学习·设计模式
kyriewen111 天前
代码写成一锅粥?这5种设计模式让你的项目“起死回生”
前端·javascript·设计模式·typescript·ecmascript·html5
kyriewen1 天前
代码写成一锅粥?这5种设计模式让你的项目“起死回生”
前端·javascript·设计模式
两年半的个人练习生^_^1 天前
每日一学:设计模式之原型模式
java·开发语言·设计模式·原型模式
断眉的派大星2 天前
工厂模式(Factory Pattern)完整详解
python·设计模式