Objective C 迭代器模式

迭代器模式(Iterator),提供一種方法順序訪問一個聚合對象中各個元素,而又不暴露該對象的內部表示。

   那麼一般在什麼時候纔會用迭代器模式呢?當你需要訪問一個聚集對象,而且不管這些對象是什麼都需要遍歷的時候,你就應該考慮用迭代器模式。另外,當你需要對聚集有多種方式遍歷時,也可以考慮用迭代器模式。

   說了這麼多,下面給大家展示一下類關係圖。


   上圖中Client的右邊是迭代器,左邊是具體迭代的類型,在迭代器內部對具體需要迭代的類型進行了引用,還算不難理解吧,呵呵。其實,看起來是爲了對具體類型進行解耦。好啦,下面給出具體的代碼實現,簡單的模擬了迭代器模式。

  • Iterator類接口

1
2
3
4
5
6
7
8
#import <Foundation/Foundation.h>
                       
@interface Iterator:NSObject
-(id)First;
-(id)Next;
-(BOOL)IsDone;
-(id)CurrentItem;
@end
  • Iterator類實現

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import "Iterator.h"
                     
@implementation Iterator
                     
-(id)First{
    return nil;
}
-(id)Next{
    return nil;
}
-(BOOL)IsDone{
    return NO;
}
-(id)CurrentItem{
    return nil;
}
                     
@end
  • ConcreteIterator類接口

1
2
3
4
5
6
7
8
9
#import "Iterator.h"
                   
@class ConcreteAggregate;
@interface ConcreteIterator :Iterator{
    ConcreteAggregate *myAggregate;
    int current;
}
-(ConcreteIterator*)MyInit:(ConcreteAggregate*)aggregate;
@end
  • ConcreteIterator類實現

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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類接口

1
2
3
4
5
6
#import <Foundation/Foundation.h>
               
@class Iterator;
@interface Aggregate:NSObject
-(Iterator*)CreateIterator;
@end
  • Aggregate類實現

1
2
3
4
5
6
7
8
#import "Aggregate.h"
#import "Iterator.h"
             
@implementation Aggregate
-(Iterator*)CreateIterator{
    return [[Iterator alloc]init];
}
@end
  • ConcreteAggregate類接口

1
2
3
4
5
6
7
8
9
#import "Aggregate.h"
           
@interface ConcreteAggregate:Aggregate{
    NSMutableArray *items;
}
-(int)GetCount;
-(id)GetObject:(int)index;
-(void)InsertObject:(id)Obj;
@end
  • ConcreteAggregate類實現

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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方法調用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
}

   好啦,上面的四個類型簡單實現了迭代器模式,其實迭代器模式就是分離了集合對象的遍歷行爲,抽象出一個迭代器類來負責,這樣既可以做到不暴露集合的內部結構,又可以讓外部代碼透明地訪問集合內部地數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章