iOS设计模式 - 迭代器
原理图
说明
提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示。
源码
https://github.com/YouXianMing/iOS-Design-Patterns
// // Node.h // IteratorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @interface Node : NSObject /** * 下一个节点 */ @property (nonatomic, strong) Node *nextNode; /** * 节点里面的内容 */ @property (nonatomic, strong) id item; /** * 初始化节点 * * @param item 节点携带的内容 * * @return 节点 */ - (instancetype)initWithItem:(id)item; @end
// // Node.m // IteratorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 YouXianMing. All rights reserved. // #import "Node.h" @implementation Node - (instancetype)initWithItem:(id)item { self = [super init]; if (self) { self.item = item; } return self; } @end
// // LinkedList.h // IteratorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> #import "Node.h" #import "IteratorProtocol.h" #import "LinkedListIterator.h" @interface LinkedList : NSObject /** * 头结点 */ @property (nonatomic, strong, readonly) Node *headNode; /** * 节点的数目 */ @property (nonatomic, assign, readonly) NSInteger numberOfNodes; /** * 添加数据 * * @param item 数据 */ - (void)addItem:(id)item; /** * 创建迭代器对象 * * @return 迭代器对象 */ - (id <IteratorProtocol>)createIterator; @end
// // LinkedList.m // IteratorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 YouXianMing. All rights reserved. // #import "LinkedList.h" @interface LinkedList () /** * 头结点 */ @property (nonatomic, strong, readwrite) Node *headNode; /** * 节点的数量 */ @property (nonatomic, assign, readwrite) NSInteger numberOfNodes; @end @implementation LinkedList - (void)addItem:(id)item { if (self.headNode == nil) { self.headNode = [[Node alloc] initWithItem:item]; } else { [self addItem:item node:self.headNode]; } self.numberOfNodes++; } - (id <IteratorProtocol>)createIterator { return [[LinkedListIterator alloc] initWithLinkedList:self]; } #pragma mark - Private Methods - (void)addItem:(id)item node:(Node *)node { if (node.nextNode == nil) { node.nextNode = [[Node alloc] initWithItem:item]; } else { [self addItem:item node:node.nextNode]; } } @end
// // LinkedListIterator.h // IteratorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> #import "IteratorProtocol.h" @class LinkedList; @interface LinkedListIterator : NSObject <IteratorProtocol> /** * 由链表进行初始化 * * @param linkedList 链表对象 * * @return 迭代器工具 */ - (id)initWithLinkedList:(LinkedList *)linkedList; @end
// // LinkedListIterator.m // IteratorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 YouXianMing. All rights reserved. // #import "LinkedListIterator.h" #import "LinkedList.h" @interface LinkedListIterator () @property (nonatomic, weak) LinkedList *linkedList; @property (nonatomic, weak) Node *currentNode; @end @implementation LinkedListIterator - (id)initWithLinkedList:(LinkedList *)linkedList { if (self = [super init]) { self.linkedList = linkedList; self.currentNode = linkedList.headNode; } return self; } - (id)next { id item = self.currentNode.item; self.currentNode = self.currentNode.nextNode; return item; } - (BOOL)hasNext { if (self.currentNode == nil) { return NO; } else { return YES; } } - (id)item { return self.currentNode.item; } @end
// // IteratorProtocol.h // IteratorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @protocol IteratorProtocol <NSObject> /** * 下一个对象 * * @return 对象 */ - (id)next; /** * 是否存在下一个对象 * * @return 对象 */ - (BOOL)hasNext; /** * 内容 * * @return 返回内容 */ - (id)item; @end
// // ViewController.m // IteratorPattern // // Created by YouXianMing on 15/10/26. // Copyright © 2015年 YouXianMing. All rights reserved. // #import "ViewController.h" #import "LinkedList.h" #import "LinkedListIterator.h" @interface ViewController () @property (nonatomic, strong) LinkedList *linkedList; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建链表结构 self.linkedList = [[LinkedList alloc] init]; // 添加链表元素 [self.linkedList addItem:@"1"]; [self.linkedList addItem:@"2"]; [self.linkedList addItem:@"3"]; [self.linkedList addItem:@"4"]; [self.linkedList addItem:@"5"]; // 创建迭代器 id <IteratorProtocol> iterator = [self.linkedList createIterator]; // 进行元素迭代 while ([iterator hasNext]) { NSLog(@"%@", iterator.item); [iterator next]; } } @end
细节