xiazunyang / chain-responsibility

责任链模式的学习代码。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

责任链模式

fun main() {

    val request = Request("baidu.com", mapOf("request header 1" to "1", "request header 2" to "2"))

    val response = Chain.Builder(request, ::getResponse)
        .addInterceptor(RetryInterceptor())
        .addInterceptor(HeadersInterceptor())
        //.addInterceptor(LoggerInterceptor())
        .start()

    println("finish. $response")
}

fun getResponse(request: Request): Response {
    if (Random(System.currentTimeMillis()).nextBoolean()) {
        throw IOException()
    }
    return Response(request, "response body.", mapOf("response1" to "10", "response2" to "11"))
}

About

责任链模式的学习代码。


Languages

Language:Kotlin 100.0%