DEV Community

Cover image for CHAIN OF RESPONSIBILITY
salamikola
salamikola

Posted on

CHAIN OF RESPONSIBILITY

I have not written a piece concerning software development in a while, combining writing and coding can be a herculean task. oh dear!!!!

This article is about one of my favourite design patterns “THE CHAIN OF RESPONSIBILITY”.

According to Wikipedia “chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects.”

For us to really get and understand the importance of COR in software development, we need to create a problem that COR will help us solve effortlessly.

if(user != authenticated){
//tell user he/she can’t see this page
}
else{
//show user the page
}

Is that a bad approach? HELL NO!!!! Remember the KISS rule. No price for writing complicated codes, so keep it simple if it simple.

We deployed our client’s web app, and everything is fine and dandy. 4 months down the line, we got a call from our client “Hey, please I need you to work on the secret page, I don’t want users from Nigeria to be able to view that page”. Your response was okay I will get it done.

Boom you edited your secret page code to something like this;

if(user != authenticated){
//tell user he/she can’t see this page
}
if(user.IP == “nigeria”){
//tell user he/she can’t see this page
}
else{
//show user the page
}

Still not bad, I guess. You updated the application and your client pleased with the new change. In less than two weeks your ever-demanding client called again. Please I don’t want free users to be able view the secret page. In your usual manner you ran to your secret page code and altered the code to this;

if(user != authenticated){
//tell user he/she can’t see this page
}
if(userIP == “nigeria”){
//tell user he/she can’t see this page
}
if(user.status == “free”){
//tell user he/she can’t see this page
}
else{
//show user the page
}

At this point, your code is beginning to look like a mess, apart from that you also violated the second rule of the SOLID principle(Open and Close Principle) from the very time you added condition to check if the user’s region is not restricted. You couldn't care less anyway; it is working that is all that matter to you.

Your Oliver Twist client called again this time you are getting fed up, the client does not want to show page to users that have not spent up to a year regardless of their location and status.

BOY OH BOY!!! Let us do it your usual way

if(user != authenticated){
//tell user he/she can’t see this page
}
if(user.IP == “nigeria”){
//tell user he/she can’t see this page
}
if(user.status == “free”){
//tell user he/she can’t see this page
}
if(user.registrationDate < “1 year”){
//tell user he/she can’t see this page
}

else{
//show user the page
}

Now take a look at your code… Are you proud of it? Can you put this up in an interview? I guess not.
Don’t bury your head shame, because in the next article we will solve this same issue using COR which will result to a clean, reusable and extensible code.

Top comments (0)