This content originally appeared on Level Up Coding - Medium and was authored by Alex Shamrai
Builder explained — Design patterns in Java
I’m going to start a series of articles explaining design patterns on Java code examples. It might be useful for anyone who wants to dive into those common OOP design techniques. The best way to master anything is through practice.

Builder
Builder is a creational design pattern, which provides an alternative way to construct complex objects. This pattern should be used when we want to build different immutable objects using the same object building process.
Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create multiple different representations.”
Now, let’s assume, we need to create Employee object, which has the following 5 attributes i.e. firstName, secondName, yearOfBirth, employeeId, registrationAddress.
Now what if only firstName ,secondName, employeeId are mandatory and the rest of the fields are optional? We need more constructors:
This problem is called the telescoping constructors problem. Just imagine how many constructors will be required for more attributes.
The Builder pattern can be described in a class, which has a single creation method and several methods to configure the resulting object. Builder methods often support chaining for example, ObjectBuilder()->attrubute1(1)->attribute2(2)->build()).
Fields validations are omitted for short (if the field value is invalid IllegalArgumentException should be thrown). Keep in mind, that the above created object does not have any setter method, so its state can not be changed once it has been built. This provides the desired immutability.
The usage of the pattern:
Lombok @Builder annotation
Project Lombok’s @Builder is an easy way for using the pattern without writing boilerplate code. Here is an example:
And a usage example in the code:
Conclusion
Builder pattern pros
- Design flexibility and much more readable code.
- Parameters to the constructor are reduced and are provided in highly readable chained method calls. This way there is no need to pass in null for optional parameters to the constructor while creating the instance of a class.
- The instance is always instantiated in a complete state.
- We can build immutable objects without much complex logic in the object-building process.
Builder pattern cons
- The pattern requires a lot of code
Use the Builder pattern when
- The algorithm for creating a complex object should be independent of the parts that make up the object and how they’re assembled
- The construction process must allow different representations for the object that’s constructed
Builder usage in Java core libraries:
To see more details about given code examples, please visit my design-patterns GitHub repo.
Builder explained. Design patterns in Java was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Alex Shamrai

Alex Shamrai | Sciencx (2022-05-03T11:40:13+00:00) Builder explained. Design patterns in Java. Retrieved from https://www.scien.cx/2022/05/03/builder-explained-design-patterns-in-java/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.