How to Implement Builder Design Pattern in Java

Last updated on Oct 05,2023 2K Views

How to Implement Builder Design Pattern in Java

edureka.co

Creating Complex Objects in Java is a very important aspect of Java. In this article, we will understand the Builder Design Pattern in Java in the following order:

 

What is a Builder Pattern in Java?

Builder Pattern in Java is another way to construct complex objects. It is used to construct a complex object. Step by step construction of the object takes place and the object is constructed at the final step.

A builder design pattern is similar to a factory design pattern. The only difference is that the builder pattern provides you more control over the object creation as compared to the factory method.

Related Article: Top 30 Java Pattern Programs

Where Are Builder Design Patterns used?

Consider you are making a pie. There are different types of pie and each has different ingredients. But some ingredients may be similar. To write a code for this we will require many constructors. This could cause a problem.  There are two major problems.

  1. Too many constructors to handle.

  2. There may be an error because there may be similar parameters.

This problem is solved by using a builder pattern.

Consider the Code:

class Pie {
    private final double sugar;
    private final double butter;
    private final int eggs;
    private final int vanilla;
    private final double flour;
    private final double bakingpowder;
    private final double milk;
    private final int cherry;
    public static class Builder {
        private double sugar;
        private double butter;
        private int eggs;
        private int vanilla;
        private double flour;
        private double bakingpowder;
        private double milk;
        private int cherry;
        public Builder sugar(double cup){this.sugar = cup; return this; }
        public Builder butter(double cup){this.butter = cup; return this; }
        public Builder eggs(int number){this.eggs = number; return this; }
        public Builder vanila(int spoon){this.vanilla = spoon; return this; }
        public Builder flour(double cup){this.flour = cup; return this; }
        public Builder bakingpowder(double spoon){this.sugar = spoon; return this; }
        public Builder milk(double cup){this.milk = cup; return this; }
        public Builder cherry(int number){this.cherry = number; return this; }
        public Pie build() {
            return new Pie(this);
        }
    }
    private Pie(Builder builder) {
        this.sugar = builder.sugar;
        this.butter = builder.butter;
        this.eggs = builder.eggs;
        this.vanila = builder.vanila;
        this.flour = builder.flour;
        this.bakingpowder = builder.bakingpowder;
        this.milk = builder.milk;
        this.cherry = builder.cherry;
    }
    @Override
    public String toString() {
        return "pie{" + "sugar=" + sugar + ", butter=" + butter + ", eggs=" + eggs + ", vanila=" + vanila + ", flour=" + flour + ", bakingpowder=" + bakingpowder + ", milk=" + milk + ", cherry=" + cherry + '}';
    }
 
}
public class BuilderPatternInterface{
    public static void main(String args[]) {
        pie whitePie = new pie.Builder().sugar(1).butter(0.5).  eggs(2).vanila(2).flour(1.5). bakingpowder(0.75).milk(0.5).build();
        System.out.println(whitePie);
    }
}

OUTPUT:

 

Explanation of the code:

In the above code, the demo of the builder pattern is shown. We have a class pie. In this class, we have a builder class that has builder methods for setting properties. In the next part, there is a constructor pie that builds the code.

This reduces the need for constructors. The only drawback is that the lines of code increases.

 

Advantages of Builder Design Pattern

 

Disadvantages of Builder Design Pattern

 

With this, we come to an end of this Builder Design Pattern in Java article. I hope you got an idea of what Design Patterns are and how do they work.

Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Builder Design Pattern in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 1st June,2024

1st June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial