美文网首页
迭代器模式

迭代器模式

作者: 泥孩儿0107 | 来源:发表于2017-12-20 14:20 被阅读0次

    何为迭代器模式?

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

    例子

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

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

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

    Iterator类接口

    复制代码代码如下:

    #import

    @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

    @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

    #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.需要提供一个统一的接口,用来遍历各种类型的组合对象。

    相关文章

      网友评论

          本文标题:迭代器模式

          本文链接:https://www.haomeiwen.com/subject/gasgwxtx.html