An Example of “Chain of Responsibility Design Pattern”

Mehmet Aydın Ünlü
2 min readSep 20, 2021

--

Photo by David Rangel on Unsplash

I believe the most important thing in programming is “how you write a code”.

A lot of programmers agree with me, code is not a static thing. If you write a function you should remember it will change one day. So maybe we can say; “it is alive”.

If you want to be a better programmer you should think about tomorrow, not only today. I think you already know this. But how can we code tomorrow? In Object Oriented Programming the answer is Design Patterns.

If you write a mobile game, you always do lots of different tasks in initializing process. And the order of the tasks is important. Sometimes a service should wait data from another service. If you can’t get the correct data from the service, you should stop initializing and give a feedback to user. On the other hand, if you get the correct data you can trigger the next action, and so on.

This picture represents our game launch process. You can see the complexity. The code implementation of this process had worked. But for example; if product owner wants a different feature in this process you have to read all of the code blocks and try to remember how it works. Because no one can remember the whole code. Actually you don’t have to remember. You need to focus on the process itself. But how can we focus on this? In Object Oriented Programming the answer is the same, Design Patterns :)

Picture-2

And this picture represents our game launch process after the refactoring.

Since it is simple and clean, this process can easily be understood. You can even change the order of the tasks and everything still works.

This is an implementation of Chain of Responsibility Pattern.

To learn more: https://refactoring.guru/design-patterns/chain-of-responsibility

Think simple, write clean.

--

--

Mehmet Aydın Ünlü
Mehmet Aydın Ünlü

Written by Mehmet Aydın Ünlü

Software Architect & Lead Game Developer | @maydinunlu

No responses yet