• 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

    细节

  • 相关阅读:
    Android应用开发之避免内存泄露
    史上最经典的数据库面试题之二
    某大型银行深化系统之二十一:Log4j执行性能
    ruby支持批量数组的定义
    为VIM提供python代码提示功能
    使用win7登陆远程机器时自动保存密码
    安装Beanstalk
    在linux下安装或者卸载nginx
    python的数据类型
    使Ruby自动定位查找本地路径
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4913584.html
Copyright © 2020-2023  润新知