icon图标素材下载网站,wordpress 4.0 中文,直播网站 咋做,为什么要推行政务公开网站建设迭代器模式 (Iterator)
迭代器模式 是一种行为型设计模式#xff0c;它提供了一种方法#xff0c;顺序访问一个聚合对象中的各个元素#xff0c;而又不需要暴露该对象的内部表示。 意图
提供一种方法#xff0c;可以顺序访问一个容器对象中的元素#xff0c;而无需暴露其…迭代器模式 (Iterator)
迭代器模式 是一种行为型设计模式它提供了一种方法顺序访问一个聚合对象中的各个元素而又不需要暴露该对象的内部表示。 意图
提供一种方法可以顺序访问一个容器对象中的元素而无需暴露其内部实现。将遍历行为从集合对象中分离出来使得遍历行为可以独立变化。 使用场景
需要顺序访问一个聚合对象中的元素 如数组、链表或集合。 需要支持多种遍历方式 如正序遍历、反序遍历等。 需要解耦遍历算法和容器实现 容器的内部结构可能经常变化但不希望影响遍历逻辑。 参与者角色
迭代器接口 (Iterator) 定义用于访问聚合对象元素的方法。 具体迭代器 (ConcreteIterator) 实现迭代器接口提供对聚合对象的具体遍历。 聚合接口 (Aggregate) 定义创建迭代器的方法。 具体聚合类 (ConcreteAggregate) 实现聚合接口提供一个数据集合并创建相应的迭代器。 示例代码
以下代码展示了迭代器模式的实现用于模拟对一个数字集合的顺序访问。
#include iostream
#include vector
#include memory// 迭代器接口
template typename T
class Iterator {
public:virtual ~Iterator() default;// 获取下一个元素virtual T next() 0;// 判断是否还有下一个元素virtual bool hasNext() const 0;
};// 聚合接口
template typename T
class Aggregate {
public:virtual ~Aggregate() default;// 创建迭代器virtual std::unique_ptrIteratorT createIterator() const 0;
};// 具体迭代器
template typename T
class ConcreteIterator : public IteratorT {
private:const std::vectorT collection; // 引用聚合对象size_t index; // 当前索引public:explicit ConcreteIterator(const std::vectorT collection): collection(collection), index(0) {}T next() override {return collection[index];}bool hasNext() const override {return index collection.size();}
};// 具体聚合类
template typename T
class ConcreteAggregate : public AggregateT {
private:std::vectorT collection; // 存储元素的集合public:void add(const T element) {collection.push_back(element);}std::unique_ptrIteratorT createIterator() const override {return std::make_uniqueConcreteIteratorT(collection);}
};// 客户端代码
int main() {// 创建一个具体聚合对象ConcreteAggregateint aggregate;aggregate.add(1);aggregate.add(2);aggregate.add(3);aggregate.add(4);aggregate.add(5);// 获取迭代器auto iterator aggregate.createIterator();// 遍历聚合对象std::cout 集合中的元素: ;while (iterator-hasNext()) {std::cout iterator-next() ;}std::cout
;return 0;
}代码解析
1. 迭代器接口 (Iterator)
定义了 next 和 hasNext 方法用于访问聚合对象中的元素。子类需要实现这些方法以提供具体的遍历逻辑。
template typename T
class Iterator {
public:virtual ~Iterator() default;virtual T next() 0;virtual bool hasNext() const 0;
};2. 具体迭代器 (ConcreteIterator)
实现了迭代器接口。使用内部索引 index 记录当前遍历的位置并提供 next 和 hasNext 方法。
template typename T
class ConcreteIterator : public IteratorT {
private:const std::vectorT collection;size_t index;
public:explicit ConcreteIterator(const std::vectorT collection): collection(collection), index(0) {}T next() override { return collection[index]; }bool hasNext() const override { return index collection.size(); }
};3. 聚合接口 (Aggregate)
定义了 createIterator 方法用于创建迭代器。
template typename T
class Aggregate {
public:virtual ~Aggregate() default;virtual std::unique_ptrIteratorT createIterator() const 0;
};4. 具体聚合类 (ConcreteAggregate)
存储数据集合并实现了 createIterator 方法返回具体的迭代器实例。
template typename T
class ConcreteAggregate : public AggregateT {
private:std::vectorT collection;
public:void add(const T element) { collection.push_back(element); }std::unique_ptrIteratorT createIterator() const override {return std::make_uniqueConcreteIteratorT(collection);}
};5. 客户端代码
客户端通过聚合对象获取迭代器并通过迭代器访问集合中的元素。
int main() {ConcreteAggregateint aggregate;aggregate.add(1);aggregate.add(2);aggregate.add(3);auto iterator aggregate.createIterator();while (iterator-hasNext()) {std::cout iterator-next() ;}
}优缺点
优点
统一遍历接口 提供了统一的遍历接口客户端无需了解容器的具体实现。 解耦遍历算法和容器实现 容器的内部结构可以改变而不影响遍历逻辑。 支持多种遍历方式 可以为同一个容器实现多种迭代器支持不同的遍历方式。
缺点
类数量增加 每种容器需要实现对应的迭代器类。 遍历效率可能降低 相比直接访问容器元素使用迭代器可能增加额外的开销。 适用场景
需要顺序访问聚合对象中的元素 如遍历集合、列表或数组。 需要支持多种遍历方式 希望为容器实现不同的遍历逻辑。 希望解耦遍历算法和容器实现 通过迭代器封装遍历逻辑避免依赖容器的内部实现。 总结
迭代器模式通过将遍历逻辑封装到迭代器中实现了聚合对象与遍历算法的解耦。它特别适用于需要顺序访问容器元素同时希望支持多种遍历方式的场景。