Java Design Pattern – Builder Pattern

Builder Pattern is under creational pattern for building a complex object in software development. JavaSampleApproach introduces reasons and how to program with Builder Pattern.

I. Problems

For initializing an object, constructor is a common solution. Example with Customer class:

package com.javasampleapproach.builderpattern;


public class Customer {
	private long id;
	private String firstname;
	private String lastname;
	private String city;
	private String gender;
	
	public Customer(long id, String firstname, String lastname, String city, String gender){
		this.id = id;
		this.firstname = firstname;
		this.lastname = lastname;
		this.city = city;
		this.gender = gender;
	}
	
	@Override
	public String toString() {
		String info = String.format("Customer id = %d, firstname = %s, lastname = %s, city = %s, gender = %s", 
				id, firstname, lastname, city, gender);
		return info;
	}
	
	public static void main(String[] args) {
		Customer cust = new Customer(1, "Jack", "Johnson ", "California", "male");
		System.out.println(cust.toString());
	}
}

Output:

Customer id = 1, firstname = Jack, lastname = Johnson , city = California, gender = male

But problems come when Customers class has lots of fields (from 10 to 20 fields or more fields in real-world base on business rules). So it makes be more harder for programming when mapping the right order of each fields in a contructor with lots of parameters. And it also makes be hard for reading the sourcecode.

One solution can be used is setter methods. But it will make another risk when developers forget to set status for any field of an object by setter methods.

II. Solution with Builder Pattern

– Builder Pattern is the best way for building a complex object. That makes sourcecode be more readable.

Sample with Customer class & Builder Pattern:

public class Customer{

	private long id;
	private String firstname;
	private String lastname;
	private String city;
	private String gender;

	public static class CustomerBuilder{
		private long id;
		private String firstname;
		private String lastname;
		private String city;
		private String gender;
		
		public CustomerBuilder(long id){
			this.id = id;
		}
		
		public CustomerBuilder firstName(String firstName){
			this.firstname = firstName;
			return this;
		}
		
		public CustomerBuilder lastName(String lastName){
			this.lastname = lastName;
			return this;
		}
		
		public CustomerBuilder atCity(String city){
			this.city = city;
			return this;
		}
		
		public CustomerBuilder gender(String gender){
			this.gender = gender;
			return this;
		}

		public Customer build(){
			Customer cust = new Customer();
			cust.id = this.id;
			cust.firstname = this.firstname;
			cust.lastname = this.lastname;
			cust.city = this.city;
			cust.gender = this.gender;
			return cust;
		}
		
	}
	
	@Override
	public String toString() {
		String info = String.format("Customer id = %d, firstname = %s, lastname = %s, city = %s, gender = %s", 
				id, firstname, lastname, city, gender);
		return info;
	}
	
	private Customer(){
	}
	
	public static void main(String[] args) {
		Customer cust = new Customer.CustomerBuilder(1)
									.firstName("Jack")
									.lastName("Johnson")
									.atCity("Califonia")
									.gender("male")
									.build();
		
		System.out.println(cust.toString());
	}
}

Output:

Customer id = 1, firstname = Jack, lastname = Johnson, city = Califonia, gender = male
III. Source code

Builder Pattern



By grokonez | December 22, 2016.

Last updated on June 4, 2017.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*