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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/419389.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android程序杀死自己的进程和其他程序进程方法

1.获取程序进程ID&#xff1b; int pidandroid.os.Process.myPid(); android.os.Process..killProcess(pid); 2.杀死其他程序进程&#xff1b; ActivityManager manager(ActivityManager)getSystemService(ACTIVITY_SERVICE); manager.killBackgroundProcesses("packa…

maven依赖关系中Scope的作用

Dependency Scope 在POM 4中&#xff0c;<dependency>中还引入了<scope>&#xff0c;它主要管理依赖的部署。目前<scope>可以使用5个值&#xff1a; * compile&#xff0c;缺省值&#xff0c;适用于所有阶段&#xff0c;会随着项目一起发布。 * provided&…

如何运行ruby代码

第一种&#xff0c;ruby -e 在命令行中运行下面命令&#xff0c;-e的意思是&#xff0c;把后面的字符串当作脚本执行 ruby -e "print hello" 使用irb交互控制台 在命令行输入irb hello worldxingooxingoo-Lenovo:~/workspace/RubyTest$ irb irb(main):001:0> p &q…

使用ViewPager制作Android引导界面

1.涉及Android知识点&#xff1a; ViewPager组件、Handler机制、SharedPreferences。 2.开发实践&#xff1a; a.布局文件设计。 第一个引导界面one.xml&#xff0c;另外两个布局文件类似。 <?xml version"1.0" encoding"utf-8"?> <LinearLay…

6、控件样式模板和使用

WPF控件模板 潜移默化学会WPF(样式篇)---改造CheckBox&#xff0c;全新metro风格 WPF CheckBox 自定义样式 继续聊WPF控件——自定义CheckBox控件外观 用WPF自定义CheckBox的样式 [wpf教程-自定义样式的checkbox开关控件 http://www_suchso.com/projecteactual/wpf-jiaocheng-c…

Android 蓝牙4.0在实际开发中的运用

1.蓝牙搜索. 首先是获取BluetoothAdapter对象&#xff1a; final BluetoothManager bluetoothManager (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter bluetoothAdapter bluetoothManager.getAdapter(); 当blueto…

Mysql递归查询,无限级上下级菜单

mysql递归查询&#xff0c;mysql中从子类ID查询所有父类&#xff08;做无限分类经常用到&#xff09; 由于mysql 不支持类似 oracle with ...connect的 递归查询语法 之前一直以为类似的查询要么用存储过程要么只能用程序写递归查询. 现在发现原来一条sql语句也是可以搞定的 先…

“睡服”面试官系列第二篇之promise(建议收藏学习)

目录 1promise的定义 2基本用法 3. Promise.prototype.then() 4. Promise.prototype.catch() 5. Promise.all() 6. Promise.race() 7. Promise.resolve() 8. Promise.reject() 9. 两个有用的附加方法 10总结 1promise的定义 Promise 是异步编程的一种解决方案&#xf…

Android M 新的运行时权限开发者需要知道的一切

android M 的名字官方刚发布不久&#xff0c;最终正式版即将来临&#xff01; android在不断发展&#xff0c;最近的更新 M 非常不同&#xff0c;一些主要的变化例如运行时权限将有颠覆性影响。惊讶的是android社区鲜有谈论这事儿&#xff0c;尽管这事很重要或许在不远的将来会…

查看IIS连接数

如果要想知道确切的当前网站IIS连接数的话&#xff0c;最有效的方法是通过windows自带的系统监视器来查看。 一、运行-->输入“perfmon.msc”. 二、在“系统监视器”图表区域里点击右键&#xff0c;然后点“添加计数器”. 三、在“添加计数器”窗口&#xff0c;“性能对象”…

SpringMVC关于json、xml自动转换的原理研究[附带源码分析]

目录 前言现象源码分析实例讲解关于配置总结参考资料 前言 SpringMVC是目前主流的Web MVC框架之一。 如果有同学对它不熟悉&#xff0c;那么请参考它的入门blog&#xff1a;http://www.cnblogs.com/fangjian0423/p/springMVC-introduction.html 现象 本文使用的demo基于maven…

“睡服”面试官系列第三篇之变量的结构赋值(建议收藏学习)

目录 变量的解构赋值 1. 数组的解构赋值 2. 对象的解构赋值 3. 字符串的解构赋值 4. 数值和布尔值的解构赋值 5. 函数参数的解构赋值 6. 圆括号问题 7. 用途 变量的解构赋值 1. 数组的解构赋值 基本用法 ES6 允许按照一定模式&#xff0c;从数组和对象中提取值&#…

【宋红康程序思想学习日记3】杨辉三角

class Shuzu3 { public static void main(String[] args) {    int[][] yanghuinew int[10][];   //初始化二维数组   for(int i0;i<yanghui.length;i){     yanghui[i]new int[i1]; }   for(int i0;i<yanghui.length;i){     for(int j0;j<yanghui…

Android为网络请求自定义加载动画

android自带的加载动画都不怎么好看&#xff0c;在这里介绍一种自定义加载动画的方法 原始图片&#xff1a; 编写动画progressbar.xml, <?xml version"1.0" encoding"utf-8"?> <animated-rotate android:drawable"drawable/publicloading&…

mybatis在xml文件中处理大于号小于号的方法

第一种方法&#xff1a; 用了转义字符把>和<替换掉&#xff0c;然后就没有问题了。 SELECT * FROM test WHERE 1 1 AND start_date < CURRENT_DATE AND end_date > CURRENT_DATE 附&#xff1a;XML转义字符 < …

Android之Notification初识

1.Notification创建 首先&#xff0c;介绍一下&#xff0c;创建一个通知所需要用到的类和方法NotificationManager类 NotificationManager类是用来管理系统的所有通知的类&#xff0c;该类的对象必须通过Context类的getSystemService()方法获取。完整代码&#xff1a; Notifica…

MyBatis参数传入集合之foreach动态sql

foreach的主要用在构建in条件中&#xff0c;它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item&#xff0c;index&#xff0c;collection&#xff0c;open&#xff0c;separator&#xff0c;close。item表示集合中每一个元素进行迭代时的别名&#xff0c;index指…