Builder
The main reason to use Builder pattern is to avoid constructors with huge amount of parameters. Some people say we should have a limit for number of parameters and it should be around 3 parameters per constructor. We should keep the number parameters around 3 because if not, it is becoming a piece of code that is difficult to read, test and manage. People, who developed feelings for a code, might say that constructors with many parameters are ugly and not friendly.
The goal when using Builder patter is to create fluent interface. Here is an example of fluent interface:
Example - User builder
Here is an example a build that constructs a user.
Then we can use the builder's fluent interface to create a new object of User
class.
Builder in JavaScript using async / await
When we are writing integration tests, we might need to insert some data, that are expected to be present, into database. We can write a SQL script or put SQLs into code that would executed before each test. But then it becomes difficult to update this script. As we add more features we might need to have multiple SQL scripts to cover various scenarios. Better is to split SQL inserts into methods of builder and let the user, the one who writes the integration tests, to choose what should be inserted.
Since we want to threat the test code as production code, we should follow DRY principle. Then it becomes handy to put data creation into builders that we can reuse in test code. Here is a builder that will help us to insert data into database so we can run our integration tests.
Then we can use the builder in a test as follows.
Last updated
Was this helpful?