松垮垮 松垮垮
首页
  • GPU并行编程
  • 图形学
  • 归并算法
  • 计算机视觉
  • css
  • html
  • JavaScript
  • vue
  • 压缩命令
  • cmdline
  • Docker
  • ftrace跟踪技术
  • gcov代码覆盖率测试
  • GDB
  • git
  • kgdb
  • linux操作
  • markdown
  • systemtap
  • valgrind
  • 设计模式
  • 分布式
  • 操作系统
  • 数据库
  • 服务器
  • 网络
  • C++
  • c语言
  • go
  • JSON
  • Makefile
  • matlab
  • OpenGL
  • python
  • shell
  • 正则表达式
  • 汇编
  • GPU并行编程
  • mysql
  • nginx
  • redis
  • 网络
  • 计算机视觉
  • 进程管理
  • linux调试
  • 【Python】:re.error bad escape i at position 4
  • 搭建ai知识助手
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

松垮垮

c++后端开发工程师
首页
  • GPU并行编程
  • 图形学
  • 归并算法
  • 计算机视觉
  • css
  • html
  • JavaScript
  • vue
  • 压缩命令
  • cmdline
  • Docker
  • ftrace跟踪技术
  • gcov代码覆盖率测试
  • GDB
  • git
  • kgdb
  • linux操作
  • markdown
  • systemtap
  • valgrind
  • 设计模式
  • 分布式
  • 操作系统
  • 数据库
  • 服务器
  • 网络
  • C++
  • c语言
  • go
  • JSON
  • Makefile
  • matlab
  • OpenGL
  • python
  • shell
  • 正则表达式
  • 汇编
  • GPU并行编程
  • mysql
  • nginx
  • redis
  • 网络
  • 计算机视觉
  • 进程管理
  • linux调试
  • 【Python】:re.error bad escape i at position 4
  • 搭建ai知识助手
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 压缩命令
  • cmdline
  • Docker
  • ftrace跟踪技术
  • gcov代码覆盖率测试
  • GDB
  • git
  • kgdb
  • linux操作
  • markdown
  • systemtap
  • valgrind
  • 设计模式

    • Adapter(适配器模式、包装模式)
    • Bridge桥接模式
    • Builder建造者模式
    • Chain
    • Command命令模式
    • Composite组合模式
    • Decorator装饰者
    • Façade门面模式(外观模式)
    • Flyweight享元模式
    • Interpreter解释器模式
    • Iterator迭代器
    • Mediator中介者模式(仲裁者模式)
    • Memento备忘录模式
    • Observer观察者模式
    • Prototype原型
    • Proxy代理模式
    • Singleton单例模式
    • State状态模式
    • Strategy战略
    • Template
    • Visitor访问者模式
    • •Abstract
    • •Factory
    • 依赖注入(控制反转IoC)
    • 概论
    • 设计模式
  • 工具和开发
  • 设计模式
songkuakua
2025-02-15

Iterator迭代器

# Iterator迭代器

Owner: -QVQ-

行为型的软件设计模式,提供一种方法能顺序访问聚合对象中的各个元素,而又不暴露其内部。

迭代器的意义就是将这个行为抽离封装起来,这样客户端只需要调用合适的迭代器,来进行对应的遍历,而不用自己去实现这一行为。

优点:

  1. 符合单一职责原则。将遍历行为抽离成单独的类。
  2. 符合开闭原则。添加新集合或者新迭代器,不改变原有代码。
  3. 便于扩展多种遍历行为。
  4. 访问数据又不暴露内部。

缺点:

  1. 若对聚合对象只需要进行简单的遍历行为,那使用迭代器模式有些大材小用。
  2. 系统复杂性提高,类数量较多。 Untitled

代码:

// 定义迭代器接口
class Iterator 
{
public:
	// 下一个
	virtual int next() = 0;
 
	// 是否有下一个
	virtual bool hasNext() = 0;
 
};
 
// 定义具体迭代器
class ConcreteIterator : public Iterator 
{
public:
	// 构造函数
	ConcreteIterator(std::vector<int> data) : m_data(data), index(0) {}
 
	// 下一个
	virtual int next() {
		return m_data[index++];
	}
 
	// 是否有下一个
	virtual bool hasNext() {
		return index < m_data.size();
	}
 
private:
	std::vector<int> m_data;
	int index;
 
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 定义容器接口
class Container 
{
public:
	virtual Iterator* create_iterator() = 0;
};
 
// 定义具体容器
class ConcreteContainer : public Container 
{
public:
	// 构造函数
	ConcreteContainer(std::vector<int> data) : m_data(data) {}
 
	// 创建迭代器
	virtual Iterator* create_iterator() {
		return new ConcreteIterator(m_data);
	}
 
private:
	std::vector<int> m_data;
 
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 创建容器和迭代器
	std::vector<int> data = { 1, 2, 3, 4, 5 };
	Container* container = new ConcreteContainer(data);
	Iterator* iterator = container->create_iterator();
 
	// 迭代器输出
	while (iterator->hasNext()) {
		std::cout << iterator->next() << " ";
	}
1
2
3
4
5
6
7
8
9

迭代器模式可以看作是桥接模式的一种特殊情况

上次更新: 2025/02/21, 14:57:10
Interpreter解释器模式
Mediator中介者模式(仲裁者模式)

← Interpreter解释器模式 Mediator中介者模式(仲裁者模式)→

最近更新
01
搭建ai知识助手
02-23
02
边缘检测
02-15
03
css
02-15
更多文章>
Theme by Vdoing | Copyright © 2025-2025 松垮垮 | MIT License | 蜀ICP备2025120453号 | 川公网安备51011202000997号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 纯净模式