How to create Builder design pattern in Java

  • 03 November 2016
  • ADM

 

How to create Builder design pattern in Java - images/logos/oopjava.jpg

 

The builder pattern is an object creation software design pattern. A Builder class builds the final object step by step.

Example

For example will use a simple User class.
package com.admfactory.domain;

import java.util.Date;

public class User {

    public enum Type {
        ADMIN, REGULAR, SUBSCRIBER;
    }

    private String name;
    private String email;
    private String password;
    private Type type;
    private Date dob;

    public static class Builder {

        private String name;
        private String email;
        private String password;
        private Type type;
        private Date dob;

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

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

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

        public Builder type(Type type) {
            this.type = type;
            return this;
        }

        public Builder dob(Date dob) {
            this.dob = dob;
            return this;
        }

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

    }

    public User() {
    }

    private User(Builder builder) {
        this.name = builder.name;
        this.email = builder.email;
        this.password = builder.password;
        this.type = builder.type;
        this.dob = builder.dob;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    @Override
    public String toString() {
        return this.getClass().getName() + " [" + "name='" + name + "'" + ", email='" + email + "'" + ", password='"
                + password + "'" + ", type='" + type + "'" + ", dob='" + dob + "']";
    }
}

Note: The public User constructor was added only to be able to show the difference between Builder design pattern and classic way of creating the objects.

Usage

package com.admfactory.domain;

import java.util.Date;

import com.admfactory.domain.User.Type;

public class UserTest {
    public static void main(String[] args) {
        System.out.println("Create new object using builder design pattern:");
        User admin = new User.Builder().name("Admin").email("admin@admfactory.com").password("aaaaaa")
                .type(User.Type.ADMIN).dob(new Date()).build();
        
        System.out.println(admin.toString());
        System.out.println();
        
        /* Classic way to create the object. */
        System.out.println("Classic way to create the object:");
        User regular = new User();
        regular.setName("Regular");
        regular.setEmail("regular@admfactory.com");
        regular.setPassword("bbbbbbb");
        regular.setType(Type.REGULAR);
        regular.setDob(new Date());
        
        System.out.println(regular.toString());
    }
}

Output

Create new object using builder design pattern:
com.admfactory.domain.User [name='Admin', email='admin@admfactory.com', password='aaaaaa', type='ADMIN', dob='Fri Mar 18 09:55:52 GMT 2016']

Classic way to create the object:
com.admfactory.domain.User [name='Regular', email='regular@admfactory.com', password='bbbbbbb', type='REGULAR', dob='Fri Mar 18 09:55:52 GMT 2016']

Conclusion

This is a simple example of how to create the Builder design pattern and because of this it is possible not to see the advantages of using it, but in a context of big objects with multiple constructors with lots of parameters definitely is an advantage.

 

References