手机
当前位置:查字典教程网 >编程开发 >IOS开发 >iOS App设计模式开发中对迭代器模式的使用示例
iOS App设计模式开发中对迭代器模式的使用示例
摘要:何为迭代器模式?迭代器提供了一种顺序访问集合对象中元素的方法,而无需暴漏结构的底层表示和细节。遍历集合中元素的职能从集合本身转移到迭代器对象...

何为迭代器模式?

迭代器提供了一种顺序访问集合对象中元素的方法,而无需暴漏结构的底层表示和细节。遍历集合中元素的职能从集合本身转移到迭代器对象。迭代器定义了一个用于访问集合元素并记录当前元素的接口。不同的迭代器可以执行不同的策略。

例子

说了这么多,下面给大家展示一下类关系图。

iOS App设计模式开发中对迭代器模式的使用示例1

上图中Client的右边是迭代器,左边是具体迭代的类型,在迭代器内部对具体需要迭代的类型进行了引用,还算不难理解吧,呵呵。其实,看起来是为了对具体类型进行解耦。好啦,下面给出具体的代码实现,简单的模拟了迭代器模式。

注意:本文所有代码均在ARC环境下编译通过。

Iterator类接口

复制代码 代码如下:

#import <Foundation/Foundation.h>

@interface Iterator:NSObject

-(id)First;

-(id)Next;

-(BOOL)IsDone;

-(id)CurrentItem;

@end

Iterator类实现

复制代码 代码如下:

#import "Iterator.h"

@implementation Iterator

-(id)First{

return nil;

}

-(id)Next{

return nil;

}

-(BOOL)IsDone{

return NO;

}

-(id)CurrentItem{

return nil;

}

@end

ConcreteIterator类接口

复制代码 代码如下:

#import "Iterator.h"

@class ConcreteAggregate;

@interface ConcreteIterator :Iterator{

ConcreteAggregate *myAggregate;

int current;

}

-(ConcreteIterator*)MyInit:(ConcreteAggregate*)aggregate;

@end

ConcreteIterator类实现

复制代码 代码如下:

#import "ConcreteIterator.h"

#import "ConcreteAggregate.h"

@implementation ConcreteIterator

-(ConcreteIterator*)MyInit:(ConcreteAggregate*)aggregate{

myAggregate = aggregate;

return self;

}

-(id)First{

return [myAggregate GetObject:0];

}

-(id)Next{

current++;

if(current< [myAggregate GetCount])

return [myAggregate GetObject:current];

else {

return nil;

}

}

-(BOOL)IsDone{

return current>= [myAggregate GetCount] ?YES:NO;

}

-(id)CurrentItem{

return [myAggregate GetObject:current];

}

@end

Aggregate类接口

复制代码 代码如下:

#import <Foundation/Foundation.h>

@class Iterator;

@interface Aggregate:NSObject

-(Iterator*)CreateIterator;

@end

Aggregate类实现

复制代码 代码如下:

#import "Aggregate.h"

#import "Iterator.h"

@implementation Aggregate

-(Iterator*)CreateIterator{

return [[Iterator alloc]init];

}

@end

ConcreteAggregate类接口

复制代码 代码如下:

#import "Aggregate.h"

@interface ConcreteAggregate:Aggregate{

NSMutableArray *items;

}

-(int)GetCount;

-(id)GetObject:(int)index;

-(void)InsertObject:(id)Obj;

@end

ConcreteAggregate类实现

复制代码 代码如下:

#import "ConcreteAggregate.h"

#import "Iterator.h"

@implementation ConcreteAggregate

-(id)init{

if(self == [super init]){

items = [NSMutableArray new];

}

return self;

}

-(Iterator*)CreateIterator{

return [[Iterator alloc]init];

}

-(id)GetObject:(int)index{

return [items objectAtIndex:index];

}

-(void)InsertObject:(id)Obj{

[items addObject:Obj];

}

-(int)GetCount{

return [items count];

}

@end

Main方法调用

复制代码 代码如下:

import <Foundation/Foundation.h>

#import "ConcreteAggregate.h"

#import "Iterator.h"

#import "ConcreteIterator.h"

int main (int argc, const char *argv[])

{

@autoreleasepool {

ConcreteAggregate *a = [[ConcreteAggregate alloc]init];

[a InsertObject:@"张三"];

[a InsertObject:@"李四"];

[a InsertObject:@"王二"];

[a InsertObject:@"麻子"];

NSLog(@"Count:%d", [a GetCount]);

Iterator *i = [[ConcreteIterator alloc]MyInit:a];

while (![i IsDone]) {

NSLog(@"%@,请买票",[i CurrentItem]);

[i Next];

}

}

return 0;

}

好啦,上面的四个类型简单实现了迭代器模式,其实迭代器模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合的内部结构,又可以让外部代码透明地访问集合内部地数据。

何时使用迭代器模式?

1.需要访问组合对象的内容,而又不暴漏其内部表示。

2.需要通过多种方式遍历组合对象。

3.需要提供一个统一的接口,用来遍历各种类型的组合对象。

【iOS App设计模式开发中对迭代器模式的使用示例】相关文章:

实例讲解设计模式中的命令模式在iOS App开发中的运用

iOS App的设计模式开发中对State状态模式的运用

iOS App设计模式开发中对interpreter解释器模式的运用

iOS应用开发中监听键盘事件的代码实例小结

iOS开发中实现邮件和短信发送的简单示例

详解Objective-C编程中对设计模式中适的配器模式的使用

iOS开发中一些手写控件及其相关属性的使用

详解iOS应用开发中使用设计模式中的抽象工厂模式

iOS App开发中Masonry布局框架的基本用法解析

iOS开发中Quartz2D绘图路径的使用以及条纹效果的实现

精品推荐
分类导航