iOS设计模式 - 访问者
2015-10-28 21:26
423 查看
iOS设计模式 - 访问者
原理图
说明
表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作。
1.Visitor 抽象访问者角色,为该对象结构中具体元素角色声明一个访问操作接口。该操作接口的名字和参数标识了发送访问请求给具体访问者的具体元素角色,这样访问者就可以通过该元素角色的特定接口直接访问它。
2.ConcreteVisitor.具体访问者角色,实现Visitor声明的接口。
3.Element 定义一个接受访问操作(accept()),它以一个访问者(Visitor)作为参数。
4.ConcreteElement 具体元素,实现了抽象元素(Element)所定义的接受操作接口。
5.ObjectStructure 结构对象角色,这是使用访问者模式必备的角色。它具备以下特性:能枚举它的元素;可以提供一个高层接口以允许访问者访问它的元素;如有需要,可以设计成一个复合对象或者一个聚集(如一个列表或无序集合)。
源码
https://github.com/YouXianMing/iOS-Design-Patterns
细节
原理图
说明
表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作。
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
细节
相关文章推荐
- #在蓝懿学习iOS的日子#Day6
- 蓝懿iOS 技术内容和心得 10。28技术内容交流和心得分享
- iOS数据持久化的几种方式
- IOS开发笔记19-OC特有语法
- 零基础ios开发(五 深入理解继承—植物大战僵尸 )
- iOS开发 关于iBeacon的一些记录
- IOS开发笔记18-继承
- IOS开发笔记17-封装
- 【10/28】 iOS开发成长之路,【tableView】
- iOS 7大手势
- iOSCoreAnimation动画系列教程(二):CABasicAnimation
- ios开发系列之页面跳转
- 类似于IOS弹出底部对话框
- iOS三方—SDWebImage的使用
- 【iOS开发】transform属性的使用
- iOS 设置button的image偏移量
- iOS7时iPAD旋转的键盘需要特殊处理
- iOS集合 学习笔记
- afnetworking_downloadFile_example
- IOS 加载地图