设计模式C++学习笔记之十四(Iterator迭代器模式)
14.1.解释
概念:提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。
main(),客户
IProject,产品接口
CProject,产品类
IIterator,迭代器接口
IProjectIterator,产品迭代器接口
CProjectIterator,产品迭代器实现类
说明:CProject实现产品类,能够返回一个迭代器的指针。这个迭代器将封装产品类里的一个数组。所以迭代器在运行Next函数时,可以遍历这个数组的所有元素。
简单来说,就是用代码实现vector<int>::iterator或vector<int>::const_iterator。
我们来看代码:
//IProject.h
#pragma once
#include "IProjectIterator.h"
#include <iostream>
using std::string;
class IProject
{
public:
IProject(void)
{
}
virtual ~IProject(void)
{
}
virtual void Add(string name, int num, int cost) = 0;
virtual string GetProjectInfo() = 0;
virtual IProjectIterator* GetIterator() = 0;
virtual void Erase() = 0;
};
//Project.h
#pragma once
#include "iproject.h"
#include "IProjectIterator.h"
#include <iostream>
#include <vector>
using std::string;
using std::vector;
class CProject :
public IProject
{
public:
CProject(void);
CProject(string name, int num, int cost);
~CProject(void);
string GetProjectInfo();
void Add(string name, int num, int cost);
IProjectIterator * GetIterator();
void Erase();
private:
string m_name;
int m_num;
int m_cost;
vector<IProject*> m_projectList;
};
//Project.cpp
#include "StdAfx.h"
#include "Project.h"
#include "..\CommonDeclare\Convert.h"
#include "ProjectIterator.h"
#include <iostream>
#include <vector>
using std::string;
using std::vector;
CProject::CProject( void )
{
m_name = "";
m_num = 0;
m_cost = 0;
}
CProject::CProject(string name, int num, int cost) :m_name(name), m_num(num), m_cost(cost)
{
}
CProject::~CProject(void)
{
}
string CProject::GetProjectInfo()
{
string info = "";
info.append("项目名称是:");
info.append(this->m_name);
info.append("\t项目人数:");
info.append(CConvert::ToString(m_num));
info.append("\t项目费用:");
info.append(CConvert::ToString(m_cost));
return info;
}
void CProject::Add( string name, int num, int cost )
{
this->m_projectList.push_back(new CProject(name, num, cost));
}
IProjectIterator * CProject::GetIterator()
{
return new CProjectIterator(this->m_projectList);
}
void CProject::Erase()
{
vector<IProject*>::reverse_iterator projectDelIt = m_projectList.rbegin();
for (; projectDelIt != m_projectList.rend(); projectDelIt++)
{
delete (*projectDelIt);
(*projectDelIt) = NULL;
}
m_projectList.clear();
}
//IIterator.h
#pragma once
class IProject;
class IIterator
{
public:
IIterator(void)
{
}
virtual ~IIterator(void)
{
}
virtual bool HasNext() = 0;
virtual IProject * Next() = 0;
};
//IProjectIterator.h
#pragma once
#include "iiterator.h"
class IProject;
class IProjectIterator :
public IIterator
{
public:
IProjectIterator(void)
{
}
virtual ~IProjectIterator(void)
{
}
virtual bool HasNext() = 0;
virtual IProject * Next() = 0;
};
//ProjectIterator.h
#pragma once
#include "iprojectiterator.h"
#include "IProject.h"
#include <vector>
using std::vector;
class CProjectIterator :
public IProjectIterator
{
public:
CProjectIterator(vector<IProject *> pl);
~CProjectIterator(void);
bool HasNext();
IProject * Next();
private:
vector<IProject *> m_projectList;
size_t m_currentItem;
};
//ProjectIterator.cpp
#include "StdAfx.h"
#include "ProjectIterator.h"
CProjectIterator::CProjectIterator(vector<IProject *> pl) : m_projectList(pl)
{
m_currentItem = 0;
}
CProjectIterator::~CProjectIterator(void)
{
}
bool CProjectIterator::HasNext()
{
bool b = true;
if (m_currentItem >= m_projectList.size())
b = false;
return b;
}
IProject * CProjectIterator::Next()
{
IProject *pp = m_projectList.at(m_currentItem ++);
return pp;
}
// Iterator.cpp
#include "stdafx.h"
#include "IProject.h"
#include "Project.h"
#include "..\CommonDeclare\Convert.h"
#include "ProjectIterator.h"
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::endl;
void DoIt()
{
cout << "----------未使用迭代模式----------" << endl;
vector<IProject*> projectList;
projectList.push_back(new CProject("星球大战项目", 10, 100000));
projectList.push_back(new CProject("扭转时空项目", 100, 10000000));
projectList.push_back(new CProject("超人改造项目", 10000, 1000000000));
for (int i = 4; i < 6; i ++)
{
string name = "";
name.append("第");
name.append(CConvert::ToString(i));
name.append("个项目");
projectList.push_back(new CProject(name, i * 5, i * 1000000));
}
vector<IProject*>::const_iterator projectIt = projectList.begin();
for (; projectIt != projectList.end(); projectIt++)
cout << (*projectIt)->GetProjectInfo().c_str() << endl;
vector<IProject*>::reverse_iterator projectDelIt = projectList.rbegin();
for (; projectDelIt != projectList.rend(); projectDelIt++)
{
delete (*projectDelIt);
(*projectDelIt) = NULL;
}
projectList.clear();
}
void DoNew()
{
cout << "----------使用迭代模式----------" << endl;
IProject *pproject = new CProject();
pproject->Add("星球大战项目", 10, 100000);
pproject->Add("扭转时空项目", 100, 10000000);
pproject->Add("超人改造项目", 10000, 1000000000);
for (int i = 4; i < 6; i ++)
{
string name = "";
name.append("第");
name.append(CConvert::ToString(i));
name.append("个项目");
pproject->Add(name, i * 5, i * 1000000);
}
IProjectIterator *pprojectIt = pproject->GetIterator();
while(pprojectIt->HasNext())
{
IProject *p = dynamic_cast<IProject*>(pprojectIt->Next());
cout << p->GetProjectInfo().c_str() << endl;
}
delete pprojectIt;
pprojectIt = NULL;
pproject->Erase();
delete pproject;
pproject = NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
//使用Iterator模式之前
DoIt();
//使用Iterator
DoNew();
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
_CrtDumpMemoryLeaks();
return 0;
}
这个模式,可能有一点绕,需要再仔细的思考一番。本来希望把模式的工作方式,用最简单易懂的语言来总结,但其实这个总结更难。简单的模式还容易些理解,也容易总结,复杂的就难了。打算在之后,学习数据结构的过程中,都总结出这样一句话来。很期待后面对数据结构的学习,发现自己越来越爱学习了。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。