Software Design Patterns
The Builder Design Pattern
What it is and how to implement it in JavaScript
The builder pattern is a creational design pattern that’s used when you need to construct complex objects that can take many optional and required parameters to create a single object. Using this pattern we can separate the construction of complex objects from their representation so that we can use the same construction process over and over to create different representations.
Examples of Complex Objects
Let's take creating a User object as an example. Now, a User can have many optional fields such as name, age, date of birth, address, email, permissions, etc… which makes it a complex object to create for different users because not every user will have all of this information associated with them.
Or another example is that you’re ordering a new custom PC. You’ll need to specify how much RAM you want, how much storage, the type of storage, what graphics card it should have, what kind of cooling system, etc… These specifications would be different for everyone that orders a new custom PC which makes this a complex object to build as well.
As stated earlier, the builder pattern will allow us to separate the construction of these…