迭代器模式定义
- 提供一种方法,按照顺序访问一个容器中对象的各个元素,而又不需要暴露该容器的内部细节(内部表示)
迭代器模式应用场景
- 遍历容器对象
迭代器模式角色划分
4个核心角色
角色一:Iterator(迭代器接口)->协议
作用: 定义了访问遍历元素的方法(标准)
角色二:Concreteiterator(具体迭代器)
作用:遍历具体元素方法实现
角色三:Aggregate(容器接口)->协议
作用:定义了存储元素的接口
角色四:concreteAggregate(具体容器)
作用:实现了存储元素的接口
注意:以后在框架设计或项目开发中,发现有遍历元素的场景,第一时间想到的就是迭代器模式(OC,swift,c++,java,kotlin等)
迭代器原理案例
遍历元素
抽象迭代器
//
// IteratorProtocol.swift
// Dream_20180716_Cursor
//
// Created by Dream on 2018/7/16.
// Copyright © 2018年 Tz. All rights reserved.
//
import Foundation
protocol IteratorProtocol {
//两个方法
//下一个元素(元素类型我们不知道->具体类型->泛型)->ItemType
//associatedtype->声明Swift泛型关键字
associatedtype ItemType
func next() -> ItemType?
//是否有元素
func hasNext() -> Bool
}
具体迭代器
//
// ConcreteIterator.swift
// Dream_20180716_Cursor
//
// Created by Dream on 2018/7/16.
// Copyright © 2018年 Tz. All rights reserved.
//
import Cocoa
//什么是泛型?定义的时候不需要指定类型,使用的时候指定类型
class ConcreteIterator<T>: AnyIterator<T> {
override func next() -> T? {
if self.hasNext() {
let result = self.array[index]
index += 1
return result
}
return nil
}
override func hasNext() -> Bool {
return index != self.array.count
}
}
抽象类型
//
// AnyIterator.swift
// Dream_20180716_Cursor
//
// Created by Dream on 2018/7/16.
// Copyright © 2018年 Tz. All rights reserved.
//
import Cocoa
class AnyIterator<T>: IteratorProtocol {
typealias ItemType = T
//记录当前遍历位置
var index:Int = 0
var array:Array<T>
init(array:Array<T>) {
self.array = array
}
func next() -> T? {
return nil
}
func hasNext() -> Bool {
return false
}
}
抽象容器
//
// AggregrateProtocol.swift
// Dream_20180716_Cursor
//
// Created by Dream on 2018/7/16.
// Copyright © 2018年 Tz. All rights reserved.
//
import Foundation
//容器:保存数据类型->泛型
//强调一次:如果你对泛型不了解,那么赶紧复习,后面数据库框架设计全部都是泛型
protocol AggregrateProtocol {
associatedtype DataType
//添加元素
func add(data:DataType)
//删除元素
func remove(index:Int) -> DataType
//迭代器
//注意:协议使用不允许指定类型->必需是实现类(语法规定)
func iterator() -> AnyIterator<DataType>
}
具体容器
//
// ConcreteAggregrate.swift
// Dream_20180716_Cursor
//
// Created by Dream on 2018/7/16.
// Copyright © 2018年 Tz. All rights reserved.
//
import Cocoa
class ConcreteAggregrate<D>: AggregrateProtocol {
//DataType给他定义一个别名
typealias DataType = D
private var array = Array<D>()
func add(data: D) {
self.array.append(data)
}
func remove(index: Int) -> D {
return self.array.remove(at: index)
}
func iterator() -> AnyIterator<D> {
//初始化(将数组传递到我们的迭代器中,由我们的迭代器来遍历元素)
return ConcreteIterator<D>(array: self.array)
}
}
具体实现
//
// main.swift
// Dream_20180716_Cursor
//
// Created by Dream on 2018/7/16.
// Copyright © 2018年 Tz. All rights reserved.
//
import Foundation
//测试
let a = "世界旁观者"
let b = "大吉大利"
let c = "NSLog"
//自己写的容器框架
let aggregrate = ConcreteAggregrate<String>()
aggregrate.add(data: a)
aggregrate.add(data: b)
aggregrate.add(data: c)
let iterator = aggregrate.iterator()
while iterator.hasNext(){
let result = iterator.next()
//得到这个元素(打印可能为nil,需要指定一个默认值->语法)
print("结果:\(result ?? "")")
}
打印结果
结果:世界旁观者
结果:大吉大利
结果:NSLog
迭代器模式-oc加深版
OrderModel数据模型
//
// OrderModel.h
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface OrderModel : NSObject
@property(nonatomic,strong)NSString *orderId;
@property(nonatomic,strong)NSString *orderName;
@property(nonatomic,strong)NSString *orderPrice;
@property(nonatomic,strong)NSString *orderDetail;
- (instancetype)initWithorderId:(NSString*)orderId orderName:(NSString*)orderName orderPrice:(NSString*)orderPrice orderDetail:(NSString*)orderDetail;
@end
NS_ASSUME_NONNULL_END
//
// OrderModel.m
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import "OrderModel.h"
@implementation OrderModel
- (instancetype)initWithorderId:(NSString*)orderId orderName:(NSString*)orderName orderPrice:(NSString*)orderPrice orderDetail:(NSString*)orderDetail
{
if ((self = [super init])) {
self.orderId=orderId;
self.orderName=orderName;
self.orderPrice=orderPrice;
self.orderDetail=orderDetail;
}
return self;
}
@end
抽象迭代器
//
// IteratorProtocol.h
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "OrderModel.h"
NS_ASSUME_NONNULL_BEGIN
//创造迭代器
@protocol IteratorProtocol <NSObject>
-(OrderModel*)next;
-(BOOL)hasNext;
@end
NS_ASSUME_NONNULL_END
抽象公用参数
//
// OrderIterator.h
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "IteratorProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@interface OrderIterator<T>: NSObject<IteratorProtocol>
{
int Index;
}
@property(nonatomic,strong)T obj;
- (instancetype)initWithObj:(T)obj;
@end
NS_ASSUME_NONNULL_END
//
// OrderIterator.m
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import "OrderIterator.h"
@interface OrderIterator()
@end
@implementation OrderIterator
- (instancetype)initWithObj:(id)obj
{
self = [super init];
if (self) {
self.obj=obj;
Index=0;
}
return self;
}
- (BOOL)hasNext {
return false;
}
- (nonnull OrderModel *)next {
return nil;
}
@end
具体迭代器
//
// TianMaoOrderIterator.h
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "OrderIterator.h"
#import "OrderModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface TianMaoOrderIterator: OrderIterator
@property(nonatomic,strong) NSEnumerator<OrderModel *> *iterator;
- (instancetype)initWIthSet:(NSSet<OrderModel*>*)set;
@end
NS_ASSUME_NONNULL_END
//
// TianMaoOrderIterator.m
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import "TianMaoOrderIterator.h"
@implementation TianMaoOrderIterator
- (instancetype)initWIthSet:(NSSet<OrderModel*>*)set
{
self = [super initWithObj:set];
if (self) {
self.iterator=set.objectEnumerator;
}
return self;
}
- (OrderModel *)next
{
if([self hasNext])
{
OrderModel* result = self.iterator.nextObject;
Index += 1;
return result;
}
return nil;
}
- (BOOL)hasNext
{
NSSet *set=(NSSet<OrderModel*>*)self.obj;
return Index!=set.count;
}
@end
具体容器
//
// TianmaoOrderAggregrate.h
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "OrderModel.h"
#import "TianMaoOrderIterator.h"
NS_ASSUME_NONNULL_BEGIN
@interface TianmaoOrderAggregrate : NSObject
@property(nonatomic,strong)NSMutableSet<OrderModel*>*set;
- (id<IteratorProtocol>)iterator;
@end
NS_ASSUME_NONNULL_END
//
// TianmaoOrderAggregrate.m
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import "TianmaoOrderAggregrate.h"
@implementation TianmaoOrderAggregrate
- (instancetype)init
{
self = [super init];
if (self) {
self.set=[[NSMutableSet alloc]init];
[self.set addObject:[[OrderModel alloc]initWithorderId:@"1" orderName:@"mac pro" orderPrice:@"2222" orderDetail:@"dddd"]];
[self.set addObject:[[OrderModel alloc]initWithorderId:@"2" orderName:@"mac pro" orderPrice:@"2222" orderDetail:@"dddd"]];
[self.set addObject:[[OrderModel alloc]initWithorderId:@"3" orderName:@"mac pro" orderPrice:@"2222" orderDetail:@"dddd"]];
}
return self;
}
- (id<IteratorProtocol>)iterator
{
return [[TianMaoOrderIterator alloc]initWIthSet:self.set];
}
@end
具体容器
//
// JingdongOrderAggregrate.h
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "OrderModel.h"
#import "JingdongOrderIterator.h"
NS_ASSUME_NONNULL_BEGIN
@interface JingdongOrderAggregrate : NSObject
@property(nonatomic,strong)NSMutableArray<OrderModel*>*set;
- (id<IteratorProtocol>)iterator;
@end
NS_ASSUME_NONNULL_END
//
// JingdongOrderAggregrate.m
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import "JingdongOrderAggregrate.h"
@implementation JingdongOrderAggregrate
- (instancetype)init
{
self = [super init];
if (self) {
self.set=[[NSMutableSet alloc]init];
[self.set addObject:[[OrderModel alloc]initWithorderId:@"1" orderName:@"aaa" orderPrice:@"2222" orderDetail:@"dddd"]];
[self.set addObject:[[OrderModel alloc]initWithorderId:@"2" orderName:@"bbb" orderPrice:@"2222" orderDetail:@"dddd"]];
[self.set addObject:[[OrderModel alloc]initWithorderId:@"3" orderName:@"ccc" orderPrice:@"2222" orderDetail:@"dddd"]];
}
return self;
}
- (id<IteratorProtocol>)iterator
{
return [[JingdongOrderIterator alloc]initWIthSet:self.set];
}
@end
具体迭代器
//
// JingdongOrderIterator.h
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "OrderIterator.h"
#import "OrderModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface JingdongOrderIterator : OrderIterator
@property(nonatomic,strong) NSEnumerator<OrderModel *> *iterator;
- (instancetype)initWIthSet:(NSMutableArray<OrderModel*>*)set;
@end
NS_ASSUME_NONNULL_END
//
// JingdongOrderIterator.m
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import "JingdongOrderIterator.h"
@implementation JingdongOrderIterator
- (instancetype)initWIthSet:(NSMutableArray<OrderModel*>*)set
{
self = [super initWithObj:set];
if (self) {
self.iterator=set.objectEnumerator;
}
return self;
}
- (OrderModel *)next
{
if([self hasNext])
{
OrderModel* result = self.iterator.nextObject;
Index += 1;
return result;
}
return nil;
}
- (BOOL)hasNext
{
NSMutableArray *set=(NSMutableArray<OrderModel*>*)self.obj;
return Index!=set.count;
}
@end
实现
//
// main.m
// Andy_20190308_iterator
//
// Created by 王宁 on 2019/3/8.
// Copyright © 2019年 王宁. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "TianmaoOrderAggregrate.h"
#import "JingdongOrderAggregrate.h"
//let aggregrate = TianmaoOrderAggregrate()
//let iterator = aggregrate.iterator()
//while iterator.hasNext(){
// let result = iterator.next()
// //得到这个元素(打印可能为nil,需要指定一个默认值->语法)
// print("orderId:\(String(describing: result?.orderId))")
// print("orderName:\(String(describing: result?.orderName))")
//}
int main(int argc, char * argv[]) {
@autoreleasepool {
//这里可以将容器抽象出来,将容器实现协议来弄
JingdongOrderAggregrate *tianmao=[[JingdongOrderAggregrate alloc]init];
id<IteratorProtocol> iterator=[tianmao iterator];
while (iterator.hasNext) {
OrderModel *ordermodel=iterator.next;
NSLog(@"orderId:%@",ordermodel.orderId);
NSLog(@"orderName:%@",ordermodel.orderName);
}
}
}
网友评论