<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[BuilderPattern]]></title><description><![CDATA[BuilderPattern]]></description><link>https://builderpatterndesign.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 02 Sep 2026 15:57:23 GMT</lastBuildDate><atom:link href="https://builderpatterndesign.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Builder Pattern]]></title><description><![CDATA[Builder Pattern: Making Objects the Easy Way
What is the Builder Pattern?
Imagine you go to a pizza shop. The person at the counter asks you:

"What size pizza?"

"Which base - thin or thick?"

"Do you want cheese?"

"Do you want tomatoes?"

"Do you ...]]></description><link>https://builderpatterndesign.hashnode.dev/builder-pattern</link><guid isPermaLink="true">https://builderpatterndesign.hashnode.dev/builder-pattern</guid><dc:creator><![CDATA[Botla Nagaraju]]></dc:creator><pubDate>Sat, 03 Jan 2026 12:37:11 GMT</pubDate><content:encoded><![CDATA[<h1 id="heading-builder-pattern-making-objects-the-easy-way">Builder Pattern: Making Objects the Easy Way</h1>
<h2 id="heading-what-is-the-builder-pattern">What is the Builder Pattern?</h2>
<p>Imagine you go to a pizza shop. The person at the counter asks you:</p>
<ul>
<li><p>"What size pizza?"</p>
</li>
<li><p>"Which base - thin or thick?"</p>
</li>
<li><p>"Do you want cheese?"</p>
</li>
<li><p>"Do you want tomatoes?"</p>
</li>
<li><p>"Do you want onions?"</p>
</li>
<li><p>"Do you want peppers?"</p>
</li>
<li><p>"Do you want olives?"</p>
</li>
</ul>
<p>Now, some things are <strong>must-have</strong> (like size and base), but other things are <strong>your choice</strong> (like toppings). You might want some toppings but not all.</p>
<p><strong>Builder Pattern</strong> is like this pizza ordering system. It helps us create objects (things) when we have many options, and some are required while others are optional.</p>
<hr />
<h2 id="heading-real-world-example-ordering-a-sandwich">Real-World Example: Ordering a Sandwich</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1767443557107/d82ab16e-6b33-41a7-954e-f7364d306b7d.png" alt class="image--center mx-auto" /></p>
<p>Let's say you go to a sandwich shop.</p>
<p><strong>Must-have things:</strong></p>
<ul>
<li><p>Bread type (white or brown)</p>
</li>
<li><p>Size (small or large)</p>
</li>
</ul>
<p><strong>Optional things:</strong></p>
<ul>
<li><p>Cheese (yes or no)</p>
</li>
<li><p>Lettuce (yes or no)</p>
</li>
<li><p>Tomato (yes or no)</p>
</li>
<li><p>Mayo (yes or no)</p>
</li>
</ul>
<p>Without the Builder Pattern, you would need to tell the shopkeeper:</p>
<p>"I want a sandwich with white bread, large size, YES cheese, NO lettuce, YES tomato, NO mayo."</p>
<p>Every time, you have to say YES or NO for every single topping, even if you don't care about some of them.</p>
<p><strong>With Builder Pattern</strong>, you only mention what you want:</p>
<p>"I want a sandwich with white bread, large size, and add cheese and tomato."</p>
<p>That's it! You don't need to worry about the things you don't want. The system handles it for you.</p>
<hr />
<h2 id="heading-what-problem-does-builder-pattern-solve">What Problem Does Builder Pattern Solve?</h2>
<h3 id="heading-problem-1-too-many-constructors-telescoping-constructor-problem">Problem 1: Too Many Constructors (Telescoping Constructor Problem)</h3>
<p>Imagine you have a class called <code>Sandwich</code>. Without Builder Pattern, you might write many constructors like this:</p>
<pre><code class="lang-java">Sandwich(bread, size)
Sandwich(bread, size, cheese)
Sandwich(bread, size, cheese, lettuce)
Sandwich(bread, size, cheese, lettuce, tomato)
Sandwich(bread, size, cheese, lettuce, tomato, mayo)
</code></pre>
<p>This becomes messy! Too many combinations. Hard to remember which constructor to use.</p>
<h3 id="heading-problem-2-passing-null-or-false-for-everything">Problem 2: Passing Null or False for Everything</h3>
<p>Another way is to have one big constructor:</p>
<pre><code class="lang-java">Sandwich(bread, size, cheese, lettuce, tomato, mayo)
</code></pre>
<p>But now, if you don't want lettuce or mayo, you have to pass:</p>
<pre><code class="lang-java"><span class="hljs-keyword">new</span> Sandwich(<span class="hljs-string">"white"</span>, <span class="hljs-string">"large"</span>, <span class="hljs-keyword">true</span>, <span class="hljs-keyword">false</span>, <span class="hljs-keyword">true</span>, <span class="hljs-keyword">false</span>)
</code></pre>
<p>See those <code>true</code> and <code>false</code>? Hard to remember what each one means! Confusing, right?</p>
<p><strong>Builder Pattern fixes both problems.</strong></p>
<hr />
<h2 id="heading-how-does-builder-pattern-work">How Does Builder Pattern Work?</h2>
<p>Think of Builder Pattern like filling a form step by step.</p>
<ol>
<li><p>First, you fill in the <strong>required fields</strong> (like name and age).</p>
</li>
<li><p>Then, you <strong>only fill the optional fields</strong> you want (like phone number or email).</p>
</li>
<li><p>Finally, you click <strong>Submit</strong> button.</p>
</li>
</ol>
<p>In programming, it works the same way:</p>
<ol>
<li><p>Set the required fields</p>
</li>
<li><p>Set only the optional fields you need</p>
</li>
<li><p>Call <code>build()</code> method to create the final object</p>
</li>
</ol>
<hr />
<h2 id="heading-simple-code-example">Simple Code Example</h2>
<p>Let's create a <code>Sandwich</code> class using Builder Pattern.</p>
<pre><code class="lang-java"><span class="hljs-comment">// Main Sandwich class</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sandwich</span> </span>{
    <span class="hljs-keyword">private</span> String bread;      <span class="hljs-comment">// required</span>
    <span class="hljs-keyword">private</span> String size;       <span class="hljs-comment">// required</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> cheese;    <span class="hljs-comment">// optional</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> lettuce;   <span class="hljs-comment">// optional</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> tomato;    <span class="hljs-comment">// optional</span>

    <span class="hljs-comment">// Private constructor - only Builder can create Sandwich</span>
    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">Sandwich</span><span class="hljs-params">(SandwichBuilder builder)</span> </span>{
        <span class="hljs-keyword">this</span>.bread = builder.bread;
        <span class="hljs-keyword">this</span>.size = builder.size;
        <span class="hljs-keyword">this</span>.cheese = builder.cheese;
        <span class="hljs-keyword">this</span>.lettuce = builder.lettuce;
        <span class="hljs-keyword">this</span>.tomato = builder.tomato;
    }

    <span class="hljs-comment">// Builder class inside Sandwich class</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SandwichBuilder</span> </span>{
        <span class="hljs-keyword">private</span> String bread;      <span class="hljs-comment">// required</span>
        <span class="hljs-keyword">private</span> String size;       <span class="hljs-comment">// required</span>
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> cheese;    <span class="hljs-comment">// optional, default false</span>
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> lettuce;   <span class="hljs-comment">// optional, default false</span>
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> tomato;    <span class="hljs-comment">// optional, default false</span>

        <span class="hljs-comment">// Constructor with required fields only</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">SandwichBuilder</span><span class="hljs-params">(String bread, String size)</span> </span>{
            <span class="hljs-keyword">this</span>.bread = bread;
            <span class="hljs-keyword">this</span>.size = size;
        }

        <span class="hljs-comment">// Methods to set optional fields</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> SandwichBuilder <span class="hljs-title">addCheese</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">this</span>.cheese = <span class="hljs-keyword">true</span>;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;  <span class="hljs-comment">// return "this" to allow chaining</span>
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> SandwichBuilder <span class="hljs-title">addLettuce</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">this</span>.lettuce = <span class="hljs-keyword">true</span>;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> SandwichBuilder <span class="hljs-title">addTomato</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">this</span>.tomato = <span class="hljs-keyword">true</span>;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
        }

        <span class="hljs-comment">// Final build method</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> Sandwich <span class="hljs-title">build</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Sandwich(<span class="hljs-keyword">this</span>);
        }
    }
}
</code></pre>
<h3 id="heading-how-to-use-this-code">How to Use This Code</h3>
<pre><code class="lang-java">Sandwich mySandwich = <span class="hljs-keyword">new</span> Sandwich.SandwichBuilder(<span class="hljs-string">"white"</span>, <span class="hljs-string">"large"</span>)
                            .addCheese()
                            .addTomato()
                            .build();
</code></pre>
<hr />
<h2 id="heading-understanding-the-code-line-by-line">Understanding the Code Line by Line</h2>
<h3 id="heading-part-1-main-sandwich-class">Part 1: Main Sandwich Class</h3>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sandwich</span> </span>{
    <span class="hljs-keyword">private</span> String bread;
    <span class="hljs-keyword">private</span> String size;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> cheese;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> lettuce;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> tomato;
</code></pre>
<p><strong>Explanation:</strong> These are the ingredients of our sandwich. <code>private</code> means no one can change them from outside.</p>
<hr />
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">Sandwich</span><span class="hljs-params">(SandwichBuilder builder)</span> </span>{
        <span class="hljs-keyword">this</span>.bread = builder.bread;
        <span class="hljs-keyword">this</span>.size = builder.size;
        <span class="hljs-keyword">this</span>.cheese = builder.cheese;
        <span class="hljs-keyword">this</span>.lettuce = builder.lettuce;
        <span class="hljs-keyword">this</span>.tomato = builder.tomato;
    }
</code></pre>
<p><strong>Explanation:</strong> This is the constructor (the recipe). It's <code>private</code>, so only the Builder class can use it. It takes values from the Builder and creates the Sandwich.</p>
<hr />
<h3 id="heading-part-2-builder-class">Part 2: Builder Class</h3>
<pre><code class="lang-java">    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SandwichBuilder</span> </span>{
        <span class="hljs-keyword">private</span> String bread;
        <span class="hljs-keyword">private</span> String size;
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> cheese;
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> lettuce;
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> tomato;
</code></pre>
<p><strong>Explanation:</strong> This is our Builder class. It has the same ingredients as Sandwich. Think of it as the order form.</p>
<hr />
<pre><code class="lang-java">        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">SandwichBuilder</span><span class="hljs-params">(String bread, String size)</span> </span>{
            <span class="hljs-keyword">this</span>.bread = bread;
            <span class="hljs-keyword">this</span>.size = size;
        }
</code></pre>
<p><strong>Explanation:</strong> This constructor asks only for the <strong>required</strong> things: bread and size. The optional things will be added later if needed.</p>
<hr />
<pre><code class="lang-java">        <span class="hljs-function"><span class="hljs-keyword">public</span> SandwichBuilder <span class="hljs-title">addCheese</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">this</span>.cheese = <span class="hljs-keyword">true</span>;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
        }
</code></pre>
<p><strong>Explanation:</strong> This method adds cheese to the sandwich. It returns <code>this</code> (which means the Builder itself), so you can keep adding more things in a chain.</p>
<hr />
<pre><code class="lang-java">        <span class="hljs-function"><span class="hljs-keyword">public</span> Sandwich <span class="hljs-title">build</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Sandwich(<span class="hljs-keyword">this</span>);
        }
</code></pre>
<p><strong>Explanation:</strong> This is like clicking the "Submit" button. It creates the final Sandwich using all the information you provided.</p>
<hr />
<h3 id="heading-using-the-builder">Using the Builder</h3>
<pre><code class="lang-java">Sandwich mySandwich = <span class="hljs-keyword">new</span> Sandwich.SandwichBuilder(<span class="hljs-string">"white"</span>, <span class="hljs-string">"large"</span>)
                            .addCheese()
                            .addTomato()
                            .build();
</code></pre>
<p><strong>Explanation step by step:</strong></p>
<ol>
<li><p><code>new Sandwich.SandwichBuilder("white", "large")</code> - Start the order with required items: white bread, large size</p>
</li>
<li><p><code>.addCheese()</code> - Add cheese (optional)</p>
</li>
<li><p><code>.addTomato()</code> - Add tomato (optional)</p>
</li>
<li><p><code>.build()</code> - Finish and create the sandwich</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1767443696288/cd92b2bb-f44b-453d-afd2-541471b27364.png" alt class="image--center mx-auto" /></p>
<p>Notice: We didn't add lettuce. That's okay! Builder handles it automatically (keeps it false).</p>
<hr />
<h2 id="heading-when-to-use-builder-pattern">When to Use Builder Pattern</h2>
<p><strong>Use it when:</strong></p>
<ol>
<li><p>Your object has <strong>many fields</strong> (like 5 or more)</p>
</li>
<li><p>Some fields are <strong>required</strong>, some are <strong>optional</strong></p>
</li>
<li><p>You want to make objects that <strong>cannot be changed</strong> after creation</p>
</li>
<li><p>You want <strong>clean and readable</strong> code</p>
</li>
</ol>
<p><strong>Real-world examples:</strong></p>
<ul>
<li><p>Amazon shopping cart (add items one by one)</p>
</li>
<li><p>Creating a user profile (name required, address optional, phone optional)</p>
</li>
<li><p>Building a computer (processor required, graphics card optional)</p>
</li>
</ul>
<hr />
<h2 id="heading-when-not-to-use-builder-pattern">When NOT to Use Builder Pattern</h2>
<p><strong>Avoid it when:</strong></p>
<ol>
<li><p>Your object has only <strong>2 or 3 simple fields</strong></p>
</li>
<li><p>All fields are <strong>required</strong></p>
</li>
<li><p>The object needs to be <strong>changed frequently</strong> after creation</p>
</li>
</ol>
<p>For simple objects, Builder Pattern is overkill (too much extra work for no benefit).</p>
<hr />
<h2 id="heading-advantages-good-things">Advantages (Good Things)</h2>
<ol>
<li><p><strong>Easy to read</strong> - Code looks clean and simple</p>
</li>
<li><p><strong>No confusion</strong> - You only set what you need</p>
</li>
<li><p><strong>No long constructors</strong> - No need to remember the order of 10 parameters</p>
</li>
<li><p><strong>Safe objects</strong> - Objects cannot be changed after creation</p>
</li>
</ol>
<hr />
<h2 id="heading-disadvantages-not-so-good-things">Disadvantages (Not-So-Good Things)</h2>
<ol>
<li><p><strong>Extra code</strong> - You need to write a separate Builder class</p>
</li>
<li><p><strong>Overkill for simple classes</strong> - If your class has only 2 fields, Builder Pattern is too much work</p>
</li>
</ol>
<hr />
<h2 id="heading-real-examples-where-builder-pattern-is-used">Real Examples Where Builder Pattern is Used</h2>
<ol>
<li><p><strong>Lombok</strong> - A Java library that automatically creates Builder for you</p>
</li>
<li><p><strong>Amazon Cart</strong> - When you add items to your cart one by one</p>
</li>
<li><p><strong>StringBuilder in Java</strong> - Building strings step by step</p>
</li>
<li><p><strong>Android AlertDialog</strong> - Creating dialogs with optional buttons and messages</p>
</li>
</ol>
<hr />
<h2 id="heading-summary-in-simple-words">Summary in Simple Words</h2>
<p>Builder Pattern is like ordering food at a restaurant:</p>
<ol>
<li><p>You tell what you <strong>must have</strong> (like rice or bread)</p>
</li>
<li><p>You add <strong>only the extras</strong> you want (like cheese or sauce)</p>
</li>
<li><p>You place the order, and your food is ready</p>
</li>
</ol>
<p>It makes creating objects easy and clean when you have many options. No need to pass <code>null</code> or <code>false</code> for things you don't want. Just add what you need, and the Builder handles the rest!</p>
<p>Think of it as building with LEGO blocks - add pieces one by one, and when you're done, you have your final creation!</p>
]]></content:encoded></item></channel></rss>