<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Builder_Design_Pattern</id>
	<title>Builder Design Pattern - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://rasimsen.com/index.php?action=history&amp;feed=atom&amp;title=Builder_Design_Pattern"/>
	<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Builder_Design_Pattern&amp;action=history"/>
	<updated>2026-04-24T09:43:40Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>http://rasimsen.com/index.php?title=Builder_Design_Pattern&amp;diff=526&amp;oldid=prev</id>
		<title>Rasimsen: /* Builder Design Pattern in Java */</title>
		<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Builder_Design_Pattern&amp;diff=526&amp;oldid=prev"/>
		<updated>2018-07-23T22:30:41Z</updated>

		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Builder Design Pattern in Java&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en-GB&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 22:30, 23 July 2018&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l20&quot; &gt;Line 20:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 20:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Let’s see how we can implement builder design pattern in java.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Let’s see how we can implement builder design pattern in java.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*First of all you need to create a static nested class and then copy all the arguments from the outer class to the Builder class. We should follow the naming convention and if the class name is Computer then builder class should be named as ComputerBuilder.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;First of all you need to create a static nested class&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039; &lt;/ins&gt;and then &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;copy all the arguments from the outer class to the Builder class&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;. We should follow the naming convention and if the class name is Computer then builder class &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;should be named as ComputerBuilder&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*Java Builder class should have a public constructor with all the required attributes as parameters.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*Java Builder class &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;should have a public constructor with all the required attributes as parameters&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*Java Builder class should have methods to set the optional parameters and it should return the same Builder object after setting the optional attribute.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*Java Builder class &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;should have methods to set the optional parameters and it should return the same Builder object after setting the optional attribute&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*The final step is to provide a build() method in the builder class that will return the Object needed by client program. For this we need to have a private constructor in the Class with Builder class as argument.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039;&lt;/ins&gt;The final step is to provide a build() method in the builder class&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;#039;&amp;#039;&amp;#039; &lt;/ins&gt;that will return the Object needed by client program. For this we need to have a private constructor in the Class with Builder class as argument.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here is the sample builder pattern example code where we have a Computer class and ComputerBuilder class to build it.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Here is the sample builder pattern example code where we have a Computer class and ComputerBuilder class to build it.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Rasimsen</name></author>
		
	</entry>
	<entry>
		<id>http://rasimsen.com/index.php?title=Builder_Design_Pattern&amp;diff=525&amp;oldid=prev</id>
		<title>Rasimsen: Created page with &quot; Separates object construction from its representation  Builder design pattern is a creational design pattern like Factory Pattern and Abstract Factory Pattern.  Builder patte...&quot;</title>
		<link rel="alternate" type="text/html" href="http://rasimsen.com/index.php?title=Builder_Design_Pattern&amp;diff=525&amp;oldid=prev"/>
		<updated>2018-07-23T22:28:01Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; Separates object construction from its representation  Builder design pattern is a creational design pattern like Factory Pattern and Abstract Factory Pattern.  Builder patte...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
Separates object construction from its representation&lt;br /&gt;
&lt;br /&gt;
Builder design pattern is a creational design pattern like Factory Pattern and Abstract Factory Pattern.&lt;br /&gt;
&lt;br /&gt;
Builder pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.&lt;br /&gt;
&lt;br /&gt;
There are &amp;#039;&amp;#039;&amp;#039;three major issues&amp;#039;&amp;#039;&amp;#039; with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.&lt;br /&gt;
&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Too Many arguments to pass&amp;#039;&amp;#039;&amp;#039; from client program to the Factory class that can be error prone because most of the time, the type of arguments are same and from client side its hard to maintain the order of the argument.&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;Some of the parameters might be optional&amp;#039;&amp;#039;&amp;#039; but in Factory pattern, &amp;#039;&amp;#039;&amp;#039;we are forced to send all the parameters and optional parameters need to send as NULL&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
*&amp;#039;&amp;#039;&amp;#039;If the object is heavy and its creation is complex&amp;#039;&amp;#039;&amp;#039;, then all that complexity will be part of Factory classes that is confusing.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;We can solve the issues with large number of parameters by providing a constructor with required parameters and then different setter methods to set the optional parameters&amp;#039;&amp;#039;&amp;#039;. The problem with this approach is that the Object state will be inconsistent until unless all the attributes are set explicitly.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
==Builder Design Pattern in Java==&lt;br /&gt;
&lt;br /&gt;
Let’s see how we can implement builder design pattern in java.&lt;br /&gt;
&lt;br /&gt;
*First of all you need to create a static nested class and then copy all the arguments from the outer class to the Builder class. We should follow the naming convention and if the class name is Computer then builder class should be named as ComputerBuilder.&lt;br /&gt;
*Java Builder class should have a public constructor with all the required attributes as parameters.&lt;br /&gt;
*Java Builder class should have methods to set the optional parameters and it should return the same Builder object after setting the optional attribute.&lt;br /&gt;
*The final step is to provide a build() method in the builder class that will return the Object needed by client program. For this we need to have a private constructor in the Class with Builder class as argument.&lt;br /&gt;
&lt;br /&gt;
Here is the sample builder pattern example code where we have a Computer class and ComputerBuilder class to build it.&lt;br /&gt;
&lt;br /&gt;
===Example - Builder Design Pattern===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package com.journaldev.design.builder;&lt;br /&gt;
&lt;br /&gt;
public class Computer {&lt;br /&gt;
	&lt;br /&gt;
	//required parameters&lt;br /&gt;
	private String HDD;&lt;br /&gt;
	private String RAM;&lt;br /&gt;
	&lt;br /&gt;
	//optional parameters&lt;br /&gt;
	private boolean isGraphicsCardEnabled;&lt;br /&gt;
	private boolean isBluetoothEnabled;&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	public String getHDD() {&lt;br /&gt;
		return HDD;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public String getRAM() {&lt;br /&gt;
		return RAM;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public boolean isGraphicsCardEnabled() {&lt;br /&gt;
		return isGraphicsCardEnabled;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	public boolean isBluetoothEnabled() {&lt;br /&gt;
		return isBluetoothEnabled;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	private Computer(ComputerBuilder builder) {&lt;br /&gt;
		this.HDD=builder.HDD;&lt;br /&gt;
		this.RAM=builder.RAM;&lt;br /&gt;
		this.isGraphicsCardEnabled=builder.isGraphicsCardEnabled;&lt;br /&gt;
		this.isBluetoothEnabled=builder.isBluetoothEnabled;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	//Builder Class&lt;br /&gt;
	public static class ComputerBuilder{&lt;br /&gt;
&lt;br /&gt;
		// required parameters&lt;br /&gt;
		private String HDD;&lt;br /&gt;
		private String RAM;&lt;br /&gt;
&lt;br /&gt;
		// optional parameters&lt;br /&gt;
		private boolean isGraphicsCardEnabled;&lt;br /&gt;
		private boolean isBluetoothEnabled;&lt;br /&gt;
		&lt;br /&gt;
		public ComputerBuilder(String hdd, String ram){&lt;br /&gt;
			this.HDD=hdd;&lt;br /&gt;
			this.RAM=ram;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		public ComputerBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) {&lt;br /&gt;
			this.isGraphicsCardEnabled = isGraphicsCardEnabled;&lt;br /&gt;
			return this;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
		public ComputerBuilder setBluetoothEnabled(boolean isBluetoothEnabled) {&lt;br /&gt;
			this.isBluetoothEnabled = isBluetoothEnabled;&lt;br /&gt;
			return this;&lt;br /&gt;
		}&lt;br /&gt;
		&lt;br /&gt;
		public Computer build(){&lt;br /&gt;
			return new Computer(this);&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that Computer class has only getter methods and no public constructor. So the only way to get a Computer object is through the ComputerBuilder class.&lt;br /&gt;
&lt;br /&gt;
Here is a builder pattern example test program showing how to use Builder class to get the object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
package com.journaldev.design.test;&lt;br /&gt;
&lt;br /&gt;
import com.journaldev.design.builder.Computer;&lt;br /&gt;
&lt;br /&gt;
public class TestBuilderPattern {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
		//Using builder to get the object in a single line of code and &lt;br /&gt;
                //without any inconsistent state or arguments management issues		&lt;br /&gt;
		Computer comp = new Computer.ComputerBuilder(&lt;br /&gt;
				&amp;quot;500 GB&amp;quot;, &amp;quot;2 GB&amp;quot;).setBluetoothEnabled(true)&lt;br /&gt;
				.setGraphicsCardEnabled(true).build();&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rasimsen</name></author>
		
	</entry>
</feed>