- Design patterns are solutions to software design problems.
- Design patterns classified into three types.
- Creational, Structural and behavioral design patterns.
- Creational patterns helps us to create objects in a manner suitable to the given situation.
- Builder design pattern is one of the creational design pattern in java.
- Builder design pattern helps us to create complex class object.
- Builder design pattern helps us to separate the construction process of a complex object from its representation so that same object construction process can be created in different representations.
- Means it will separate complex construction into two parts initialization of class instance and return class instance.
- When a class having more number of fields and constructor of that class take care of assigning initial values.
- And when we want to create object of the class we need to pass all parameters and should be in same order which constructor is accepting.
- Builder design pattern helps us to create same class object by passing required number of fields by using separate builder class object.
- Builder design pattern is useful when object creation is very complex.
Advantages of builder design pattern:
- Builder design pattern simplifies complex object creation.
- Builder design pattern provides separation between instance creation and representation
- Re usability
Program #1: Builder design pattern in java with example program
Employee:
- package com.designpatternsinjava.builderdesignpattern;
- public class Employee {
- String name;
- String company;
- int id;
- String passport_number;
- String temp_address;
- String perm_address;
- int salary;
- Employee(String name,String company,int id,String passport_number,String
- temp_address,String perm_address,int salary){
- this.name=name;
- this.company=company;
- this.id=id;
- this.passport_number=passport_number;
- this.temp_address=temp_address;
- this.perm_address=perm_address;
- this.salary=salary;
- }
- public String toString(){
- return "Name="+name+" \n Company="+company+"\n id="+id+"\n
- passport_number="+passport_number+"" +"\n temp_address="+temp_address+"\n
- perm_address"+perm_address+"\n
- salary="+salary;
- }
- }
EmployeeBuilder
- package com.designpatternsinjava.builderdesignpattern;
- public class EmployeeBuilder {
- String name;
- String company;
- int id;
- String passport_number;
- String temp_address;
- String perm_address;
- int salary;
- public EmployeeBuilder setName(String name) {
- this.name = name;
- return this;
- }
- public EmployeeBuilder setCompany(String company) {
- this.company = company;
- return this;
- }
- public EmployeeBuilder setId(int id) {
- this.id = id;
- return this;
- }
- public EmployeeBuilder setPassport_number(String passport_number) {
- this.passport_number = passport_number;
- return this;
- }
- public EmployeeBuilder setTemp_address(String temp_address) {
- this.temp_address = temp_address;
- return this;
- }
- public EmployeeBuilder setPerm_address(String perm_address) {
- this.perm_address = perm_address;
- return this;
- }
- public EmployeeBuilder setSalary(int salary) {
- this.salary = salary;
- return this;
- }
- public Employee build(){
- return new Employee(name, company, id, passport_number, temp_address,
- perm_address, salary);
- }
- }
BuilderDemo
- To create object of employee class we need to provide all the fields values to the constructor.
- So it is somewhat difficult to pass all values all times.
- Employee builder class taken all variables of Employee and in setter methods accepts a value and returns EmployeBuilder object.
- And EmployeBuilder class has build method which will assign all values to employee and returns Employee object.
- Employee empobj= new EmployeeBuilder().setName("Saidesh").setId(1234).build();
- Now we create object of Employee class by creating EmployeBuilder class object and caling calling setter methods whatever we have.
- Is is very easy to set the values because we have corresponding setter method name is same as variable name.
- After setting the values we need to call build method so that it will return employee object with values.
No comments