Design Pattern: Builder Pattern

Hi! In this article, we will understand what a builder pattern is and how you can apply the builder pattern in our code. Let's get started!

Builder Pattern?

The builder pattern is one of the creational design patterns. It allows you to create a different object based on your need. Let's just look at the code and see why we need a builder pattern.

Code

public class Task {

    private Long id;
    private String title;

    public Task() {
    }

    public Task(Long id, String title) {
        this.id = id;
        this.title = title;
    }
}

Problem

Suppose that your clients ask you to do XYZ and you need to add more fields.

private Long id;
private String title;
private String a;
private String b;
private String c;
and so on ...

Given the situation, you could also add multiple constructors for each of the fields to meet the criteria of the requirements.

Solution

One easy solution is Builder Pattern! I will just show you the code and explain later.

public class Task {

    private Long id;
    private String title;
    private String a;
    private String b;
    private String c;

    public Task() {
    }

    public Task(Long id, String title, String a, String b, String c) {
        this.id = id;
        this.title = title;
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public static TaskBuilder builder() {
        return new TaskBuilder();
    }

    public static class TaskBuilder {
            private Long id;
            private String title;
            private String a;
            private String b;
            private String c;       

            TaskBuilder() {
        }

        public TaskBuilder id(Long id) {
            this.id = id;
            return this;
        }

        public TaskBuilder title(String title) {
            this.title = title;
            return this;
        }

        public TaskBuilder a(String a){
            this.a = a;
            return this;

        }

        and so on ...


        public Task build() {
            return new Task(this.id, this.title, this.a, this.b, this.c);
        }
    }

}

Explanation

There are many ways you could implement a builder pattern. With the example above, i chose to implement using static class and function. Another way is to create an interface and create a separate class for Task like TaskBuilder and do the same thing.

With this builder pattern, you will be able to create an instance with a different combination of constructors without creating constructors for each class!

public static void main(String[] args) {
        Task task = new Task(1L, "example","a","b", "c");
        Task task2 = new Task(2L, "a", "b"); // need to create a separate constructor

        Task builderTask = Task.builder().id(1L).title("new title").a("a here").build();
    }

Lombok

Project Lombok is a java library that automatically plugs into your editor and builds tools with annotations. Instead of making extra code, you could simply add an annotation to apply a builder pattern.

import lombok.Builder;

@Builder
public class Task {
}

Conclusion

In this post, i briefly went through the builder pattern and how you can apply it in your code. Feel free to comment! Thank you for reading.