迭代器模式是提供一种方法访问一个容器中对象的各个元素,而不暴露对象内部细节的设计模式。
看一下苹果提供的迭代器以及使用:
NSArray *array = @[@"123",@"234",@"345"];
NSEnumerator *enumerator =array.objectEnumerator;
id object = nil;
while((object = enumerator.nextObject)){
NSLog(@"object = %@",object);
}
数组的底层是链表,那么我们就用链表实现一下类似效果,首先模拟实现一个数组。
1.创建一个节点类
@interface Node : NSObject
/**前一个节点*/
@property(nonatomic,strong)Node *previous;
/**后一个节点*/
@property(nonatomic,strong)Node *next;
/**节点的数据*/
@property(nonatomic,strong)id data;
@end
2.创建链表类
@interface LinkList : NSObject
/**头节点*/
@property(nonatomic,strong)Node *first;
/**尾节点*/
@property(nonatomic,strong)Node *last;
/**链表长度*/
@property(nonatomic,assign)NSUInteger size;
-(void)addObject:(id)obj;
-(void)insertObject:(id)obj anIndex:(NSUInteger)index;
-(void)removeObject:(id)obj;
-(void)removeObjectAtIndex:(NSUInteger)index;
@end
3.写一下实现:
#pragma mark -添加元素
-(void)addObject:(id)obj{
if(!obj){
return;
}
_size++;
Node *node =[Node new];
if(!_first){
_first = node;
_last = node;
node.previous = nil;
node.next = nil;
node.data = obj;
return;
}
node.previous = _last;
_last.next = node;
_last = node;
node.data = obj;
node.next = nil;
}
#pragma mark -插入元素
-(void)insertObject:(id)obj anIndex:(NSUInteger)index {
_size++;
Node *node =[Node new];
if(!_first){
_first = node;
_last = node;
node.previous = nil;
node.next = nil;
node.data = obj;
return;
}
node = _first;
Node *insertNode =[Node new];
for(NSUInteger i = 0;i <= _size;i++){
if(index == i){
Node *previousNode = node.previous;
insertNode.previous = previousNode;
insertNode.next = node;
insertNode.data = obj;
previousNode.next = insertNode;
node.previous = insertNode;
return;
}
if(node.next == nil){
insertNode.previous = node;
insertNode.next = nil;
insertNode.data = obj;
_last = insertNode;
node.next = insertNode;
return;
}
node = node.next;
}
}
#pragma mark -删除元素
-(void)removeObject:(id)obj{
Node *node = _first;
for(NSUInteger i = 0;i < _size;i++){
if([node.data isEqual:obj]){
[self removeArrayNode:node];
return;
}
node = node.next;
}
}
#pragma mark -通过索引删除元素
-(void)removeObjectAtIndex:(NSUInteger)index{
Node *node = _first;
for(NSUInteger i = 0;i < _size;i++){
if(index == i){
[self removeArrayNode:node];
return;
}
node = node.next;
}
}
#pragma mark -删除节点
-(void)removeArrayNode:(Node *)node{
_size--;
if(_size == 0){
_first = _last = nil;
}else if(node.previous == nil){
_first = node.next;
}else if(node.next == nil){
_last = _last.previous;
}
Node *previousNode = node.previous;
Node *nextNode = node.next;
previousNode.next = nextNode;
nextNode.previous = previousNode;
node.data = nil;
}
到这里链表的类就已经写好了,在viewController里调用一下:
LinkList *linkList =[[LinkList alloc]init];
[linkList addObject:@"123"];
[linkList insertObject:@"234" anIndex:1];
[linkList insertObject:@"345" anIndex:1];
[linkList removeObjectAtIndex:1];
[linkList removeObject:@"234"];
是不是和数组很相似?
接下来我们就要写一个迭代器了
创建一个协议
#import "Node.h"
#import
@protocol LinkProtocol
-(Node *)nextNode;
@end
再创建一个迭代器的类去实现协议
#import "LinkProtocol.h"
#import "LinkList.h"
#import
@interface LinkIterator : NSObject
+(instancetype)linkWithLinkList:(LinkList *)linkList;
-(Node *)nextNode;
@end
#import "LinkIterator.h"
@interface LinkIterator()
/**链表*/
@property(nonatomic,strong)LinkList *linkList;
/**当前节点*/
@property(nonatomic,strong)Node *currentNode;
@end
@implementation LinkIterator
+(instancetype)linkWithLinkList:(LinkList *)linkList{
LinkIterator *iterator =[[LinkIterator alloc]init];
iterator.linkList = linkList;
iterator.currentNode =[Node new];
iterator.currentNode.next = linkList.first;
return iterator;
}
-(Node *)nextNode{
self.currentNode = self.currentNode.next;
return self.currentNode;
}
@end
去viewController里调用一下:
LinkList *linkList =[[LinkList alloc]init];
[linkList addObject:@"123"];
[linkList insertObject:@"234" anIndex:1];
[linkList insertObject:@"345" anIndex:1];
[self iterator:linkList];
- (void)iterator:(LinkList*)linkList{
LinkIterator*iterator = [LinkIteratorlinkWithLinkList:linkList];
Node*node =nil;
while((node = [iteratornextNode])) {
NSLog(@"data = %@",node.data);
}
}
log如下:
2017-12-18 14:08:59.119874+0800 LinkList[4318:159757] data = 123
2017-12-18 14:08:59.120040+0800 LinkList[4318:159757] data = 345
2017-12-18 14:08:59.120136+0800 LinkList[4318:159757] data = 234
搞定!
如果issue,欢迎指正!