Builder Design Pattern Example in Java

Often in our applications we need to work with objects that contain lots class fields. To instantiate such objects is very inconvenient using class constructor and is not thread safe using the setters and getters approach. The best way out will be to use Builder design pattern and in this blog post I am going to share with you how to create a User object using the Builder design pattern approach.

Creating User object using Builder Design Pattern

package com.appsdeveloperblog.designpatterns.builder;

/**
 *
 * @author skargopolov
 */
public class User {

    private final String firstName;
    private final String lastName;
    private final String email;
    private final String secondaryEmail;
    private final String homePhone;
    private final String mobilePhone;
    private final String country;
    private final String city;

    private User(Builder builder) {
        this.firstName = builder.firstName;
        this.lastName = builder.lastName;
        this.email = builder.email;
        this.secondaryEmail = builder.secondaryEmail;
        this.homePhone = builder.homePhone;
        this.mobilePhone = builder.mobilePhone;
        this.country = builder.country;
        this.city = builder.city;
    }
    
    @Override
    public String toString()
    {
        return "User [firstName = "+ firstName +", lastName = "+ lastName +", "
                + "email = "+ email +", secondaryEmail = "+ secondaryEmail +", "
                + "homePhone = "+ homePhone +", mobilePhone = "+ mobilePhone +", "
                + "country = "+ country +", city = "+ city +", ]";
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return the secondaryEmail
     */
    public String getSecondaryEmail() {
        return secondaryEmail;
    }

    /**
     * @return the homePhone
     */
    public String getHomePhone() {
        return homePhone;
    }

    /**
     * @return the mobilePhone
     */
    public String getMobilePhone() {
        return mobilePhone;
    }

    /**
     * @return the country
     */
    public String getCountry() {
        return country;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    public static class Builder {

        private String firstName;
        private String lastName;
        private String email;
        private String secondaryEmail;
        private String homePhone;
        private String mobilePhone;
        private String country;
        private String city;

        public Builder(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

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

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

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

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

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

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

        public User build() {
            return new User(this);
        }

    }

}

Instantiating User Object using Builder Design Pattern

package com.appsdeveloperblog.designpatterns.builder;

/**
 *
 * @author skargopolov
 */
public class UserBuilderExample {
 public static void main(String[] agrs)
 {
     User user = new User.Builder("Sergey", "Kargopolov").city("Vancouver").build();
     System.out.println("User object toString() = " + user);
     System.out.println("User first name = " + user.getFirstName());
     System.out.println("City name = " + user.getCity());
 }
}

And this is it!

I hope this blog post was helpful to you. There lots of good books and video courses on design pattern and below, I will share with you a list of resources that will help you learn design patters for Java as well as Swift and Android.

Books on Design Patterns using Java

Books on Design Patterns on iOS Using Swift

Books on Design Patterns on Android 

Video Courses


Leave a Reply

Your email address will not be published. Required fields are marked *