1. 用途: 能够制定多个对象实现<Protocol>, 同一个代理方法,可以在多个对象中同时实现

2.原理: 利用消息转发机制,将方法分发到多个对象中

使用方式:

    self.tableView.delegate = AOProtocolDispatcher(UITableViewDelegate, self, self.delegateSource);

.h

#import <Foundation/Foundation.h>

#define AOProtocolDispatcher(__protocol__, ...)  \
[ProtocolDispatcher dispatcherProtocol:@protocol(__protocol__) \
toImplemertors:[NSArray arrayWithObjects:__VA_ARGS__, nil]] @interface ProtocolDispatcher : NSObject + (id)dispatcherProtocol:(Protocol *)protocol toImplemertors:(NSArray *)implemertors; @end

.m

#import "ProtocolDispatcher.h"
#import <objc/runtime.h> struct objc_method_description MethodDescriptionForSELInProtocol(Protocol *protocol, SEL sel) {
struct objc_method_description description = protocol_getMethodDescription(protocol, sel, YES, YES);
if (description.types) {
return description;
}
description = protocol_getMethodDescription(protocol, sel, NO, YES);
if (description.types) {
return description;
}
return (struct objc_method_description){NULL, NULL};
} BOOL ProtocolContainSel(Protocol *protocol, SEL sel) {
return MethodDescriptionForSELInProtocol(protocol, sel).types ? YES: NO;
} @interface ImplemertorContext : NSObject @property (nonatomic, weak) id implemertor; @end @implementation ImplemertorContext @end @interface ProtocolDispatcher () @property (nonatomic, strong) Protocol *prococol;
@property (nonatomic, strong) NSArray *implemertors; @end @implementation ProtocolDispatcher + (id)dispatcherProtocol:(Protocol *)protocol toImplemertors:(NSArray *)implemertors {
return [[ProtocolDispatcher alloc] initWithProtocol:protocol toImplemertors:implemertors];
} - (instancetype)initWithProtocol:(Protocol *)protocol toImplemertors:(NSArray *)implemertors {
if (self = [super init]) {
self.prococol = protocol;
NSMutableArray *implemertorContexts = [NSMutableArray arrayWithCapacity:implemertors.count];
[implemertors enumerateObjectsUsingBlock:^(id implemertor, NSUInteger idx, BOOL * _Nonnull stop) {
ImplemertorContext *implemertorContext = [ImplemertorContext new];
implemertorContext.implemertor = implemertor;
[implemertorContexts addObject:implemertorContext];
objc_setAssociatedObject(implemertor, _cmd, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}];
self.implemertors = implemertorContexts;
}
return self;
} - (BOOL)respondsToSelector:(SEL)aSelector {
if (!ProtocolContainSel(self.prococol, aSelector)) {
return [super respondsToSelector:aSelector];
} for (ImplemertorContext *implemertorContext in self.implemertors) {
if ([implemertorContext.implemertor respondsToSelector:aSelector]) {
return YES;
}
}
return NO;
} - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
if (!ProtocolContainSel(self.prococol, aSelector)) {
return [super methodSignatureForSelector:aSelector];
} struct objc_method_description methodDescription = MethodDescriptionForSELInProtocol(self.prococol, aSelector);
return [NSMethodSignature signatureWithObjCTypes:methodDescription.types];
} - (void)forwardInvocation:(NSInvocation *)anInvocation {
SEL aSelector = anInvocation.selector;
if (!ProtocolContainSel(self.prococol, aSelector)) {
[super forwardInvocation:anInvocation];
return;
} for (ImplemertorContext *implemertorContext in self.implemertors) {
if ([implemertorContext.implemertor respondsToSelector:aSelector]) {
[anInvocation invokeWithTarget:implemertorContext.implemertor];
}
}
} @end

Protocol协议分发器的更多相关文章

  1. iOS 协议分发

    Github:AOMultiproxier.HJProtocolDispatcher 协议实现分发器,能够轻易实现将协议事件分发给多个实现者. 一.AOMultiproxier.h #define A ...

  2. 开源负载均衡通讯分发器(LB dispatcher) - G5

    from:http://bbs.csdn.net/topics/390753043 1.开发背景今天和系统运维的老大聊天,谈到一直在用的F5,行里对其评价为价格过高.功能复杂难懂,反正印象不是很好,使 ...

  3. 实现 Redis 协议解析器

    本文是 <用 Golang 实现一个 Redis>系列文章第二篇,本文将分别介绍Redis 通信协议 以及 协议解析器 的实现,若您对协议有所了解可以直接阅读协议解析器部分. Redis ...

  4. SIP (Session Initiation Protocol) 协议

    Session Initiation Protocol 介绍 SIP是VoIP技术最常使用的协议,它是一种应用程序层协议,可与其他应用程序层协议配合使用,以控制Internet上的多媒体通信会话. V ...

  5. jQuery源码分析系列(33) : AJAX中的前置过滤器和请求分发器

    jQuery1.5以后,AJAX模块提供了三个新的方法用于管理.扩展AJAX请求,分别是: 1.前置过滤器 jQuery. ajaxPrefilter 2.请求分发器 jQuery. ajaxTran ...

  6. SpringMVC核心分发器DispatcherServlet分析[附带源码分析]

    目录 前言 DispatcherServlet初始化过程 DispatcherServlet处理请求过程 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不 ...

  7. Objective-C( protocol协议)

    protocol 协议 protocol:用来声明方法 1.协议的定义 @protocol 协议名称 <NSObject> // 方法声明列表.... @end 2.如何遵守协议 1> ...

  8. ISO 基础之 (十三) protocol 协议

    一 简绍 protocol,简单来说就是一系列不属于任何类的方法列表,其中声明的方法可以被任何类实现.这种模式一般称为代理(delegation)模式.通过Protocol定义各种行为,在不同的场景采 ...

  9. 【转】iOS开发-Protocol协议及委托代理(Delegate)传值

    原文网址:http://www.cnblogs.com/GarveyCalvin/p/4210828.html 前言:因为Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来 ...

随机推荐

  1. js 解决函数加载的问题

    var queue = function(funcs, scope) {         (function next() {               if(funcs.length > 0 ...

  2. aspnet core in docker

    1 创建一个文件夹(app), 将项目发布后的文件放入该文件夹中 并且创建Dockerfile文件 2 打开Dockerfile文件,编辑一下内容 #基于 `microsoft/dotnet:-cor ...

  3. deque(双向队列)基本用法

    deque(双向队列)基本用法 阅读体验:https://zybuluo.com/Junlier/note/1297030 简单介绍 就是可以两头插元素,两头删元素的数据结构 那么具体的STL操作(只 ...

  4. Java反射----数组操作

    1,获取数组字段 在Person类中定义了一个一维数组字段:int[] a1 = new int[]{1,2,3}; 如何通过反射技术来操作该字段? 补充:Java操作数组主要用的是Array类. @ ...

  5. Go语言_方法和接口

    方法和接口 本节课包含了方法和接口,可以用这种构造来定义对象及其行为. Go 作者组编写,Go-zh 小组翻译. https://tour.go-zh.org/methods/1 方法 Go 没有类. ...

  6. C# 字符串Trim进阶

    private void button1_Click(object sender, EventArgs e) {//去掉字符串头尾指定字符 string MyInfo= "--中华人民共和国 ...

  7. JS 逻辑非!简单总结

    !""                  true!"aaa"          false""==false          true ...

  8. RESET - 把一个运行时参数值恢复为缺省值

    SYNOPSIS RESET name RESET ALL DESCRIPTION 描述 RESET 将运行时参数恢复为缺省值. RESET 是下面语句的一个变种 SET parameter TO D ...

  9. React(3) --react绑定属性

    react绑定属性 /* react绑定属性注意: class要换成className for要换成 htmlFor style: <div style={{"color": ...

  10. python正则表达式 re (二)sub

    背景: re.sub是re模块重要的组成部分,并且功能也非常强大,主要功能实现正则的替换. re.sub定义: sub(pattern, repl, string, count=0, flags=0) ...