迭代模式

2018-06-23  本文已影响22人  Colin_狂奔的蚂蚁

迭代模式

迭代器提供了一种顺序访问集合对象中元素的方法,而无需暴漏结构的底层表示和细节。遍历集合中元素的职能从集合本身转移到迭代器对象。迭代器定义了一个用于访问集合元素并记录当前元素的接口。不同的迭代器可以执行不同的策略。
何时使用迭代器模式?
@:需要访问组合对象的内容,而又不暴漏其内部表示。
@:需要通过多种方式遍历组合对象。
@:需要提供一个统一的接口,用来遍历各种类型的组合对象。

链表图.png

测试代码:

#import "ViewController.h"
#import "LinkedList.h"
#import "LinkedIterator.h"

@interface ViewController ()
@property (nonatomic, strong) LinkedList *list;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 1. 数组集合
    NSArray *data = @[@"1",@"2",@"3",@"4"];

    // 2.创建迭代器
    NSEnumerator *enumerator = [data objectEnumerator];

    // 3.访问每一个元素
    id obj = nil;
    while (obj = [enumerator nextObject]) {
        NSLog(@"---%@",obj);
    }

    // 1.创建集合类
    self.list = [[LinkedList alloc] init];
    [self.list addData:@"A"];
    [self.list addData:@"B"];
    [self.list addData:@"C"];

    // 2.迭代器的创建
    LinkedIterator *linkedIterator = [LinkedIterator linkedObjectIterator:self.list];
    
    // 3.访问每一个元素
    Node *node = nil;
    while (node = [linkedIterator nextObject]) {
        NSLog(@"---%@",node.data);
    }
}
@end

2018-06-23 20:32:02.636647+0800 LinkedListDemo[46853:2480975] ---1
2018-06-23 20:32:02.636777+0800 LinkedListDemo[46853:2480975] ---2
2018-06-23 20:32:02.636890+0800 LinkedListDemo[46853:2480975] ---3
2018-06-23 20:32:02.636982+0800 LinkedListDemo[46853:2480975] ---4
2018-06-23 20:32:02.637114+0800 LinkedListDemo[46853:2480975] ---A
2018-06-23 20:32:02.637215+0800 LinkedListDemo[46853:2480975] ---B
2018-06-23 20:32:02.637314+0800 LinkedListDemo[46853:2480975] ---C

其它文件:

===================Node文件===================
#import <Foundation/Foundation.h>

@interface Node : NSObject
// 指向下一个节点(链表中的指针)
@property (nonatomic, strong) Node *nextNode;

// 节点里面的data
@property (nonatomic, strong) id data;

// 节点数据
+ (instancetype)nodeData:(id)data;
@end


#import "Node.h"

@implementation Node
+ (instancetype)nodeData:(id)data {
    Node *node = [[self alloc] init];
    node.data = data;
    
    return node;
}
@end

===================LinkedList文件===================
#import <Foundation/Foundation.h>
#import "Node.h"

@interface LinkedList : NSObject

@property (nonatomic, strong) Node *headNode; /**< 头结点 */

- (void)addData:(id)data; /**< 节点上的数据 */

@end


#import "LinkedList.h"

@implementation LinkedList

- (instancetype)init
{
    self = [super init];
    if (self) {
        //  1.  初始化时创建一个头节点
        self.headNode = [Node nodeData:nil];
    }
    return self;
}

- (void)addData:(id)data {
    [self addData:data node:self.headNode];
}

// 2. 有头结点了,就插入到下一个节点
- (void)addData:(id)data node:(Node *)node {
    if (node.nextNode == nil) {
        node.nextNode = [Node nodeData:data];
    } else {
        // 当下一个节点也有值了,进行递归调用
        [self addData:data node:node.nextNode];
    }
}
@end
===================LinkedIterator文件===================
#import <Foundation/Foundation.h>
#import "LinkedList.h"

@interface LinkedIterator : NSObject
@property (nonatomic, strong) Node *currentNode;
- (id)nextObject;
// 迭代器的实现方法
+ (instancetype)linkedObjectIterator:(LinkedList *)linkedList;
@end


#import "LinkedIterator.h"

@interface LinkedIterator ()

@end

@implementation LinkedIterator

+ (instancetype)linkedObjectIterator:(LinkedList *)linkedList {
    // 1.初始化迭代器
    LinkedIterator *linkedIterator = [[LinkedIterator alloc] init];
    
    // 2. 保存链表数据
    //linkedIterator.linkedList = linkedList;
    linkedIterator.currentNode = linkedList.headNode;
    
    return linkedIterator;
}

- (id)nextObject {
    self.currentNode = self.currentNode.nextNode;
    return self.currentNode;
}
@end
上一篇下一篇

猜你喜欢

热点阅读