迭代器模式:提供一个方法能迅速的访问一个聚合对象中的各个元素。而不暴露内部的表示。
迭代器模式是行为型模式之一。意在提供一种公开的,便捷的,可以迅速访问聚合对象中各个元素的方法。这样方便对不同的聚合对象统一调度。
我们开发的过程中,聚合的情况可能有多种多样,数组,hashtable, 普通数组,自定义的聚合形式,等等。如果我们在整理代码的时候如何统一的去管理这些数据形式的调用?或者说我们如何在设计架构的时候把这种情况考虑进去?
显然可以选择创建一个迭代器封装“遍历集合中每一个对象的过程”。
举个栗子
我们有两个画板用来展示英文的原音字母和辅音字母。
展示原音字母的画板由张三开发
展示辅音字母的画板由李四开发
他们处理相同的展示对象。但是张三用了系统提供的数组,而李四使用了公司的数组框架。
如何改造,让访问数据更加友好?
迭代器协议(java的接口)
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@protocol Iterator <NSObject>
-(BOOL)hasNext;
-(id)next;
-(void)remove:(id)obj;
@end
NS_ASSUME_NONNULL_END
展示字符的类(逻辑很简单,只做内存级数据存取,这里不贴实现)
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface DisplayCharactor : NSObject
@property (nonatomic ,assign)NSString * charactor;
@end
NS_ASSUME_NONNULL_END
原音字符的迭代器实现,处理系统数组
#import "OriginalSoundIterator.h"
@interface OriginalSoundIterator ()
@property (nonatomic ,strong)NSMutableArray * array;
@property (nonatomic ,assign)NSInteger index;
@end
@implementation OriginalSoundIterator
-(instancetype)initWithCharactorArray:(NSMutableArray *)array {
if (self = [super init]) {
_index = 0;
_array = array;
}return self;
}
-(id)next {
NSInteger index = _index;
_index ++;
return _array[index];
}
-(void)remove:(id)obj {
_index--;
[_array removeObject:obj];
}
-(BOOL)hasNext {
if (_index >= _array.count) {
_index = 0;
return NO;
}
return YES;
}
@end
辅音字符的迭代器实现,采用数组框架
#import "ConsonantIterator.h"
#import "CharactorArray.h"
@interface ConsonantIterator ()
@property (nonatomic ,strong)CharactorArray * array;
@property (nonatomic ,assign)NSInteger index;
@end
@implementation ConsonantIterator
-(instancetype)initWithCharactorArray:(CharactorArray *)array {
if (self = [super init]) {
_index = 0;
_array = array;
}return self;
}
-(id)next {
NSInteger index = _index;
_index ++;
return [_array objectAtIndex:index];
}
-(void)remove:(id)obj {
_index--;
[_array removeObject:obj];
}
-(BOOL)hasNext {
if (_index >= _array.length) {
_index = 0;
return NO;
}
return YES;
}
@end
原音字符画板实现
#import "OriginalSoundBoard.h"
#import "DisplayCharactor.h"
#import "OriginalSoundIterator.h"
@interface OriginalSoundBoard ()
@property (nonatomic ,strong)NSMutableArray * charactorArray;
@property (nonatomic ,strong)id <Iterator> iterator;
@end
@implementation OriginalSoundBoard
-(instancetype)init {
if (self = [super init]) {
_charactorArray = [NSMutableArray array];
NSArray <NSString *>* charactors = @[@"a",@"e",@"i",@"o",@"u"];
for (int i = 0; i < charactors.count; i++) {
[self addCharactor:charactors[i]];
}
}return self;
}
-(void)addCharactor:(NSString *)charactor{
DisplayCharactor * display = [[DisplayCharactor alloc] init];
display.charactor = charactor;
[_charactorArray addObject:display];
}
-(id<Iterator>)creatIterator {
if (!_iterator) {
_iterator = [[OriginalSoundIterator alloc] initWithCharactorArray:_charactorArray];
}
return _iterator;
}
@end
辅音字符画板实现
#import "ConsonantBoard.h"
#import "CharactorArray.h"
#import "DisplayCharactor.h"
#import "ConsonantIterator.h"
@interface ConsonantBoard ()
@property (nonatomic ,strong)CharactorArray * charactorArray;
@property (nonatomic ,strong)id <Iterator> iterator;
@end
@implementation ConsonantBoard
-(instancetype)init {
if (self = [super init]) {
_charactorArray = [[CharactorArray alloc] init];
NSArray <NSString *> * charactors = @[@"b",@"c",@"d",@"f",@"g",@"h",@"j",@"k",@"l",@"m",@"n",@"p",@"q",@"r",@"s",@"t",@"v",@"w",@"x",@"y",@"z"];
for (int i = 0; i < charactors.count; i++) {
[self addCharactor:charactors[i]];
}
}return self;
}
-(void)addCharactor:(NSString *)charactor{
DisplayCharactor * display = [[DisplayCharactor alloc] init];
display.charactor = charactor;
[_charactorArray addObject:display];
}
- (id<Iterator>)creatIterator {
if (!_iterator) {
_iterator = [[ConsonantIterator alloc] initWithCharactorArray:_charactorArray];
}
return _iterator;
}
@end
公司数组框架(随便写的一个,充当下自定义的聚合实现)
#import "CharactorArray.h"
@interface CharactorArray ()
@property (nonatomic ,strong)NSMutableArray * charactorArray;
@end
@implementation CharactorArray
-(instancetype)init {
if (self = [super init]) {
_charactorArray = [NSMutableArray array];
}return self;
}
-(void)addObject:(id)charactor {
[_charactorArray addObject:charactor];
}
-(void)removeObject:(id)charactor {
[_charactorArray removeObject:charactor];
}
-(id)objectAtIndex:(NSInteger)index {
return [_charactorArray objectAtIndex:index];
}
-(NSInteger)length {
return _charactorArray.count;
}
@end
迭代器的具体使用
#import <Foundation/Foundation.h>
#import "Iterator.h"
#import "OriginalSoundBoard.h"
#import "ConsonantBoard.h"
#import "DisplayCharactor.h"
void showOnBoard(id<Iterator> iterator);
int main(int argc, const char * argv[]) {
@autoreleasepool {
id <Iterator> origin = [[[OriginalSoundBoard alloc] init] creatIterator];
id <Iterator> consonant = [[[ConsonantBoard alloc] init] creatIterator];
NSLog(@"原音之母:");
showOnBoard(origin);
NSLog(@"辅音之母:");
showOnBoard(consonant);
}
return 0;
}
void showOnBoard(id<Iterator> iterator) {
while ([iterator hasNext]) {
DisplayCharactor * charactor = [iterator next];
NSLog(@"%@",charactor.charactor);
}
}
优点
1.支持以不同的方式遍历一个聚合的对象
2.简化聚合类
3.实现了类之间的松耦合
缺点
将遍历聚合对象的逻辑抽离出来,导致类增多,逻辑在一定程度变得复杂
iOS用自己的命名规则“枚举器/枚举”改写了迭代器模式。
iOS中的枚举器/枚举有多种
1.NSENumrator
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:5];
NSEnumerator * rator = [array objectEnumerator];
id item ;
while (item = [rator nextObject]) {
}
2.块枚举
[array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
}];
3.快速枚举
forin
4.内部枚举
NSArray 有一个[array makeObjectsPerformSelector:@selector(selector)]; 该方法提供一个给数组中所有元素发送一个消息的功能,但是如果元素如果不能不能响应就会抛出异常。主要适用于不需要进行太多的运行时检查的简单操作