How to implement chain of responsibility pattern using kotlin coroutines.

kotlin has supports both object oriented programming and functional programming paradigms.

In this post we will look at how to make use of functional programming aspect of kotlin to implement Chain of Responsibility pattern.

What is chain of Responsibility ?

Client can emmit different events and there are different handlers in the system which can handle only one type of event respectively.

The basic idea is , there are different type of events, that flow through a chain of handlers as shown in the above figure. If handler-1 is able to handle the event, it gets consumed by it, but if handler-1 is not able to handle the event, it passes the event to handler-2 which is next in the chain and this continues through the chain till the event gets handled by one of the handlers in the chain and if no suitable handler found, event simply gets discarded.

If you would like to see , how its implemented in Java, please take a look at this post linked below

The key takeaways :

1. Define an interface with abstract method that allows for an option to set next element in chain

2. Also interface should have abstract method to identify whether, the current handler can handle the event or not

3. All the participating handlers in the system implement the above interface

4. Then create concrete handlers and create a chain by calling setNext() on the previous handler object.

Interface

Handler-1

Handler-2

Chain

Well now that you know, how to implement a chain of responsibility, lets see how we can make kotlin coroutines based chain.

Checkout the previous post to understand suspending functions in kotlin.

Now lets go into the details. Every handler is a generator and that generator accepts another handler as its input.

Again the example that we are showing here is , we are reading lines from access-log.txt and feeding it to our chain.

firstHandler (): handles lines starting with character “2”

secondHandler (): handles lines starting with character “7”

thirdHandler (): handles lines starting with character “8”

In this case Generator accepts a String and produces a String. Here we are assuming that our events are type String.

CanHandle() is a function that accepts a lambda, that determines if a particular handler is able to handle a particular event.

Now all that is remaining is creating a chain, line no 55 to 57 do exactly that.

If you have followed previous post , you already know that, a generator that is only created is really of no use. Its lazy :-)

Thats why on line 69, we start all the generators , which causes all of them to be suspended at yield() statements respectively for each.

Now all that is left is , read each line in file and feed it to our pipeline.

There is nothing wrong in following the same way as it was implemented in Java, this is just another take using coroutines in Kotlin. Its wonderful to explore such possibilities and its fun.

Thats it for the post. I hope you really like it and if you do, please don’t forget to clap for it. You can follow me on twitter Smital Desai.

Read the previous posts here at https://medium.com/@desaismital

Thank you so much for reading. Please don’t forget to let me know what do you think in the comments.