DEV Community

Cover image for Laravel Pipelines By Example | Chain of Responsibility Design Pattern
Mohammad Fazel
Mohammad Fazel

Posted on

Laravel Pipelines By Example | Chain of Responsibility Design Pattern

Have you ever worked with pipelines in Laravel?!

Probably you have heard it while having an interview?

It has lots of use cases and one of its usage is middleware implementation in its kernel.

I'm going to write about pipelines and its design pattern.

What is pipeline

First of all Pipelines in Laravel are implemented based on chain of responsibility design pattern.

In chain of responsibility which is a behavioral design pattern we pass data to receivers and this receivers implement a specific interface that has a handler method.

Chain of responsibility design pattern

Handler methods may deal with the passed argument and pass it to the next handler.

read more about the design pattern here :

https://www.geeksforgeeks.org/chain-responsibility-design-pattern/

Twitter Bot

Imagine we want to implement a twitter bot that gets some tweets around the trends and filter the words that we don't wanna show finally send as a new tweet from our bot.

About the filtering part, if it was me, I would implement it with pipelines.

Why?!

Currently I know exactly what words should be filtered but what about the future?! What if I want to add more filters, I will have to modify the source code and add more switch, if else statements, etc.

I want my code to be robust and maintainable keeping SOLID principles.

Example

So I'm gonna use pipeline like this (picture) :

Filter Tweet Pipelines

First I will have my initial filters and also give the ability to myself or other developers to add more filters without even seeing the code.

See the picture and lets go deeper and check the main interface and pipelines.

Filterable Contract

I have defined a contract for all of my filters, every new filter and existing filters must implement this interface and implement the filter method

As you can see in the previous picture we gave our method name "filter" passed to via()

Nothing more let's go for filters

Remove bad words filter

This is one of the filters we have implemented as you can see everything is clear filter method gets an argument called $content which is the text we wanna filter and when we modify the content and remove bad words it calls the next receiver from pipelines filters we defined at first.

Conclusion

That's it all.
This is the Pipeline (chain of responsibility) design pattern we have in Laravel.

As you can see there are lots of advantages using this design pattern having SOLID standards.

But sometimes it's better to keep things simple when our program is not that big and you are the only developer.

Because When using chain of responsibility design pattern we will instantiate lots of classes and program may get complex and debugging maybe difficult going through various classes.

However the benefits it gives when developing highly scalable applications is so lovely and also you enable other developers to contribute easily...

Twitter

Latest comments (1)

Collapse
 
faniuta profile image
faniuta • Edited

Thank you for your fluent and practical post. An example of this design pattern is in Laravel Middleware, right? Is there another example in Core Laravel with this design pattern?