iOS设计模式 - 迭代器
生活随笔
收集整理的這篇文章主要介紹了
iOS设计模式 - 迭代器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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?
細節
轉載于:https://www.cnblogs.com/YouXianMing/p/4911148.html
總結
以上是生活随笔為你收集整理的iOS设计模式 - 迭代器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Work Tips
- 下一篇: ASP.NET(c#)实现重定向的三种方