Android Design patterns Java Software Engineering

How and when to use Builder pattern?

Introduction

The Builder Pattern is creational design pattern that encapsulate the construction of a product and allow it to be constructed in steps.

I like to use Builder design pattern when an object has too many parameters. This pattern is really good when you have object with both mandatory and optional parameters. With this pattern you can avoid creating multiple long constructors with a bunch of parameters.

Builder Pattern benefits:

  • encapsulate the way a complex object is constructed
  • allows objects to created in multi-step and varying process

Drawbacks:

  • Constructing object requires more domain knowledge

If you are working with Android you probably have used Builder pattern. For example, when you are creating dialog in Android, then you use Builder pattern:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

builder.setTitle("Quit?")
       .setMessage("Are you sure?")
       .setPositiveButton("Yes",quiteClickListener)
       .setNegativeButton("No", null)
       .create() 
       .show();

Implementation

To illustrate advantages of the Builder pattern, I’m going to use the following example with User class. This class is going to have too many parameters, so we will use Builder pattern.

Step 1

Create User class with parameters that will accept Builder class, then create Builder class that will have methods to manage all parameters and an method return a new user. That method is usually called build or create, but you can call it whatever you want.

public class User {
    
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private String address;
    private String city;
    private String country;

    public User(Builder builder){
    }
    
    public static class Builder {
    
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private String address;
    private String city;
    private String country;
    
    public Builder() {        
    }
    
    public Builder firstName(String firstName){
        this.firstName = firstName;
        return this;
    }
    
    public Builder lastName(String lastName){
        this.lastName = lastName;
        return this;
    }
    
    public Builder email(String email){
        this.email = email;
        return this;
    }
    
    public Builder password(String password){
        this.password = password;
        return this;
    }

    public Builder address(String address){
        this.address = address;
        return this;
    }

    public Builder city(String city){
        this.city = city;
        return this;
    }

   public Builder country(String country){
        this.country = country;
        return this;
    }
    
    public User build(){
        return new User(this);
    }
}    
}

Step 2

Now let’s just instantiate User builder and set values. That’s it. You can see how much easier is now to create an object and set it values. This is especially true if there are too much parameters and some of them are optional.

public class BuilderTest {

    public static void main(String[] args) {
        
        User.Builder userBuilder = new User.Builder();
        
        userBuilder.email(" dangerx@gmail.com")
                .firstName("Zoran")
                .lastName("Pavlovic")
                .password("123456")
                .build();
    }    
}

Conclusion

The Builder Pattern is the great way to construct complex objects. I like to use them when I have a big list of parameters for an object.  If your class is going to have just a few parameters, then you do not need to use Builder Pattern. In that case I would create objects directly or use a static factory method, since that would be a less code and it is not hard to read and write it.

You Might Also Like

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.