iOS设计模式 - 访问者

iOS设计模式 - 访问者

 

原理图

 

说明

表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作。

1.Visitor 抽象访问者角色,为该对象结构中具体元素角色声明一个访问操作接口。该操作接口的名字和参数标识了发送访问请求给具体访问者的具体元素角色,这样访问者就可以通过该元素角色的特定接口直接访问它。
2.ConcreteVisitor.具体访问者角色,实现Visitor声明的接口。
3.Element 定义一个接受访问操作(accept()),它以一个访问者(Visitor)作为参数。
4.ConcreteElement 具体元素,实现了抽象元素(Element)所定义的接受操作接口。
5.ObjectStructure 结构对象角色,这是使用访问者模式必备的角色。它具备以下特性:能枚举它的元素;可以提供一个高层接口以允许访问者访问它的元素;如有需要,可以设计成一个复合对象或者一个聚集(如一个列表或无序集合)。

 

源码

https://github.com/YouXianMing/iOS-Design-Patterns

//
//  ElementProtocol.h
//  VisitorPattern
//
//  Created by YouXianMing on 15/10/27.
//  Copyright © 2015年 ZiPeiYi. All rights reserved.
//

#import <Foundation/Foundation.h>
@protocol VisitorProtocol;

@protocol ElementProtocol <NSObject>

/**
 *  接收访问者
 *
 *  @param visitor 访问者对象
 */
- (void)accept:(id <VisitorProtocol>)visitor;

/**
 *  元素公共的操作
 */
- (void)operation;

@end
//
//  VisitorProtocol.h
//  VisitorPattern
//
//  Created by YouXianMing on 15/10/27.
//  Copyright © 2015年 ZiPeiYi. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ElementProtocol.h"

@protocol VisitorProtocol <NSObject>

- (void)visitElement:(id <ElementProtocol>)element;

@end
//
//  ElementCollection.h
//  VisitorPattern
//
//  Created by YouXianMing on 15/10/27.
//  Copyright © 2015年 ZiPeiYi. All rights reserved.
//

#import <Foundation/Foundation.h>
@protocol ElementProtocol;

@interface ElementCollection : NSObject

/**
 *  添加元素
 *
 *  @param element 元素
 *  @param key     元素的键值
 */
- (void)addElement:(id <ElementProtocol>)element withKey:(NSString *)key;

/**
 *  获取所有元素的键值
 *
 *  @return 所有元素的键值
 */
- (NSArray *)allKeys;

/**
 *  根据元素键值获取元素
 *
 *  @param key 元素的键值
 *
 *  @return 元素
 */
- (id <ElementProtocol>)elementWithKey:(NSString *)key;

@end
//
//  ElementCollection.m
//  VisitorPattern
//
//  Created by YouXianMing on 15/10/27.
//  Copyright © 2015年 ZiPeiYi. All rights reserved.
//

#import "ElementCollection.h"
#import "ElementProtocol.h"

@interface ElementCollection ()

@property (nonatomic, strong) NSMutableDictionary  *elementsDictionary;

@end

@implementation ElementCollection

- (instancetype)init {
    
    self = [super init];
    
    if (self) {
    
        self.elementsDictionary = [NSMutableDictionary dictionary];
    }
    
    return self;
}

- (void)addElement:(id <ElementProtocol>)element withKey:(NSString *)key {

    NSParameterAssert(element);
    NSParameterAssert(key);
    
    [self.elementsDictionary setObject:element forKey:key];
}

- (NSArray *)allKeys {

    return self.elementsDictionary.allKeys;
}

- (id <ElementProtocol>)elementWithKey:(NSString *)key {

    NSParameterAssert(key);
    
    return [self.elementsDictionary objectForKey:key];
}

@end

 

细节

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值