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设计模式 - 迭代器的更多相关文章

  1. iOS书摘之Objective-C编程之道 iOS设计模式解析

    来自<Objective-C编程之道iOS设计模式解析>一书的摘要总结 一.Prototype 原型模式 定义:使用原型实例指定创建对象的种类,并通过复制这个原型创建新的对象.(<设 ...

  2. iOS设计模式 - (1)概述

    近期可自由安排的时间比較多, iOS应用方面, 没什么好点子, 就先放下, 不写了.花点时间学学设计模式. 之后将会写一系列博文, 记录设计模式学习过程. 当然, 由于我自己是搞iOS的, 所以之后设 ...

  3. 19. 星际争霸之php设计模式--迭代器模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  4. IOS设计模式之一(MVC模式,单例模式)

    iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...

  5. iOS 设计模式之工厂模式

    iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...

  6. iOS设计模式之生成器

    iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...

  7. IOS设计模式之三:MVC模式

    IOS设计模式之三:MVC模式   模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...

  8. iOS设计模式 - 享元

    iOS设计模式 - 享元 原理图 说明 享元模式使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件:它适合用于只是因重复而导致使用无法令人接受的大量内存的大量物件.通常物件中的部分 ...

  9. iOS设计模式 - 责任链

    iOS设计模式 - 责任链 原理图 说明 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链 ...

随机推荐

  1. Visual Studio使用阿里云Code Git服务器的常见问题

    使用Github的服务器太慢,阿里的https://code.aliyun.com的国内服务器还是很快的.但是使用阿里的Git服务器总是有些地方出问题,现记录下常见的问题: 1.如提示源码已在TFS管 ...

  2. 使用gitlab, jenkins搭建CI(持续集成)系统(1) -- 准备环境

    1. 环境设计 搭建一个从开发到测试知道发布上线可以自动换完成的CI系统.这个系统中包含4个环境. 开发(dev)环境: 码农使用. 测试(test)环境: 测试人员使用. 预发布(prepublis ...

  3. FSM有限状态机

    1.什么是有限状态机 有限状态机(Finite State Machine),简称FSM,它由一组有限个状态.输入和根据输入及现有状态转换为下一个状态的转换函数组成,当然,通常每个状态机都必须有一个初 ...

  4. [CQOI 2018]社交网络

    Description 题库链接 求 \(n\) 个点以 \(1\) 为根的有向生成树个数. \(1\leq n\leq 250\) Solution 我终于会 \(\texttt{Matrix-Tr ...

  5. sql 数据库数据 批量判断修改

    A表B表相关联  更新B表中的VisitWeek字段值 CCD_PartnerVisit 此为B表 Dell_FiscalWeek  此为A表 UPDATE CCD_PartnerVisit SET ...

  6. mybatis在oracle中的分页扩展

    applicationContext.xml <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlS ...

  7. 线程8--GCD常见用法

    1.延迟执行 /***********************延迟第一种方法**************************/ /* 第一种方法, 调用NSObject的方法 [self perf ...

  8. 关于PLSQL启动用时较长的问题解决

    问题: 打开登陆界面缓慢. 解决: 1.删除控制面板中的打印机 2.将打印机改为手动并停止启动状态 .

  9. ibatis中$和#的区别

    比如当变量name的类型是Stirng时, $name$ 打印出来的是 张三 #name# 打印出来的是 ‘张三’ $ 的作用实际上是字符串拼接 #用于变量替换 那什么时候用$,什么时候 用 # (1 ...

  10. 性能是.NET Core的一个关键特性

    关键要点1).NET Core是跨平台的,可运行在Windows.Linux.Mac OS X和更多平台上:与.NET相比,发布周期要短得多.大多数.NET Core都是通过NuGet软件包交付的,可 ...