Exploring the Chain Builder Pattern for Better Code Organization: Seeking Feedback from Developers
Tags: designpatterns, php, architecture
Published: 2023-03-26 16:09:21

Have you ever found yourself needing help managing the order of execution in your code while trying to keep the individual functions independent of what will follow? Perhaps you have a complex algorithm that requires multiple steps, or you need to perform a sequence of operations on an object, but you're unsure where to start. If so, you're not alone.

Fortunately, there is a design pattern that can help: the chain builder pattern. In this article, we'll explore this pattern in depth and explain how it can be used to manage the order of execution in your code. I`d like to think that I thought of that by myself when struggling with code optimization, but I'm pretty sure that's not the case. This is my variation of the chain of responsibility behavioral pattern, where I all of the building components are in a single class.

At its core, the chain builder pattern is a way to organize a series of operations into a sequence, with the order of execution defined by the programmer. This pattern is especially useful in situations where you need to perform a series of operations on an object, but the order in which those operations are performed may vary depending on the context.

The key to the chain builder pattern is the use of a variable to set the order of execution. This variable acts as a "switch" that determines which operation will be performed next. By setting this variable to different values, you can control the order in which the operations are performed.

To illustrate the chain builder pattern, let's consider a simple example. Imagine you have an object that represents a car, and you need to perform a series of operations on that object to get it ready for a race. The operations might include installing a new engine, adding racing tires, and adjusting the suspension.

Using the chain builder pattern, you would define each of these operations as a separate function or method, and then use a variable to control the order in which they are performed. 
Note that my example will be in PHP, but this pattern and the whole code can easily be translated to most of the OOP programming languages like Typescript for example.

How I approach the problem:

* Define the underlying interface for all “Builders” - this is what I call the chain builder classes that contain the business logic

![Chain builder interface definition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4n6515675ai0ujm4o1l.png)

All builders will implement the facade “build” method which will run the whole sequence of operations.

* Implement the base builder functionality and make it extendable throughout the sub-classes - 

The “return next” method is the tool to iterate over the order of functions we will define in the next step. We are checking whether there is a next function in the order after the one we are calling the returnNext from and call it, if no, then simply return the accumulated data.
The “parseMethodName” tool is a way to extract only the caller method name from the full name, containing the class name and namespace as well.

![Base chain builder definition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c96t4970biwuayvarctz.png)

* Create individual builders - set up the individual methods that carry the logic, as well as assign the variable order of execution. In the following example, we will be building the parameters to pass to a placeholder class to create a car. The focus will be on the building process of the parameters.

The data array is a way to build up data  throughout all stages of the chain
The params array is the configuration or external parameters, based on which we can make decisions in the builders
Aside from these inputs, the only other thing we need to take care of is to pass the name of the current caller method, so we would extract our current and next builder


![Concrete chain builder definition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/afvhceixopjbep1frld3.png)

After we have defined our control structures all we have left is to call for them and wait


![Client code to create object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkruz5hunvo6wr8rp0sk.png)

This is a structure I started working with relatively soon, so even if I thought of some components to this pattern by myself, this is nothing between some different patterns. I still lack the base for comparison to say whether this will be any good, viable, bad, or actually useful. 
I would like to open a discussion about this, if you have any thoughts I would really appreciate any feedback. And if you can help me optimize it, let's go for it!