builder pattern image

Builder Pattern

In Object-Oriented Programming (OOP) we spend most of our time creating objects or instances of classes. We usually prefer constructors when we create these objects. However, increasing the number of fields leads us to think differently because of the exponential and unnecessary complexity of the constructors. Builder pattern, on the other hand, overcomes this issue by using a builder to create an object in a step by step manner.

Creating objects is a commonly occurring task and we should find a reusable solution about creating them.

And when you apply a reusable solution for that kind of commonly occurring task we call it a “design pattern“.

Design patterns are very well known in software engineering, and I will not get into the details, however, I want to mention about “Builder Pattern” which is one of the “Creational Patterns”.

Builder pattern allows you to construct a complex object in a step by step process.

Normally, you’d have to write different constructors for every single combination of your mandatory fields of your class.

On the other hand, by using builder pattern you will set your parameters one by one in a meaningful way.

The catch is builder pattern comes with its burden of code duplication though. However, you will gain much more elegant and readable code. To be able to achieve that we have to follow the fluent interfaces. You can find more detail about Fluent Interface by Martin Fowler to gain more knowledge about fluent interfaces later on.

Let’s see the builder pattern in action when you want to make a pizza

The most noticeable part is the style of the building the pizza object. The intention behind the building code is easily understandable.

Pizza pizza = new Pizza.Builder("medium")

Fluency in this code is not only about the chaining of the calls, but also it smoothly allows you to set the properties of the object.

and finally the output is:

This is a medium sized pizza with tomato souce.
It contains;
Cooked Sausage

The basics of the builder pattern

Common ımplementatıon detaıls
  1. The methods of builder class always return itself.
  2. Make the constructors private so it cannot be instantiated by any other means.
  3. Make the fields finals so that your object becomes immutable after it’s being created.
  4. Do not try to apply this pattern if you do not need it. For instance, consider builder pattern when you have 5 or more fields in your constructors.
  5. There are some plugins for common IDE’s to save you from the boilerplate code writings.
  1. Allows method chaining and fluency in your code.
  2. Cleaner and more readable code.
  3. Allows you to construct immutable objects by using mutable fields of builder.
  4. You can force users to call some methods to build and actual object.
  1. Code duplication. Since you need to carry out all of the fields from the original class.

Real world examples

Android Dialog Builder is a great example for builder pattern.

AlertDialog.Builder builder = new AlertDialog.Builder(context);
        .setPositiveButton(positive, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                if (action != null) {

if (negative != null) {
    builder.setNegativeButton(negative, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            if (action != null) {

AlertDialog alertDialog = builder.create();;

Thank you.