迭代器模式:提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。
优点:
1.抽象出一个迭代器类来负责遍历
2.不暴露集合的内部结构,又可让外部代码透明地访问集合内部的数据
缺点:
1.需要遍历的集合类会随着时间的增加而增加,维护起来会比较麻烦
总结:
迭代器模式在访问数组,集合,列表等数据时非常普遍,但是由于各种高级语言都对它进行了封装,所有这个模式学习意义已经大于了实际意义。
项目地址:https://github.com/GameRisker/Study-Design-Patterns.git
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 29 30 31 32 33 34 |
// // main.m // Iterator // // Created by GameRisker on 2016/10/23. // Copyright © 2016年 GameRisker. All rights reserved. // #import <Foundation/Foundation.h> #import "ConcreteAggregate.h" #import "ConcreteIterator.h" #import "Iterator.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... ConcreteAggregate *aggregate = [[ConcreteAggregate alloc]init]; [aggregate addObjectWithName:@"name0" index:0]; [aggregate addObjectWithName:@"name1" index:1]; [aggregate addObjectWithName:@"name2" index:2]; [aggregate addObjectWithName:@"name3" index:3]; [aggregate addObjectWithName:@"name4" index:4]; id iterator = [[ConcreteIterator alloc]initConcreteIterator:aggregate]; while ([iterator MoveNext]) { NSLog(@"%@",[iterator Current]); } } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// // Iterator.h // Iterator // // Created by GameRisker on 2016/10/23. // Copyright © 2016年 GameRisker. All rights reserved. // @protocol Iterator <NSObject> -(id) Current; -(bool) MoveNext; -(void) Reset; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// // Aggregate.h // Iterator // // Created by GameRisker on 2016/10/23. // Copyright © 2016年 GameRisker. All rights reserved. // @protocol Aggregate <NSObject> -(id)ConcreteIterator; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // ConcreteAggregate.h // Iterator // // Created by GameRisker on 2016/10/23. // Copyright © 2016年 GameRisker. All rights reserved. // #import "Aggregate.h" @interface ConcreteAggregate : NSObject<Aggregate> @property (nonatomic,strong) NSMutableArray<NSString*> *items; @property (nonatomic,assign,readonly) NSInteger count; -(void) addObjectWithName:(NSString*) name index:(NSInteger)index; @end |
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 29 30 31 32 33 34 35 36 37 38 39 |
// // ConcreteAggregate.m // Iterator // // Created by GameRisker on 2016/10/23. // Copyright © 2016年 GameRisker. All rights reserved. // #import <Foundation/Foundation.h> #import "ConcreteIterator.h" #import "ConcreteAggregate.h" @implementation ConcreteAggregate : NSObject -(NSMutableArray *) items { if(!_items) { _items = @[].mutableCopy; } return _items; } -(NSInteger)count { return self.items.count; } -(id) ConcreteIterator { return [[ConcreteIterator alloc]init]; } -(void) addObjectWithName:(NSString *)name index:(NSInteger)index { [self.items insertObject:name atIndex:index]; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// // ConcreteIterator.h // Iterator // // Created by GameRisker on 2016/10/23. // Copyright © 2016年 GameRisker. All rights reserved. // #import "Iterator.h" #import "ConcreteAggregate.h" @interface ConcreteIterator : NSObject<Iterator> @property (nonatomic,strong) ConcreteAggregate * aggregate; @property (nonatomic,assign) NSInteger current; -(instancetype) initConcreteIterator:(ConcreteAggregate*) aggregate; @end |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
// // ConcreteIterator.m // Iterator // // Created by GameRisker on 2016/10/23. // Copyright © 2016年 GameRisker. All rights reserved. // #import <Foundation/Foundation.h> #import "ConcreteIterator.h" @implementation ConcreteIterator : NSObject -(instancetype) initConcreteIterator:(ConcreteAggregate*) aggregate { if (self = [super init]) { [self Reset]; self.aggregate = aggregate; } return self; } -(id) Current { id name; if (self.current < self.aggregate.count) { name = self.aggregate.items[self.current]; } return name; } -(bool) MoveNext { self.current++; bool isnext =(self.current < self.aggregate.count); return isnext; } -(void) Reset { self.current = -1; } @end |