《大话设计模式》C++实现:20 迭代器模式

1、什么是迭代器模式?

1、迭代器模式(Iterator),提供了一种顺序访问一个聚合对象中各个元素,而又不保留该对象的内部表示的方法。
2、简单来说,迭代器模式,分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合的内部结构,又可让外部代码透明地党文集合内部的数据。
3、类似于 foreach in ,不需要知道集合对象是什么,就可以遍历所有对象的循环工具。

2、迭代器模式的适用场景?

1、当需要访问一个聚集对象,且,不管这些对象是什么,都需要遍历的时候,应该考虑迭代器模式。
2、需要对聚集对象有多种方式遍历时,可以考虑迭代器模式。
3、使用现状:
迭代器模式在访问数据、集合、列表等数据时,尤其是数据库操作时,是非常普遍的引用,但由于它太普通了,所以各种高级语言都对它进行了封装,故,反而给人感觉迭代器模式本身不太常用了。

3、怎样使用迭代器模式?

在这里插入图片描述

4、实例

1、Iterator.h

#pragma once
#include <string>
class Iterator
{
public:
	virtual std::string first() = 0;
	virtual std::string next() = 0;
	virtual bool isDone() = 0;
	virtual std::string currentItem() = 0;
};

2、Aggregate.h

#pragma once
#include <string>
#include "Iterator.h"

class Aggregate
{
public:
	virtual int count() = 0;
	virtual void push(const std::string& strValue) = 0;
	virtual std::string operator[](int nIndex) = 0;
	virtual Iterator* createIterator() = 0;
};

3、ConcreteIterator.h

#pragma once
#include "Iterator.h"
#include "Aggregate.h"
class ConcreteIterator :
	public Iterator
{
public:
	ConcreteIterator(Aggregate* pAggregate);
	std::string first();
	std::string next();
	bool isDone();
	std::string currentItem();
private:
	Aggregate* m_pAggregate{ nullptr };
	int m_nCurrent{ 0 };
};

ConcreteIterator::ConcreteIterator(Aggregate* pAggregate)
	:Iterator(), m_pAggregate(pAggregate)
{
}

std::string ConcreteIterator::first()
{
	return (*m_pAggregate)[0];
}

std::string ConcreteIterator::next()
{
	std::string strRet;
	m_nCurrent++;
	if (m_nCurrent < m_pAggregate->count())
	{
		strRet = (*m_pAggregate)[m_nCurrent];
	}
	return strRet;
}

bool ConcreteIterator::isDone()
{
	return (m_nCurrent >= m_pAggregate->count()) ? true : false;
}

std::string ConcreteIterator::currentItem()
{
	return (*m_pAggregate)[m_nCurrent];
}

4、ConcreteAggregate.h

#pragma once
#include "Aggregate.h"
#include "ConcreteIterator.h"
#include <vector>
class ConcreteAggregate :
	public Aggregate
{
public:
	~ConcreteAggregate();
	int count() override;
	void push(const std::string& strValue)override;
	//重载下标运算符,只重载了取值功能
	std::string operator[](int nIndex);
	Iterator* createIterator();
private:
	std::vector<std::string> m_vecItems;
	Iterator* m_pIterator{ nullptr };
};

ConcreteAggregate::~ConcreteAggregate()
{
	if (nullptr != m_pIterator)
	{
		delete m_pIterator;
		m_pIterator = nullptr;
	}
}

int ConcreteAggregate::count()
{
	return m_vecItems.size();
}

void ConcreteAggregate::push(const std::string& strValue)
{
	m_vecItems.push_back(strValue);
}

//下标运算符感觉只重载了一半,其他参考函数都是返回引用,我这边返回引用会崩溃,不知道什么原因?
std::string ConcreteAggregate::operator[](int nIndex)
{
	std::string strRet;
	if (nIndex < count())
	{
		strRet = m_vecItems[nIndex];
	}
	return strRet;
}

Iterator* ConcreteAggregate::createIterator()
{
	if (nullptr == m_pIterator)
	{
		m_pIterator = new ConcreteIterator(this);
	}
	return m_pIterator;
}

5、测试程序

#include "iterator.h"
#include "ConcreteAggregate.h"
#include <iostream>
void test()
{
	ConcreteAggregate* pName = new ConcreteAggregate();
	if (nullptr != pName)
	{
		pName->push("墨白");
		pName->push("金戈铁马");
		pName->push("气吞万里如虎");
	}
	Iterator* iter = pName->createIterator();
	if (nullptr != iter)
	{
		std::string strItem = iter->first();
		while (!iter->isDone())
		{
			std::cout << iter->currentItem() << std::endl;
			iter->next();
		}
	}
	std::cout << "================================" << std::endl;
	for (int i = 0; i < pName->count(); ++i)
	{
		std::cout << (*pName)[i] << std::endl;
	}
}

int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述
注:代码有借鉴:c++ 迭代器模式(iterator) ,做了一些更改,去掉了pop函数,增加了下标运算符[]重载。

此为《大话设计模式》学习心得系列 P202~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值