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:@""];
[self.linkedList addItem:@""];
[self.linkedList addItem:@""];
[self.linkedList addItem:@""];
[self.linkedList addItem:@""]; // 创建迭代器
id <IteratorProtocol> iterator = [self.linkedList createIterator]; // 进行元素迭代
while ([iterator hasNext]) { NSLog(@"%@", iterator.item);
[iterator next];
}
} @end
细节

iOS设计模式 - 迭代器的更多相关文章
- iOS书摘之Objective-C编程之道 iOS设计模式解析
来自<Objective-C编程之道iOS设计模式解析>一书的摘要总结 一.Prototype 原型模式 定义:使用原型实例指定创建对象的种类,并通过复制这个原型创建新的对象.(<设 ...
- iOS设计模式 - (1)概述
近期可自由安排的时间比較多, iOS应用方面, 没什么好点子, 就先放下, 不写了.花点时间学学设计模式. 之后将会写一系列博文, 记录设计模式学习过程. 当然, 由于我自己是搞iOS的, 所以之后设 ...
- 19. 星际争霸之php设计模式--迭代器模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- IOS设计模式之一(MVC模式,单例模式)
iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...
- iOS 设计模式之工厂模式
iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...
- iOS设计模式之生成器
iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...
- IOS设计模式之三:MVC模式
IOS设计模式之三:MVC模式 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...
- iOS设计模式 - 享元
iOS设计模式 - 享元 原理图 说明 享元模式使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件:它适合用于只是因重复而导致使用无法令人接受的大量内存的大量物件.通常物件中的部分 ...
- iOS设计模式 - 责任链
iOS设计模式 - 责任链 原理图 说明 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链 ...
随机推荐
- Hadoop+Hive 操作mongodb数据
Hadoop+Hive 操作mongodb数据 1.版本概述 hadoop-2.7.3.hive-2.2 下载响应的jar包:http://mvnrepository.com/,直接搜索想要的jar包 ...
- Spring Boot 不使用默认的 parent,改用自己的项目的 parent
在初学spring boot时,官方示例中,都是让我们继承一个spring的 spring-boot-starter-parent 这个parent: <parent> <group ...
- chrome和IE下的滚动条样式修改
火狐下的滚动条样式无法去修改,但chorme下的则可以任意修改,惊喜的是IE竟然是最早实现这一功能的浏览器,IE5都能有效果. chorme下的滚动条样式修改: <!DOCTYPE html&g ...
- jsp页面查询的数据导出到excel
java导入导出excel操作(jxl) jxl.jar 包下载地址:http://www.andykhan.com/jexcelapi/真实下载地址:http://www.andykhan.com/ ...
- https在电子邮件安全解决方案
电子邮件安全解决方案 电子邮件已经成为现代人最重要和最不可缺少的个人生活和工作的通信工具之一,特别是企业应用.但是,您也许不知道,所有电子邮件系统都是明文传输,也就是说:您的每一个重要邮件都是在以“明 ...
- APP消息推送机制的实现(PUSH)
出于好奇,想了解一下消息推送机制,在网上搜索到了几篇文章,感觉还不错,粘贴下来,等真正用到的时候再仔细研究 以下两篇是关于ios的 1.http://blog.csdn.net/xyxjn/artic ...
- HTTP协议以及HTTP请求中8种请求方法
HTTP协议以及HTTP请求中8种请求方法 什么是协议? 协议,是指通信的双方,在通信流程或内容格式上,共同遵守的标准. 什么是http协议? http协议,是互联网中最常见的网络通信标准. http ...
- ASPxGridView控件的基本属性
1.//ASPxGridView前台获取行号 <ClientSideEvents RowClick="function(s, e) { s.GetRowKey(e.visibleInd ...
- shellExcute 与shellExcuteex 的一些东西
最近在做外部调用exe并传给exe相应参数 方法有两种.. 1,用program.start() 2,shell() 3,调用API 以下内容来自http://www.pinvoke.net/defa ...
- Linq in条件查询
Linq 实现sql中的not in和in条件查询 T-SQL的IN: Select ProductID, ProductName, CategoryID From dbo.Products Wh ...