设计模式---数据结构模式之迭代器模式(Iterate)
2024-08-25 04:48:27
一:概念
迭代模式是行为模式之一,它把对容器中包含的内部对象的访问委让给外部类,使用Iterator(遍历)按顺序进行遍历访问的设计模式。
在应用Iterator模式之前,首先应该明白Iterator模式用来解决什么问题。或者说,如果不使用Iterator模式,会存在什么问题?
.由容器自己实现顺序遍历。直接在容器类里直接添加顺序遍历方法
.让调用者自己实现遍历。直接暴露数据细节给外部。
以上方法1与方法2都可以实现对遍历,这样有问题呢?
,容器类承担了太多功能:一方面需要提供添加删除等本身应有的功能;一方面还需要提供遍历访问功能。
,往往容器在实现遍历的过程中,需要保存遍历状态,当跟元素的添加删除等功能夹杂在一起,很容易引起混乱和程序运行错误等。
Iterator模式就是为了有效地处理按顺序进行遍历访问的一种设计模式,简单地说,Iterator模式提供一种有效的方法,可以屏蔽聚集对象集合的容器类的实现细节,而能对容器内包含的对象元素按顺序进行有效的遍历访问。
所以,Iterator模式的应用场景可以归纳为满足以下几个条件:
.访问容器中包含的内部对象;
.按顺序访问
二:动机
在软件构建过程中,集合对象内部结构常常变化各异。但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码透明的访问其中包含的元素;同时这种“透明遍历”也为“同一种算法在多种集合对象上进行操作”提供了可能。
使用面向对象技术将这种遍历机制抽象为“迭代器对象”为“因对变化中的集合对象”提供了一种优雅的方式。
三:模式定义
提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露(隔离变化,稳定)该对象的内部表示。 ——《设计模式》GoF
这种方式在C++现在来说已经过时了,因为泛型编程和STL中实现了迭代器。
迭代器模式最核心缺点就是出现在面向对象上,虚函数调用是有性能成本的,需要绕一个虚函数的表指针,然后找到函数地址,才能去调用,要经过指针间接运算。当循环次数太高后,性能就会被削减。
泛型编程中优点:迭代器是使用模板来实现的,而模板(也是一种多态技术),但是他实现的多态是编译时多态,编译器会在编译时判断具体是调用哪段代码。
虚函数是运行时多态,运行时多态性能低于编译时多态,编译时多态不需要计算他的地址了,因此STL性能高于面向对象模式迭代器,功能也更多。
四:类图(结构)
五:代码讲解
(一)Iterator基类
template<typename T>
class Iterator
{
public:
virtual void first() = ;
virtual void next() = ;
virtual bool isDone() const = ;
virtual T& current() = ;
};
(二)基类Collection返回迭代器
template<typename T>
class MyCollection{ public: Iterator<T> GetIterator(){
//...
} };
(三)子类CollectionIterator实现迭代方法
template<typename T>
class CollectionIterator : public Iterator<T>{
MyCollection<T> mc;
public: CollectionIterator(const MyCollection<T> & c): mc(c){ } void first() override { }
void next() override { }
bool isDone() const override{ }
T& current() override{ }
};
(四)进行调用
void MyAlgorithm()
{
MyCollection<int> mc; Iterator<int> iter= mc.GetIterator(); for (iter.first(); !iter.isDone(); iter.next()){
cout << iter.current() << endl;
} }
六:要点总结
(一)Java,C#类库是按照上面实现,因为毕竟没有编译时多态,现在C++基本没有这种实现,随着技术的发展,有些设计模式会过时,但是思想不会过时。
(二)迭代抽象:访问一个聚合对象的内容而无需暴露他的内部表示
(三)迭代多态:为遍历不同的集合结构提供一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作。
(四)迭代器的健壮性考虑:遍历的同时更改迭代器所在的集合结构,会导致问题。
七:案例实现
(一)实现迭代器基类
class Iterator
{
public:
virtual void first() = ;
virtual void next() = ;
virtual bool isDone() const= ;
virtual int& current() = ;
virtual ~Iterator(){}
};
(二)实现聚合基类
class Aggregate
{
public:
virtual Iterator* CreateIntertor() = ;
virtual int getSize() = ;
virtual int getItem(int index) = ;
virtual ~Aggregate(){}
};
(三)实现具体迭代器子类
class ConcreteIterator :public Iterator
{
private:
Aggregate* _ag;
int _idx;
public:
ConcreteIterator(Aggregate* ag)
{
_ag = ag;
_idx = ;
} virtual void first()
{
_idx = ;
} virtual void next()
{
if (_idx<_ag->getSize())
_idx++;
} virtual bool isDone() const
{
return _idx == _ag->getSize();
} virtual int current()
{
return _ag->getItem(_idx);
}
};
(四)实现聚合子类
class ConcreteAggregate :public Aggregate
{
private:
int *Object;
int size;
public:
ConcreteAggregate(int size)
{
Object = (int *)malloc(size*sizeof(int)); for (int i = ; i < size; i++)
Object[i] = i*i; this->size = size;
} virtual Iterator* CreateIntertor()
{
return new ConcreteIterator(this);
} virtual int getSize()
{
return this->size;
} virtual int getItem(int index)
{
return this->Object[index];
} ~ConcreteAggregate()
{
delete Object;
}
};
(五)结果测试
int main()
{
Aggregate* ag = new ConcreteAggregate();
Iterator* it = ag->CreateIntertor(); for (it; !it->isDone(); it->next())
cout << it->current() << endl; delete it;
delete ag; system("pause");
return ;
}
作为迭代器,最好设置为模板类吧
#include <iostream>
#include <string> using namespace std; template<typename T>
class Iterator
{
public:
virtual void first() = ;
virtual void next() = ;
virtual bool isDone() const= ;
virtual T& current() = ;
virtual ~Iterator(){}
}; template<typename T>
class Aggregate
{
public:
virtual Iterator<T>* CreateIntertor() = ;
virtual int getSize() = ;
virtual T& getItem(int index) = ;
virtual ~Aggregate(){}
}; template<typename T>
class ConcreteIterator :public Iterator<T>
{
private:
Aggregate<T>* _ag;
int _idx;
public:
ConcreteIterator(Aggregate<T>* ag)
{
_ag = ag;
_idx = ;
} virtual void first()
{
_idx = ;
} virtual void next()
{
if (_idx<_ag->getSize())
_idx++;
} virtual bool isDone() const
{
return _idx == _ag->getSize();
} virtual T& current()
{
return _ag->getItem(_idx);
}
}; template<typename T>
class ConcreteAggregate :public Aggregate<T>
{
private:
T *Object;
int size;
public:
ConcreteAggregate(int size)
{
Object = (T *)malloc(size*sizeof(T)); for (int i = ; i < size; i++)
Object[i] = +i; this->size = size;
} virtual Iterator<T>* CreateIntertor()
{
return new ConcreteIterator<T>(this);
} virtual int getSize()
{
return this->size;
} virtual T& getItem(int index)
{
return this->Object[index];
} ~ConcreteAggregate()
{
delete Object;
}
}; int main()
{
Aggregate<char>* ag = new ConcreteAggregate<char>();
Iterator<char>* it = ag->CreateIntertor(); for (it; !it->isDone(); it->next())
cout << it->current() << endl; delete it;
delete ag; system("pause");
return ;
}
使用模板函数实现(重点)
最新文章
- asp.net App_Code文件夹相关操作
- UIView 的粗浅解析
- php用jquery-ajax上传多张图片限制图片大小
- ASP.NET简单登录注册实例
- 图表控件MsChart使用demo
- fstab 介绍
- (HTTPS)-tomcat 实现 https 登录,去掉端口号
- Python安装第三方包(模块/工具)出现链接超时,网速慢,安装不上的问题如何解决
- 【Tomcat】Tomcat相关设计模式分析
- 写了个限制文本框输入最大长度的jquery插件 - jquery.restrictFieldLength.js
- linux 下动态链接实现原理
- HTML语义化
- C# winfrom 写的一个搜索助手,可以按照标题和内容搜索,支持doc,xls,ppt,pdf,txt等格式的文件搜索
- jquery 绑定文本即时查询功能
- CF438D The Child and Sequence(线段树)
- Error:Cause: org/gradle/api/publication/maven/internal/DefaultMavenFactory Android
- 9.10Django模板
- (翻译)Angular 1.3中的验证器管道
- HTML5-canvas实例:2D折线数据图与2D扇形图
- 基于maven从头搭建springMVC框架