Chain as a Service (CaaS)
Design patterns are very important in daily work of a software developer. At least I think so. I assume that many of you know chain of responsibility design pattern.
If you don’t know this design pattern I advise you to read one of those articles before you read further this article:
or you can find article or book about this design pattern on your own.
So now everyone knows chain of responsibility (CoR) design pattern so we can back to main aim of this article. As we know CoR contains series of handlers which are doing their job on incoming requests. After handler’s job is done request is passed to its successor. That scenario can be illustrated with an image of a chain:
So let go back to article’s title. Chain as a Service (CaaS). I’m pretty sure that every developer needed at least once to create in career a service. I’ve a question to you: Omitting service purpose do you consider to create a service with CoR design pattern? If not I’ll show you something. Let’s take our chain from previous image and let’s connect first chain link with last one. We’ll recive something like that:
Isn’t that simple? With that image in back of my head I’ve created some example project on github. It’s written in .Net Core 2.0. Of course you can download it and play with it on your own.
I’ve implemented 3 handlers:
- which returns “Hello world! to the console
- which returns current date and time to the console
- which freezes application for one second
I’ve created a ServiceFactory class which implements IHandlerFactory interface and produces my simple service. My service consist of:
and output from application looks like:
Hello world! Current date and time: 16.11.2017 17:18:12 Hello world! Current date and time: 16.11.2017 17:18:14 Hello world! Current date and time: 16.11.2017 17:18:16
I hope this example will encourage you to more frequent use chain of responsibility design pattern. I would be honored if you will build a service in a way I presented.
I hope you enjoy this article. Your feedback will be extremely valuable to me.