Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Builder design pattern in java with example program

  • 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:
  1. package com.designpatternsinjava.builderdesignpattern;

  2. public class Employee {

  3. String name;
  4. String company;
  5. int id;
  6. String passport_number;
  7. String temp_address;
  8. String perm_address;
  9. int salary;

  10. Employee(String name,String company,int id,String passport_number,String
  11. temp_address,String perm_address,int salary){
  12. this.name=name;
  13. this.company=company;
  14. this.id=id;
  15. this.passport_number=passport_number;
  16. this.temp_address=temp_address;
  17. this.perm_address=perm_address;
  18. this.salary=salary;
  19. }

  20. public String toString(){
  21.  return "Name="+name+" \n Company="+company+"\n id="+id+"\n
  22.    passport_number="+passport_number+"" +"\n temp_address="+temp_address+"\n
  23.    perm_address"+perm_address+"\n
  24.    salary="+salary;

  25. }
  26. }

EmployeeBuilder

  1. package com.designpatternsinjava.builderdesignpattern;

  2. public class EmployeeBuilder {


  3. String name;
  4. String company;
  5. int id;
  6. String passport_number;
  7. String temp_address;
  8. String perm_address;
  9. int salary;
  10. public EmployeeBuilder setName(String name) {
  11. this.name = name;
  12. return this;
  13. }

  14. public EmployeeBuilder setCompany(String company) {
  15. this.company = company;
  16. return this;
  17. }

  18. public EmployeeBuilder setId(int id) {
  19. this.id = id;
  20. return this;
  21. }

  22. public EmployeeBuilder setPassport_number(String passport_number) {
  23. this.passport_number = passport_number;
  24. return this;
  25. }

  26. public EmployeeBuilder setTemp_address(String temp_address) {
  27. this.temp_address = temp_address;
  28. return this;
  29. }

  30. public EmployeeBuilder setPerm_address(String perm_address) {
  31. this.perm_address = perm_address;
  32. return this;
  33. }

  34. public EmployeeBuilder setSalary(int salary) {
  35. this.salary = salary;
  36. return this;
  37. }
  38. public Employee build(){
  39. return new Employee(name, company, id, passport_number, temp_address,
  40. perm_address, salary);
  41. }

  42. }

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.


    This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

    Share the post

    Builder design pattern in java with example program

    ×

    Subscribe to Java Tutorial - Instanceofjava

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×