Chain of responsibility

The chain of responsibility pattern is an alternative version of the if { ... } else if { ... } else if { ... } else block. According to this pattern, we have a source of request objects and a chain of processing objects. A processing object that can't handle a request passes it to another handler. The following diagram demonstrates this:

The preceding diagram contains the Request interface that is implemented by the DELETERequest, PUTRequest, POSTRequest, and GETRequest classes. The RequestHandler interface is implemented by the DELETERequestHandler, POSTRequestHandler, PUTRequestHandler, and DELETERequestHandler classes. A class that implements the RequestHandler interface handles an operation that is encapsulated by an instance of the Request type.

The Request interface looks like this:

interface Request

The classes that implement this interface look as follows:

class GETRequest: Request

class POSTRequest: Request

class PUTRequest: Request

class DELETERequest: Request

The RequestHandler interface contains the nextHandler property and the handle method:

interface RequestHandler {
val nextHandler: RequestHandler?
fun handle(request: Request)
}

The GETRequestHandler class overrides the nextHandler property and initializes it with an instance of the POSTRequestHandler class, as follows:

class GETRequestHandler: RequestHandler {

override val nextHandler = POSTRequestHandler()

override fun handle(request: Request) {
if (request is GETRequest) {
println("Handle GET request...")
} else {
nextHandler.handle(request)
}
}
}

The handle method processes an incoming request or passes it to the next POSTRequestHandler handler:

class POSTRequestHandler: RequestHandler {

override val nextHandler = PUTRequestHandler()

override fun handle(request: Request) {
if (request is POSTRequest) {
println("Handle POST request...")
} else {
nextHandler.handle(request)
}
}
}

The POSTRequestHandler class overrides the nextHandler property and initializes it with an instance of the PUTRequestHandler class:

class PUTRequestHandler: RequestHandler {
override val nextHandler = DELETERequestHandler()

override fun handle(request: Request) {
if (request is PUTRequest) {
println("Handle PUT request...")
} else {
nextHandler.handle(request)
}
}
}

The PUTRequestHandler class overrides the nextHandler property with an instance of the DELETERequestHandler class. DELETERequestHandler is the last link in the chain:

class DELETERequestHandler: RequestHandler {
override val nextHandler: RequestHandler? = null

override fun handle(request: Request) {
if (request is DELETERequest) {
println("Handle DELETE request...")
}
}
}

We can use this code as follows:

fun main(args: Array<String>) {
GETRequestHandler().apply {
handle(GETRequest())
handle(DELETERequest())
}
}

The output is as follows:

Handle GET request...
Handle DELETE request...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.47.166