责任链模式是一种对象的行为模式。经过创建一条链来组织请求的处理者,请求将沿着链进行传递,请求发送者无须知道请求在什么时候、何处以及如何被处理,实现了请求发送者与处理者的解耦。html
如Android 中的事件传递,Activity->ViewGroup->View,固然也能够View->ViewGroup-> Activity ,android 中是U形事件传递android
另外OKhttp的interceptors实现,OkHttp发送网络请求的一切核心功能,包括创建链接、发送请求、读取缓存等,都是经过interceptors来实现的。这些interceptors在运行的时候彼此协做,构成了一个责任链interceptor chain
git
Response getResponseWithInterceptorChain() throws IOException { // Build a full stack of interceptors. List<Interceptor> interceptors = new ArrayList<>(); interceptors.addAll(client.interceptors()); interceptors.add(retryAndFollowUpInterceptor); interceptors.add(new BridgeInterceptor(client.cookieJar())); interceptors.add(new CacheInterceptor(client.internalCache())); interceptors.add(new ConnectInterceptor(client)); if (!forWebSocket) { interceptors.addAll(client.networkInterceptors()); } interceptors.add(new CallServerInterceptor(forWebSocket)); Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0, originalRequest, this, eventListener, client.connectTimeoutMillis(), client.readTimeoutMillis(), client.writeTimeoutMillis()); return chain.proceed(originalRequest); } 复制代码
在 OkHttp 中,命令对象就是 Request 对象,处理对象就是每个 Interceptor 对象。每一个 interceptor 对 request 进行一些步骤的处理,而将其他的工做交给下一个 interceptor。注意到,责任链中的处理对象若是能够全权处理命令对象,则不须要交给下一个处理对象。OkHttp 中的 CacheInterceptor 也是具备全权处理的能力。若是请求的结果已经缓存,则不须要再交给 ConnectInterceptor 等进行链接服务器、发送请求的处理,直接返回已缓存的 response 便可。github
例如常见的流程审批(固然在企业级APP中由服务端处理)编程
fun main(args: Array<String>) { // 引用链 val cto = CTO(null) val department = DepartmentLeader(cto) val group = GroupLeader(department) // group.handleEvent(FlowEvent("filepath ", 113)) } data class FlowEvent(val filePath: String, val fileID: Int) interface FlowHandler { val next: FlowHandler? fun handleEvent(event: FlowEvent) } class GroupLeader(override val next: FlowHandler?) : FlowHandler { override fun handleEvent(event: FlowEvent) { when (event.fileID) { // 根据文件ID分配审批权限 处理 111 -> print("GroupLeader收到流程:${event.filePath}") // 不然转发 else -> when (next) { // 转发到下一级 is FlowHandler -> { println("GroupLeader: 此流程被转发了") next.handleEvent(event) } else -> println("GroupLeader: 此流程不能被处理") } } } } class DepartmentLeader(override val next: FlowHandler?) : FlowHandler { override fun handleEvent(event: FlowEvent) { when (event.fileID) { // 根据文件ID分配审批权限 处理 112 -> println("DepartmentLeader收到流程:${event.filePath}") // 不然转发 else -> when (next) { // 转发到下一级 is FlowHandler -> { println("DepartmentLeader: 此流程被转发了") next.handleEvent(event) } else -> println("DepartmentLeader: 此流程不能被处理") } } } } class CTO(override val next: FlowHandler?) : FlowHandler { override fun handleEvent(event: FlowEvent) { when (event.fileID) { // 根据文件ID分配审批权限 113 -> println("CTO收到流程 :${event.filePath},正在处理") else -> println("CTO: 此流程不能被处理") } } } 复制代码
输出:api
GroupLeader: 此流程被转发了
DepartmentLeader: 此流程被转发了
CTO收到流程文件:filepath
复制代码
以上能够参见责任链的工做机理,整个链条的每一个处理环节都有 对其输入参数的校验,只有输入参数在责任链环节的有效接收范围以内,才能处理相关的逻辑缓存
偏函数是数学中的概念,指的是定义域X中可能存在某些值在值域Y中没有相对应的值。bash
一个多元函数传入了部分参数以后的获得的新的函数。 参数的固定通常使用默认参数+具名参数。若是须要固定的参数在中间,虽说能够经过具名参数来解决,可是很尴尬,由于必须使用一大堆具名参数。g服务器
Function参见这里markdown
fun main(args: Array<String>) { val chain = GroupLeader orElse DepartmentLeader orElse CTO chain(FlowEvent("xxxxx",113)) } /** * 责任链模式中各环节对于输入的校验已经处理逻辑的问题。 * * 协变- 泛型对象做为函数的参数,逆变- 泛型做为内部方法的返回。 * * @param check 校验函数 * @param handle 处理函数 */ class ChainFunction<in P1, out R>( private val check: (P1) -> Boolean, private val handle: (P1) -> R) : (P1) -> R { // 进行有效性检验 传递参数p1 override fun invoke(p1: P1): R { if (check(p1)) { // 校验经过执行handle函数 return handle(p1) } else { // 检验不经过抛出异常 throw IllegalArgumentException("事件: ($p1) 没法被处理 ") } } fun isChecked(p1: P1) = check(p1) } // 利用中缀函数造成引用链 infix fun <P1, R> ChainFunction<P1, R>.orElse(function: ChainFunction<P1, R>): ChainFunction<P1, R> { return ChainFunction({ this.isChecked(it) || function.isChecked(it) }) { when { this.isChecked(it) -> this(it) else -> function(it) } } } val GroupLeader = { val event: (FlowEvent) -> Boolean = { it.fileID == 111 } val handler: (FlowEvent) -> Unit = { print("GroupLeader已处理该流程:filePath ${it.filePath}") } // 传递 ChainFunction(event,handler) }() val DepartmentLeader = { val event: (FlowEvent) -> Boolean = { it.fileID == 112 } val handler: (FlowEvent) -> Unit = { print("DepartmentLeader已处理该流程:filePath ${it.filePath}") } // 传递 ChainFunction(event,handler) }() val CTO = { val event: (FlowEvent) -> Boolean = { it.fileID == 113 } val handler: (FlowEvent) -> Unit = { print("CTO已处理该流程:filePath ${it.filePath}") } // 传递 ChainFunction(event,handler) }() 复制代码
CTO已处理该流程:filePath xxxxx
复制代码
若是责任链过长,或者链上的事件传递须要判断处理时间太长的话会影响性能,尤为是递归循环。
参考:
funKTionale 《Kotlin核心编程》