Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

Builder. This article describes the "Builder" design pattern and its implementation in the programming language Java with Java 8.

1. The builder pattern

1.1. Definition

The builder pattern provides a build object which is used to construct a complex object called the product. It encapsulates the logic of constructing the different pieces of the product.

1.2. Example

Typically the builder pattern is implemented by an class which has several methods to configure the product. These methods typically return the builder object. This allows to use the builder via a fluent API, e.g, by calling methods directly after each other. Once the product is completely configured a build() method is called to construct the object.

Assume you have a data model like the following.

package com.vogella.model.task;

import java.util.Date;

public class Task {
        private final long id;
        private String summary = "";
        private String description = "";
        private boolean done = false;
        private Date dueDate;

        public Task(long id) {
                this.id = id;
        }

        public Task(long id, String summary, String description, boolean done,
                        Date dueDate) {
                this.id = id;
                this.summary = summary;
                this.description = description;
                this.done = done;
                this.dueDate = dueDate;

        }

        public long getId() {
                return id;
        }

        public String getSummary() {
                return summary;
        }

        public void setSummary(String summary) {
                this.summary = summary;
        }

        public String getDescription() {
                return description;
        }

        public void setDescription(String description) {
                this.description = description;
        }

        public boolean isDone() {
                return done;
        }

        public void setDone(boolean done) {
                this.done = done;
        }

        public Date getDueDate() {
                return new Date(dueDate.getTime());
        }

        public void setDueDate(Date dueDate) {
                this.dueDate = new Date(dueDate.getTime());
        }
}

Before Java 8 your builder would looks like the following.

package com.vogella.model.task;

import java.util.Date;

public class TaskBuilder {
        private final long id;
        private String summary = "";
        private String description = "";
        private boolean done = false;
        private Date dueDate;

        public TaskBuilder(long id, String summary, String description, boolean done,
                        Date dueDate) {
                this.id = id;
                this.summary = summary;
                this.description = description;
                this.done = done;
                this.dueDate = dueDate;
        }


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

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

        public TaskBuilder setDone(boolean done) {
                this.done = done;
                return this;
        }

        public TaskBuilder setDueDate(Date dueDate) {
                this.dueDate = new Date(dueDate.getTime());
                return this;
        }
        public Task build() {
                return new Task(id,summary, description,done, dueDate);
        }
}

And here is how you can use it.

package com.vogella.model.task;

public class MainTest {

        public static void main(String[] args) {
                Task task = new TaskBuilder(5).setDescription("Hello").setSummary("Test").build();
                System.out.println(task);
        }

}

1.3. Evaluation

The builder pattern simplifies the creation of objects. It also simplifies the code as your do not have to call a complex constructor or call several setter methods on the created object.

The builder pattern can be used to create an immutable class.

2. About this website

3. Links and Literature

3.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.