日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

iOS设计模式 - 迭代器

發布時間:2023/12/10 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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设计模式 - 迭代器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。