赞
踩
迭代器模式,用于提供一种方法来顺序访问一个聚合对象中的各个元素,而又不需暴露该对象的内部表示。它将遍历和聚合分离开来,使得可以独立地改变遍历方法而不影响聚合对象。这种模式通常包括一个迭代器接口定义和一个聚合对象接口定义,以及相应的迭代器和聚合对象的实现类。
实现场景:设计一个迭代器和聚合对象,迭代器依次遍历聚合对象中的数据。
`ConcreteAggregate`类表示了一个具体的聚合对象,它使用了`std::vector`作为内部存储容器。`ConcreteIterator`类表示了具体的迭代器,它使用了索引来遍历聚合对象中的元素。在`main`函数中,我们创建了一个`ConcreteAggregate`对象并添加了一些元素,然后使用迭代器遍历这些元素并输出它们的值。
- #include <iostream>
- #include <vector>
-
- // 迭代器接口
- class Iterator {
- public:
- virtual bool hasNext() const = 0;
- virtual int next() = 0;
- };
-
- // 聚合对象接口
- class Aggregate {
- public:
- virtual Iterator* createIterator() const = 0;
- virtual void add(int element) = 0;
- virtual int size() const = 0;
- virtual int get(int index) const = 0;
- };
-
- // 具体的迭代器实现
- class ConcreteIterator : public Iterator {
- private:
- const Aggregate& aggregate;
- int index;
-
- public:
- ConcreteIterator(const Aggregate& agg) : aggregate(agg), index(0) {}
-
- bool hasNext() const override {
- return index < aggregate.size();
- }
-
- int next() override {
- return aggregate.get(index++);
- }
- };
-
- // 具体的聚合对象实现
- class ConcreteAggregate : public Aggregate {
- private:
- std::vector<int> elements;
-
- public:
- Iterator* createIterator() const override {
- return new ConcreteIterator(*this);
- }
-
- void add(int element) override {
- elements.push_back(element);
- }
-
- int size() const override {
- return elements.size();
- }
-
- int get(int index) const override {
- return elements[index];
- }
- };
-
- int main() {
- ConcreteAggregate aggregate;
- aggregate.add(1);
- aggregate.add(2);
- aggregate.add(3);
-
- Iterator* iterator = aggregate.createIterator();
- while (iterator->hasNext()) {
- std::cout << iterator->next() << " ";
- }
- std::cout << std::endl;
-
- delete iterator;
-
- return 0;
- }

`ConcreteAggregate`表示了一个具体的聚合对象,它使用了 Java 的 `ArrayList` 作为内部存储容器。`ConcreteIterator`表示了具体的迭代器,它使用了索引来遍历聚合对象中的元素。在 `main` 方法中,我们创建了一个 `ConcreteAggregate` 对象并添加了一些元素,然后使用迭代器遍历这些元素并输出它们的值。
- package behavioralpattern.iterator;
- import java.util.ArrayList;
- import java.util.List;
-
- public class IteratorDemo {
- // 迭代器接口
- interface Iterator {
- boolean hasNext();
- int next();
- }
-
- // 聚合对象接口
- interface Aggregate {
- Iterator createIterator();
- }
-
- // 具体迭代器类
- static class ConcreteIterator implements Iterator {
- private List<Integer> elements;
- private int position;
-
- ConcreteIterator(List<Integer> elements) {
- this.elements = elements;
- this.position = 0;
- }
-
- public boolean hasNext() {
- return position < elements.size();
- }
-
- public int next() {
- return elements.get(position++);
- }
- }
-
- // 具体聚合对象类
- static class ConcreteAggregate implements Aggregate {
- private List<Integer> elements;
-
- ConcreteAggregate() {
- elements = new ArrayList<>();
- }
-
- public void add(int element) {
- elements.add(element);
- }
-
- public Iterator createIterator() {
- return new ConcreteIterator(elements);
- }
- }
-
- public static void main(String[] args) {
- ConcreteAggregate aggregate = new ConcreteAggregate();
- aggregate.add(1);
- aggregate.add(2);
- aggregate.add(3);
-
- Iterator iterator = aggregate.createIterator();
- while (iterator.hasNext()) {
- System.out.print(iterator.next() + " ");
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。