In this article we will look into builder design pattern in java and try to understand what problem it solves and its practical use case.
Introduction
There is different ways of creational design pattern and provide different mechanism to create object that help in increasing the flexibility and also promote code re-usability.
Factory Method: Provide an interface for creating object in superclass but let the subclasses decide which classes need to instantiate at runtime
Abstract Factor Method: Allows to create related objects without specifying their concrete classes.
Builder: To create object in multiple steps.
Singleton: Create single instance through the application.
Porotype: Prefer cloning techniques instead of creating new object form scratch.
Builder Design pattern in details
Builder design patter is the creational design patter that help you to create the complex object step by step.
This pattern solves the problem associated with object creation of class that have large number of parameters. By providing the ways to build the object in step by step and return final object by calling the build method.
Builder Design Pattern need
Builder design patter solve the problem associated with Factory and Abstract Factory design pattern when the class contain large no of field. These issues are as.
- To many programs’ parameter need to pass from client to the factor class that can be error prone.
- Optional parameter needs to send as null unnecessarily.
- If Object is heavy and its creation logic is complex, then all these complexities need to be accommodated in Factory class that create confusion.
Builder Design pattern creation rule in java
These are the step mention below, that will help to create builder object in step-by-step process and get the final object in last by calling build method.
- Create private constructor, so that no one can call the initiate this class from outside.
- Remove all setter method.
- Create a static nested class as Builder class and copy all the field from outer class to builder class.
- Builder class should contain public constructor with mandatory field and parameter.
- Builder class should have method to set the optional parameter and it should return same builder object.
- In final step, builder class should provide build method that will return the object required by the client.
Builder Example
Let create an example by applying the above rule to create the builder class, As per the naming convention, let take outer class as Student then Inner class would be StudentBuilder
package com.javaocean.designpattern.builder; /** * Outer Class */ public class Student { private String name; // mandatory fields private String city; // Optional Parameter private int age; // Optional Parameter private int mobile; // Optional Parameter private String county; // Optional Parameter /** * Private Constructor * * @param builder */ private Student(StudentBuilder builder) { super(); this.name = builder.name; this.city = builder.city; this.age = builder.age; this.mobile = builder.mobile; this.county = builder.county; } /** * Generate only getter*/ public String getName() { return name; } public String getCity() { return city; } public int getAge() { return age; } public int getMobile() { return mobile; } public String getCounty() { return county; } @Override public String toString() { return "Student [name=" + name + ", city=" + city + ", age=" + age + ", mobile=" + mobile + ", county=" + county+ "]"; } /** * Create Static inner Class **/ public static class StudentBuilder { private String name; private String city; private int age; private int mobile; private String county; /** * Public Constructor with required field only * * @param name */ public StudentBuilder(String name) { this.name = name; } /** * Generate only Setter That return this, ie, Object of Static Inner Class */ public StudentBuilder setName(String name) { this.name = name; return this; } public StudentBuilder setCity(String city) { this.city = city; return this; } public StudentBuilder setAge(int age) { this.age = age; return this; } public StudentBuilder setMobile(int mobile) { this.mobile = mobile; return this; } public StudentBuilder setCounty(String county) { this.county = county; return this; } /** * This is required method Build Method * This method will invocke private constructor of OuterClass * */ public Student build() { return new Student(this); } } }
7 thoughts on “Builder design pattern in java”