<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Parth Kamal</title>
    <description>The latest articles on Forem by Parth Kamal (@parthkamal).</description>
    <link>https://forem.com/parthkamal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1081438%2Fc9b15916-9979-4c6c-bf30-3038701b888b.jpeg</url>
      <title>Forem: Parth Kamal</title>
      <link>https://forem.com/parthkamal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/parthkamal"/>
    <language>en</language>
    <item>
      <title>System design terms</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Mon, 17 Nov 2025 09:40:26 +0000</pubDate>
      <link>https://forem.com/parthkamal/system-design-terms-1794</link>
      <guid>https://forem.com/parthkamal/system-design-terms-1794</guid>
      <description>&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;scalibility&lt;/strong&gt; → Ability of the system to handle increased load by adding resources (horizontal/vertical scaling).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;stability&lt;/strong&gt; → Predictable behavior under fluctuating or sudden load; avoids meltdown and cascading failures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;availability&lt;/strong&gt; → Percentage of time a system is up and reachable (uptime metric).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;durability&lt;/strong&gt; → Guarantees that once data is written, it will never be lost—even during failures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;fault tolerance&lt;/strong&gt; → Ability of the system to continue operating even when components fail.
→ Fault tolerance improves availability but is not equal to availability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;reliability&lt;/strong&gt; → Ability of the system to operate correctly and failure-free over time.
→ Measured by MTBF (Mean Time Between Failures).
→ Reliability ≠ availability metric.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;consistency&lt;/strong&gt; → Every read returns the most recent write (for strong consistency).
→ Or follows the defined consistency model (eventual, causal, etc.).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;latency&lt;/strong&gt; → Time taken for the system to respond to a request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;performance&lt;/strong&gt; → Performance = how fast the system processes work (latency + throughput + resource usage).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;efficiency&lt;/strong&gt; → How optimally the system uses CPU, memory, network, storage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;maintainability&lt;/strong&gt; → How easy it is to update, debug, refactor, and extend the system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;observability&lt;/strong&gt; → Ability to understand the internal state of a system from its external outputs (logs, metrics, traces). Not just logging&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;security&lt;/strong&gt; → Protecting the system against unauthorized access, misuse, attacks, and data breaches.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Understanding the `$` Symbol in Bash Scripting</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Fri, 14 Nov 2025 21:26:57 +0000</pubDate>
      <link>https://forem.com/parthkamal/understanding-the-symbol-in-bash-scripting-2ee4</link>
      <guid>https://forem.com/parthkamal/understanding-the-symbol-in-bash-scripting-2ee4</guid>
      <description>&lt;p&gt;In Bash, the &lt;code&gt;$&lt;/code&gt; symbol appears everywhere — and for good reason.&lt;br&gt;
Whether you’re working with variables, command substitution, arithmetic, or script arguments, &lt;code&gt;$&lt;/code&gt; is the gateway that lets you access underlying values.&lt;br&gt;
Once you understand how it behaves, the shell becomes significantly easier to navigate.&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;What &lt;code&gt;$&lt;/code&gt; Actually Does&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;At the core, &lt;code&gt;$&lt;/code&gt; instructs Bash to:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“Expand this into its value.”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This applies consistently across variables, commands, expressions, and special parameters.&lt;br&gt;
A single rule powering multiple forms of expansion.&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Key Uses of &lt;code&gt;$&lt;/code&gt; in Bash&lt;/strong&gt;
&lt;/h2&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;1. Working with Variables&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Parth"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. Quoting to Avoid Word Splitting&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;msg&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Hello World"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$msg&lt;/span&gt;      &lt;span class="c"&gt;# splits into separate words&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$msg&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;    &lt;span class="c"&gt;# preserves the original string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. Using &lt;code&gt;${var}&lt;/code&gt; When Appending Text&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"log"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.txt"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4. Capturing Command Output&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;today&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt; | &lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$today&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$count&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;5. Arithmetic Expansion&lt;/strong&gt;
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
&lt;span class="nv"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;4
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt;n &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6. Special Shell Parameters Every Script Uses&lt;/strong&gt;
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Exit code of the previous command&lt;/strong&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;PID of the current shell&lt;/strong&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Script name&lt;/strong&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Positional parameters (arguments passed to the script)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./script.sh apple banana cherry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;    &lt;span class="c"&gt;# apple&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt;    &lt;span class="c"&gt;# banana&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;    &lt;span class="c"&gt;# cherry&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$# &lt;/span&gt;   &lt;span class="c"&gt;# total arguments (3)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;  &lt;span class="c"&gt;# all arguments as separate values&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$*&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;  &lt;span class="c"&gt;# all arguments as one combined string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;7. Indirect and Meta Expansions&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sleep &lt;/span&gt;5 &amp;amp;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$!&lt;/span&gt;    &lt;span class="c"&gt;# PID of the most recent background job&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;user_1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;
&lt;span class="nv"&gt;user_2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Bob"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="p"&gt;!user_*&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;   &lt;span class="c"&gt;# prints variable names starting with 'user_'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;8. A Common Beginner Error&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;result &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;    &lt;span class="c"&gt;# incorrect — spaces make this invalid&lt;/span&gt;
&lt;span class="nv"&gt;result&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;      &lt;span class="c"&gt;# valid form&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;The Underlying Principle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;All these examples rely on one idea:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;$&lt;/code&gt; transforms a reference into the evaluated value behind it.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$name&lt;/code&gt; → gets a variable’s value&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$(command)&lt;/code&gt; → gets command output&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$(( ))&lt;/code&gt; → gets an arithmetic result&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$0&lt;/code&gt; → gets the script’s name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$1&lt;/code&gt;, &lt;code&gt;$2&lt;/code&gt;, … → get argument values&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$?&lt;/code&gt; → gets the previous command’s exit code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A single symbol enabling a wide spectrum of expansions.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Understanding the role of &lt;code&gt;$&lt;/code&gt; brings clarity to Bash scripting.&lt;br&gt;
Once this expansion model clicks, the shell feels far more structured and logical.&lt;/p&gt;

</description>
      <category>bash</category>
    </item>
    <item>
      <title>Arithmetic Expansion in Bash: The Little Feature That Makes Your Scripts Cleaner</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Fri, 14 Nov 2025 21:17:24 +0000</pubDate>
      <link>https://forem.com/parthkamal/arithmetic-expansion-in-bash-the-little-feature-that-makes-your-scripts-cleaner-35fa</link>
      <guid>https://forem.com/parthkamal/arithmetic-expansion-in-bash-the-little-feature-that-makes-your-scripts-cleaner-35fa</guid>
      <description>&lt;p&gt;If you’ve written Bash for any amount of time, you’ve probably bumped into situations where you needed a quick calculation. Maybe it was a loop counter, maybe file-size math, or maybe you were just trying to add two numbers inside a shell script without summoning half the Unix toolchain.&lt;/p&gt;

&lt;p&gt;Most of us start with &lt;code&gt;expr&lt;/code&gt;, and then eventually escape into &lt;code&gt;awk&lt;/code&gt; or &lt;code&gt;bc&lt;/code&gt; when things get messy. It works… but it always feels heavier than it needs to be.&lt;/p&gt;

&lt;p&gt;There’s a simpler tool, always sitting inside the shell, always ready: &lt;strong&gt;arithmetic expansion.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It’s one of those Bash features that quietly improves the quality of your scripts without making a big deal about it.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Core Idea&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Bash lets you evaluate math directly inside your script using the syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;$((&lt;/span&gt; expression &lt;span class="k"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It sounds small, but the convenience and readability it adds is real. You write what you mean, and Bash gives you back the evaluated result.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="k"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it. No external commands, no fuss.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why This Matters More Than It Seems&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;People underestimate arithmetic expansion because it’s small. But in scripting, the small things add up.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It makes loops easier to read.&lt;/li&gt;
&lt;li&gt;It removes awkward &lt;code&gt;expr&lt;/code&gt; invocations.&lt;/li&gt;
&lt;li&gt;It avoids unnecessary process spawning.&lt;/li&gt;
&lt;li&gt;It keeps conditions compact.&lt;/li&gt;
&lt;li&gt;It lets your script &lt;em&gt;feel&lt;/em&gt; like code, not plumbing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you start using it, you don’t go back.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;A More Natural Way of Writing Math&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here’s a simple example that shows how natural it feels:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;10
&lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;20
&lt;span class="nb"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;a &lt;span class="o"&gt;+&lt;/span&gt; b&lt;span class="k"&gt;))&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$sum&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don’t need quotes. You don’t need &lt;code&gt;$&lt;/code&gt; inside the expression. Bash understands what you want.&lt;/p&gt;

&lt;p&gt;Even better, you’re not forced to follow awkward spacing rules. Both of these work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;$((&lt;/span&gt;a+b&lt;span class="k"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;$((&lt;/span&gt; a &lt;span class="o"&gt;+&lt;/span&gt; b &lt;span class="k"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use whatever reads nicely in your script.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What You Can Actually Do With It&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;All the basic arithmetic operators work exactly the way you expect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;addition (&lt;code&gt;+&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;subtraction (&lt;code&gt;-&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;multiplication (&lt;code&gt;*&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;integer division (&lt;code&gt;/&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;modulo (&lt;code&gt;%&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;exponentiation (&lt;code&gt;**&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A quick example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
&lt;span class="c"&gt;# Outputs: 45&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bash also supports increment, decrement, and compound assignments — the stuff that makes loops and counters feel nice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;((&lt;/span&gt;i++&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;((&lt;/span&gt;x +&lt;span class="o"&gt;=&lt;/span&gt; 10&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where arithmetic expansion starts to blend naturally into your scripting style.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Cleaner Conditions&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;One of the highest-impact uses of arithmetic expansion is inside &lt;code&gt;if&lt;/code&gt; statements. It replaces the traditional test syntax (&lt;code&gt;-gt&lt;/code&gt;, &lt;code&gt;-lt&lt;/code&gt;, etc.) with something more readable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt; x &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 3 &lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"x is greater than 3"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suddenly, your code looks like code again.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;A Few Real-World Cases&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here are some examples where arithmetic expansion quietly reduces friction:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Loop counters:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nv"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1&lt;span class="p"&gt;;&lt;/span&gt; i&amp;lt;&lt;span class="o"&gt;=&lt;/span&gt;5&lt;span class="p"&gt;;&lt;/span&gt; i++&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Iteration: &lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Calculating file sizes:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;stat&lt;/span&gt; &lt;span class="nt"&gt;-c&lt;/span&gt;%s file.txt&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt;size &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;1024&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;&lt;span class="s2"&gt;" KB"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Tiny inline math functions:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;add&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nv"&gt;$2&lt;/span&gt; &lt;span class="k"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Working with counts:&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt; | &lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt;count &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are the kinds of places where scripts usually fill up with tiny noisy utilities. Arithmetic expansion keeps things centered, simple, and local.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Classic Mistake to Avoid&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There’s one error everyone makes at least once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;result &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;   &lt;span class="c"&gt;# ❌ Wrong&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bash interprets this as:&lt;br&gt;
“run the command &lt;code&gt;result&lt;/code&gt; with arguments &lt;code&gt;=&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt;.”&lt;/p&gt;

&lt;p&gt;The fix is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;result&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No spaces around &lt;code&gt;=&lt;/code&gt;.&lt;br&gt;
Small rule, big habit.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why It's Actually Faster&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This part surprises people: &lt;code&gt;$(( ))&lt;/code&gt; is not just cleaner — it’s &lt;strong&gt;faster&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Commands like &lt;code&gt;expr&lt;/code&gt;, &lt;code&gt;awk&lt;/code&gt;, and &lt;code&gt;bc&lt;/code&gt; create new processes. Arithmetic expansion happens inside Bash itself. When you’re in a loop with thousands of iterations, the performance difference becomes noticeable.&lt;/p&gt;

&lt;p&gt;This is one of the reasons DevOps engineers and shell power-users rely on it so heavily.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Bottom Line&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Arithmetic expansion is one of those hidden gems in Bash — small, elegant, and surprisingly powerful. You don’t need it to write shell scripts, but once you start using it, your scripts become more readable, more consistent, and a little bit more enjoyable to write.&lt;/p&gt;

&lt;p&gt;It’s not flashy.&lt;br&gt;
It’s not complicated.&lt;br&gt;
But it makes your scripts feel like they were written intentionally.&lt;/p&gt;

&lt;p&gt;And that’s the whole point.&lt;/p&gt;

</description>
      <category>cli</category>
      <category>linux</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding the Prototype Design Pattern in Java</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Fri, 07 Nov 2025 13:45:17 +0000</pubDate>
      <link>https://forem.com/parthkamal/understanding-the-prototype-design-pattern-in-java-3d52</link>
      <guid>https://forem.com/parthkamal/understanding-the-prototype-design-pattern-in-java-3d52</guid>
      <description>&lt;p&gt;In real-world software development, creating new objects can often be &lt;strong&gt;costly or time-consuming&lt;/strong&gt; — especially when they involve heavy initialization logic, database connections, or complex configurations. The &lt;strong&gt;Prototype Design Pattern&lt;/strong&gt; helps tackle this challenge by allowing you to &lt;strong&gt;clone existing objects instead of creating new ones&lt;/strong&gt; from scratch. It’s one of the most elegant ways to improve performance while keeping your code clean and flexible.&lt;/p&gt;




&lt;h3&gt;
  
  
  What is the Prototype Pattern?
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Prototype Design Pattern&lt;/strong&gt; is a &lt;strong&gt;creational pattern&lt;/strong&gt; that focuses on &lt;strong&gt;object cloning&lt;/strong&gt;. Rather than instantiating an object using &lt;code&gt;new&lt;/code&gt;, you create a “prototype” — an original instance — and then generate new objects by cloning it.&lt;/p&gt;

&lt;p&gt;This pattern becomes extremely useful when &lt;strong&gt;object creation is expensive&lt;/strong&gt;, or when you want to &lt;strong&gt;decouple the client code from specific class implementations&lt;/strong&gt;. Essentially, it shifts the focus from constructing to copying.&lt;/p&gt;




&lt;h3&gt;
  
  
  Key Idea Behind Prototype
&lt;/h3&gt;

&lt;p&gt;At the core of the pattern lies a &lt;strong&gt;&lt;code&gt;clone()&lt;/code&gt; method&lt;/strong&gt; that allows an object to produce its copy. This method is defined in a &lt;strong&gt;Prototype interface&lt;/strong&gt;, and each class implementing it decides &lt;strong&gt;how deep the copy should be&lt;/strong&gt; — shallow or deep.&lt;/p&gt;

&lt;p&gt;This simple idea offers &lt;strong&gt;tremendous flexibility&lt;/strong&gt;, especially when combined with a &lt;strong&gt;Prototype Registry&lt;/strong&gt;, which acts as a centralized store for predefined prototypes that can be cloned anytime.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example Implementation
&lt;/h3&gt;

&lt;p&gt;Let’s look at a simple Java implementation to understand it better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.HashMap&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Map&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Prototype&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Prototype&lt;/span&gt; &lt;span class="nf"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Prototype&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="nf"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getDepartment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;department&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PrototypeRegistry&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Prototype&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;registry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Prototype&lt;/span&gt; &lt;span class="n"&gt;prototype&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prototype&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Prototype&lt;/span&gt; &lt;span class="nf"&gt;getPrototype&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorld&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"parth"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"me"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;PrototypeRegistry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;register&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"emp"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="n"&gt;clonedEmployee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;PrototypeRegistry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPrototype&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"emp"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clonedEmployee&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;p&gt;Here, the &lt;strong&gt;&lt;code&gt;Prototype&lt;/code&gt; interface&lt;/strong&gt; defines a single method &lt;code&gt;clone()&lt;/code&gt;, which the &lt;strong&gt;&lt;code&gt;Employee&lt;/code&gt; class&lt;/strong&gt; implements. Instead of creating a new &lt;code&gt;Employee&lt;/code&gt; object directly using &lt;code&gt;new&lt;/code&gt;, we simply &lt;strong&gt;clone an existing prototype&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;PrototypeRegistry&lt;/code&gt;&lt;/strong&gt; holds a collection of registered prototypes. When we call &lt;code&gt;getPrototype()&lt;/code&gt;, it &lt;strong&gt;retrieves and clones&lt;/strong&gt; the prototype, returning a fresh copy each time.&lt;/p&gt;

&lt;p&gt;This structure helps maintain &lt;strong&gt;clean separation between the object creation logic and the client code&lt;/strong&gt;, leading to more modular and scalable design.&lt;/p&gt;




&lt;h3&gt;
  
  
  Shallow Copy vs Deep Copy — The Core Difference
&lt;/h3&gt;

&lt;p&gt;When it comes to cloning, &lt;strong&gt;not all copies are the same&lt;/strong&gt;. The Prototype pattern can be implemented in two ways depending on how you copy the object’s internal data:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Shallow Copy&lt;/strong&gt; – Copies only the object’s &lt;strong&gt;primitive fields&lt;/strong&gt; and keeps references to the same inner objects.&lt;br&gt;
If the cloned object modifies those inner objects, it affects the original too.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deep Copy&lt;/strong&gt; – Creates a &lt;strong&gt;completely independent clone&lt;/strong&gt;, including copies of nested or referenced objects.&lt;br&gt;
This ensures that changes in the clone &lt;strong&gt;don’t affect the original&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s an example to make the difference clear:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;deptName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;deptName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deptName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deptName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Prototype&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;department&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Deep copy&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="nf"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deptName&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getDepartment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;deptName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this version, the &lt;strong&gt;clone creates a new Department object&lt;/strong&gt; instead of reusing the existing reference — this is a &lt;strong&gt;deep copy&lt;/strong&gt;.&lt;br&gt;
So if you modify the cloned object’s department, the original remains unaffected.&lt;/p&gt;




&lt;h3&gt;
  
  
  Why Deep Copy Matters
&lt;/h3&gt;

&lt;p&gt;In real applications, most objects contain references to other objects — lists, maps, or nested entities. Using a shallow copy in such cases can lead to &lt;strong&gt;unexpected side effects&lt;/strong&gt; where changes in one clone ripple back to the original.&lt;/p&gt;

&lt;p&gt;That’s why understanding &lt;strong&gt;when to use deep vs shallow copy&lt;/strong&gt; is crucial in applying the Prototype pattern correctly. Deep copy ensures &lt;strong&gt;true independence&lt;/strong&gt; of clones, at the cost of slightly higher memory and CPU usage.&lt;/p&gt;




&lt;h3&gt;
  
  
  Advantages of Prototype Pattern
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Faster Object Creation:&lt;/strong&gt; Reduces the overhead of expensive initialization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decoupled Design:&lt;/strong&gt; The client doesn’t need to know the exact class being cloned.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runtime Flexibility:&lt;/strong&gt; Prototypes can be added or modified dynamically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supports Deep Cloning:&lt;/strong&gt; Makes it possible to produce fully independent copies.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Real-World Analogy
&lt;/h3&gt;

&lt;p&gt;Think of the Prototype pattern like &lt;strong&gt;using document templates&lt;/strong&gt; in Word or Google Docs. You set up one base document (prototype), and every new document you create from it is a copy. You don’t rebuild it from scratch each time. Similarly, in code, instead of reconstructing complex objects, you just clone the prototype and tweak what you need.&lt;/p&gt;




&lt;h3&gt;
  
  
  When to Use
&lt;/h3&gt;

&lt;p&gt;You should consider using the Prototype pattern when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Object creation is &lt;strong&gt;complex or resource-heavy&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You frequently need &lt;strong&gt;objects similar to existing ones&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You want to &lt;strong&gt;avoid tight coupling&lt;/strong&gt; between client code and concrete classes.&lt;/li&gt;
&lt;li&gt;You need to &lt;strong&gt;preserve initial states or configurations&lt;/strong&gt; across multiple instances.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Prototype Design Pattern&lt;/strong&gt; is not just about copying objects — it’s about &lt;strong&gt;copying efficiently and safely&lt;/strong&gt;. It shines in scenarios where &lt;strong&gt;object creation is expensive or repetitive&lt;/strong&gt;, and offers the flexibility to choose between &lt;strong&gt;shallow and deep copies&lt;/strong&gt; based on your needs.&lt;/p&gt;

&lt;p&gt;By combining a &lt;strong&gt;Prototype Registry&lt;/strong&gt; with &lt;strong&gt;custom cloning logic&lt;/strong&gt;, you can build scalable, high-performance systems that reuse existing data without redundancy — keeping your code both efficient and elegant.&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>java</category>
      <category>performance</category>
    </item>
    <item>
      <title>Builder Design Pattern in Java</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Fri, 07 Nov 2025 12:54:30 +0000</pubDate>
      <link>https://forem.com/parthkamal/builder-design-pattern-in-java-3p53</link>
      <guid>https://forem.com/parthkamal/builder-design-pattern-in-java-3p53</guid>
      <description>&lt;p&gt;The &lt;strong&gt;Builder Design Pattern&lt;/strong&gt; is one of the most widely used creational patterns in Java. It helps in constructing complex objects step-by-step, especially when a class has many optional parameters or configurations. While the traditional builder pattern is enough for most use cases, there’s a useful variation that combines the &lt;strong&gt;Classic Builder&lt;/strong&gt; approach with a &lt;strong&gt;Director&lt;/strong&gt;, bringing more flexibility and structure to the object creation process.&lt;/p&gt;




&lt;h3&gt;
  
  
  Understanding the Need
&lt;/h3&gt;

&lt;p&gt;Suppose you are building a &lt;code&gt;Computer&lt;/code&gt; object with multiple fields like CPU, RAM, Storage, and GPU. Some computers might be gaming PCs, others might be office machines, and each of them has different configurations. If you try to use constructors for all these variations, you’ll end up with too many overloaded constructors, making your code confusing and error-prone. The Builder pattern helps you build these objects in a readable, step-by-step manner, and the &lt;strong&gt;Director&lt;/strong&gt; helps you define reusable configurations for specific types of objects.&lt;/p&gt;




&lt;h3&gt;
  
  
  Classic Builder Implementation
&lt;/h3&gt;

&lt;p&gt;Let’s start with the &lt;strong&gt;classic nested builder&lt;/strong&gt; version of the &lt;code&gt;Computer&lt;/code&gt; class.&lt;br&gt;
Here, we define a static inner &lt;code&gt;Builder&lt;/code&gt; class that provides setter methods for each field and a &lt;code&gt;build()&lt;/code&gt; method that returns the constructed object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cpu&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;ram&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;gpu&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Computer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;cpu&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;cpu&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ram&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ram&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;storage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;storage&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gpu&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gpu&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Builder&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cpu&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;ram&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;gpu&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Builder&lt;/span&gt; &lt;span class="nf"&gt;setCpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cpu&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;cpu&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cpu&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Builder&lt;/span&gt; &lt;span class="nf"&gt;setRam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;ram&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ram&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ram&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Builder&lt;/span&gt; &lt;span class="nf"&gt;setStorage&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;storage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Builder&lt;/span&gt; &lt;span class="nf"&gt;setGpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;gpu&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;gpu&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gpu&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Computer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="nd"&gt;@Override&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Computer [CPU="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;cpu&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", RAM="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ram&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", Storage="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", GPU="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;gpu&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"]"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this setup, you can create different computer objects by chaining method calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="n"&gt;gamingPC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Intel i9"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setRam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"32GB"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setStorage&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1TB SSD"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setGpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"RTX 4090"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach works well when each object configuration is manually built in client code.&lt;br&gt;
But what if we need to create &lt;strong&gt;standard configurations&lt;/strong&gt; repeatedly, like gaming PCs, office PCs, or budget laptops?&lt;br&gt;
That’s where the &lt;strong&gt;Director&lt;/strong&gt; comes in.&lt;/p&gt;


&lt;h3&gt;
  
  
  Introducing the Director
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Director&lt;/strong&gt; is responsible for managing the &lt;strong&gt;construction process&lt;/strong&gt;. It defines the &lt;em&gt;sequence&lt;/em&gt; and &lt;em&gt;configuration&lt;/em&gt; of the building steps, while the &lt;strong&gt;Builder&lt;/strong&gt; handles the actual object creation.&lt;br&gt;
In our example, the Director will provide ready-made build methods for different types of computers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ComputerDirector&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="nf"&gt;buildGamingComputer&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Intel i9"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setRam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"32GB"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setStorage&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1TB SSD"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setGpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"RTX 4090"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="nf"&gt;buildOfficeComputer&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Intel i5"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setRam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"16GB"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setStorage&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"512GB SSD"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setGpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Integrated Graphics"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="nf"&gt;buildBudgetComputer&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Computer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Intel i3"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setRam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"8GB"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setStorage&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"256GB SSD"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setGpu&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Integrated Graphics"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Using the Director in the Client Code
&lt;/h3&gt;

&lt;p&gt;Now, the client doesn’t need to remember configurations.&lt;br&gt;
They just need to tell the &lt;strong&gt;Director&lt;/strong&gt; what kind of computer they want, and the Director uses the Builder to create it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ComputerDirector&lt;/span&gt; &lt;span class="n"&gt;director&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ComputerDirector&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="n"&gt;gamingPC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;director&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;buildGamingComputer&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="n"&gt;officePC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;director&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;buildOfficeComputer&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;Computer&lt;/span&gt; &lt;span class="n"&gt;budgetPC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;director&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;buildBudgetComputer&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gamingPC&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;officePC&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;budgetPC&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Computer [CPU=Intel i9, RAM=32GB, Storage=1TB SSD, GPU=RTX 4090]
Computer [CPU=Intel i5, RAM=16GB, Storage=512GB SSD, GPU=Integrated Graphics]
Computer [CPU=Intel i3, RAM=8GB, Storage=256GB SSD, GPU=Integrated Graphics]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  How the Director Complements the Builder
&lt;/h3&gt;

&lt;p&gt;The combination of &lt;strong&gt;Director + Builder&lt;/strong&gt; gives you the best of both worlds.&lt;br&gt;
The &lt;strong&gt;Builder&lt;/strong&gt; provides flexibility for creating custom objects, while the &lt;strong&gt;Director&lt;/strong&gt; defines reusable construction recipes.&lt;br&gt;
This makes your code more modular, reduces duplication, and separates &lt;em&gt;how an object is built&lt;/em&gt; from &lt;em&gt;what the final configuration is&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In large-scale systems, the Director can even take different builder implementations (for example, &lt;code&gt;GamingBuilder&lt;/code&gt;, &lt;code&gt;OfficeBuilder&lt;/code&gt;, &lt;code&gt;ServerBuilder&lt;/code&gt;), allowing full polymorphism.&lt;br&gt;
In simpler terms, the Director knows the &lt;strong&gt;process&lt;/strong&gt;, while each Builder knows the &lt;strong&gt;details&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Key Takeaways
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Director with Classic Builder&lt;/strong&gt; variation is a clean and structured approach when your application needs multiple standardized object configurations.&lt;br&gt;
It maintains &lt;strong&gt;readability&lt;/strong&gt;, improves &lt;strong&gt;code reuse&lt;/strong&gt;, and follows the &lt;strong&gt;Single Responsibility Principle&lt;/strong&gt; — the Builder builds, and the Director directs.&lt;/p&gt;

&lt;p&gt;So the next time you’re creating different variants of complex objects, think beyond the simple builder — let the &lt;strong&gt;Director&lt;/strong&gt; take control of the construction process, while your Builder focuses purely on object assembly.&lt;/p&gt;

</description>
      <category>java</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>Abstract Factory Pattern — Designing Consistent Object Families</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Fri, 07 Nov 2025 11:47:59 +0000</pubDate>
      <link>https://forem.com/parthkamal/abstract-factory-pattern-designing-consistent-object-families-5dn6</link>
      <guid>https://forem.com/parthkamal/abstract-factory-pattern-designing-consistent-object-families-5dn6</guid>
      <description>&lt;p&gt;In software design, one recurring problem is how to create related objects without exposing their concrete classes everywhere in the codebase. The &lt;strong&gt;Abstract Factory pattern&lt;/strong&gt; solves this problem elegantly. It allows you to produce &lt;strong&gt;families of related or dependent objects&lt;/strong&gt; while keeping the client code unaware of the specific implementations. In short, it gives you &lt;strong&gt;flexibility, consistency, and clean separation&lt;/strong&gt; between object creation and usage.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧠 The Core Idea
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Abstract Factory Pattern&lt;/strong&gt; provides an interface for creating families of related objects without specifying their concrete classes. Think of it as a &lt;strong&gt;“factory of factories”&lt;/strong&gt; — a higher-level factory that groups together multiple factories responsible for producing related components.&lt;/p&gt;

&lt;p&gt;Imagine you’re building a &lt;strong&gt;cross-platform GUI library&lt;/strong&gt; that should support both &lt;strong&gt;Windows&lt;/strong&gt; and &lt;strong&gt;Mac&lt;/strong&gt; environments. Each platform has its own style of buttons and checkboxes, and you want the same client code to work with both families of components seamlessly. You don’t want dozens of &lt;code&gt;if-else&lt;/code&gt; checks like &lt;code&gt;if (os == "Windows") new WindowsButton()&lt;/code&gt; scattered around. The Abstract Factory pattern makes that unnecessary.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧱 Step-by-Step Example
&lt;/h3&gt;

&lt;p&gt;Let’s break it down.&lt;/p&gt;

&lt;p&gt;We first define the &lt;strong&gt;abstract products&lt;/strong&gt; — interfaces that declare how our UI components behave.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;paint&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Checkbox&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we define the &lt;strong&gt;concrete products&lt;/strong&gt;, each belonging to a family. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WindowsButton&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;paint&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Rendering a Windows-style button"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MacButton&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;paint&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Rendering a Mac-style button"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WindowsCheckbox&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Checkbox&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Rendering a Windows-style checkbox"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MacCheckbox&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Checkbox&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Rendering a Mac-style checkbox"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we define the &lt;strong&gt;abstract factory interface&lt;/strong&gt;, which provides creation methods for the entire product family:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;GUIFactory&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="nf"&gt;createButton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;Checkbox&lt;/span&gt; &lt;span class="nf"&gt;createCheckbox&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each &lt;strong&gt;concrete factory&lt;/strong&gt; implements this interface and produces its own family of components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WindowsFactory&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;GUIFactory&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="nf"&gt;createButton&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WindowsButton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Checkbox&lt;/span&gt; &lt;span class="nf"&gt;createCheckbox&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WindowsCheckbox&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MacFactory&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;GUIFactory&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="nf"&gt;createButton&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MacButton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Checkbox&lt;/span&gt; &lt;span class="nf"&gt;createCheckbox&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MacCheckbox&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;client code&lt;/strong&gt; depends only on the interfaces, not on the actual implementations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Button&lt;/span&gt; &lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Checkbox&lt;/span&gt; &lt;span class="n"&gt;checkbox&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GUIFactory&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createButton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;checkbox&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createCheckbox&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;paint&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;checkbox&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;render&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Demo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;GUIFactory&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Windows"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// could be read from config&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equalsIgnoreCase&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Windows"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WindowsFactory&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MacFactory&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;render&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rendering a Windows-style button
Rendering a Windows-style checkbox
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  🎯 Why This Pattern Matters
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;client code never touches any concrete class&lt;/strong&gt; like &lt;code&gt;WindowsButton&lt;/code&gt; or &lt;code&gt;MacCheckbox&lt;/code&gt;. It only knows the interfaces — &lt;code&gt;Button&lt;/code&gt;, &lt;code&gt;Checkbox&lt;/code&gt;, and &lt;code&gt;GUIFactory&lt;/code&gt;. This is the essence of &lt;strong&gt;dependency inversion&lt;/strong&gt;: the high-level code depends only on abstractions, not on details.&lt;/p&gt;

&lt;p&gt;Notice that each &lt;strong&gt;concrete factory is tightly coupled to its product family&lt;/strong&gt; — for example, &lt;code&gt;WindowsFactory&lt;/code&gt; knows about &lt;code&gt;WindowsButton&lt;/code&gt; and &lt;code&gt;WindowsCheckbox&lt;/code&gt;. At first glance, this looks like a drawback, but it’s &lt;strong&gt;intentional and necessary&lt;/strong&gt;. The tight coupling is &lt;strong&gt;localized inside the factory&lt;/strong&gt;, so it doesn’t spread across your application. The rest of your system remains decoupled and flexible.&lt;/p&gt;

&lt;p&gt;This design also ensures &lt;strong&gt;consistency among related products&lt;/strong&gt;. Since both &lt;code&gt;Button&lt;/code&gt; and &lt;code&gt;Checkbox&lt;/code&gt; come from the same factory, you’ll never end up with a Windows button next to a Mac checkbox. The Abstract Factory enforces that consistency by design.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧩 Comparing with Factory Method
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Factory Method&lt;/strong&gt; pattern focuses on creating &lt;strong&gt;one type of product&lt;/strong&gt; and uses &lt;strong&gt;inheritance&lt;/strong&gt; — subclasses decide which class to instantiate.&lt;br&gt;
The &lt;strong&gt;Abstract Factory&lt;/strong&gt;, on the other hand, focuses on creating &lt;strong&gt;families of related products&lt;/strong&gt; and uses &lt;strong&gt;composition&lt;/strong&gt; — the factory contains multiple creation methods. In large systems, both patterns often coexist: each method inside the Abstract Factory might itself be implemented using the Factory Method.&lt;/p&gt;

&lt;p&gt;To simplify:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Factory Method = one product, decided by subclass&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Abstract Factory = multiple related products, grouped by family&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  🧠 When to Use Abstract Factory
&lt;/h3&gt;

&lt;p&gt;Use it when you need to create a &lt;strong&gt;consistent set of related objects&lt;/strong&gt; that should work together.&lt;br&gt;
Typical cases include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Building cross-platform UI components (Windows, Mac, Web, etc.)&lt;/li&gt;
&lt;li&gt;Managing multiple themes (Light/Dark mode components)&lt;/li&gt;
&lt;li&gt;Creating plugin systems where each plugin provides its own compatible product set&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Avoid it when you only need to create one object type — the Factory Method pattern will be simpler.&lt;/p&gt;




&lt;h3&gt;
  
  
  🪶 Key Takeaways
&lt;/h3&gt;

&lt;p&gt;The Abstract Factory pattern helps you &lt;strong&gt;encapsulate object creation&lt;/strong&gt;, &lt;strong&gt;preserve consistency&lt;/strong&gt;, and &lt;strong&gt;isolate dependencies&lt;/strong&gt;. The &lt;strong&gt;tight coupling between a concrete factory and its products is deliberate&lt;/strong&gt; — it keeps all creation logic contained, ensuring that clients remain independent of implementation details.&lt;/p&gt;

&lt;p&gt;Whenever you’re designing a system where multiple objects must belong to the same family or theme, &lt;strong&gt;Abstract Factory&lt;/strong&gt; is your go-to tool. It lets you switch entire object families without touching client code, keeping your design clean, extensible, and consistent across environments.&lt;/p&gt;




&lt;p&gt;Would you like me to add a &lt;strong&gt;UML diagram-style ASCII illustration&lt;/strong&gt; (showing factories and product families) right after the example section? It makes the relationships visually clear for readers.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Composition vs Inheritance — The Practical Guide Every Developer Should Know</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Fri, 07 Nov 2025 08:59:40 +0000</pubDate>
      <link>https://forem.com/parthkamal/composition-vs-inheritance-the-practical-guide-every-developer-should-know-3ie</link>
      <guid>https://forem.com/parthkamal/composition-vs-inheritance-the-practical-guide-every-developer-should-know-3ie</guid>
      <description>&lt;h1&gt;
  
  
  Favor Composition Over Inheritance
&lt;/h1&gt;

&lt;p&gt;Every developer hears this advice at some point: &lt;em&gt;“Favor composition over inheritance.”&lt;/em&gt;&lt;br&gt;
But very few really internalize when and why that matters. Inheritance and composition are both powerful tools in object-oriented design, but they serve different purposes. The key is knowing when to use each — and when not to.&lt;/p&gt;




&lt;h2&gt;
  
  
  Understanding the Core Idea
&lt;/h2&gt;

&lt;p&gt;At its heart, the difference comes down to the kind of relationship you want to model.&lt;br&gt;
&lt;strong&gt;Inheritance&lt;/strong&gt; represents an &lt;strong&gt;“is-a”&lt;/strong&gt; relationship — for example, a &lt;code&gt;Car&lt;/code&gt; &lt;em&gt;is a&lt;/em&gt; &lt;code&gt;Vehicle&lt;/code&gt;. It means the subclass should conceptually be a specialized form of the parent.&lt;br&gt;
&lt;strong&gt;Composition&lt;/strong&gt;, on the other hand, models a &lt;strong&gt;“has-a”&lt;/strong&gt; relationship — for example, a &lt;code&gt;Car&lt;/code&gt; &lt;em&gt;has an&lt;/em&gt; &lt;code&gt;Engine&lt;/code&gt;. Here, one class simply contains or uses another to get its work done.&lt;/p&gt;

&lt;p&gt;This distinction seems simple, but it changes how your code evolves over time.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use Inheritance
&lt;/h2&gt;

&lt;p&gt;Inheritance isn’t bad — it’s just easily abused. It’s the right choice when the relationship between classes truly reflects the idea of specialization. If the child class logically “is” the parent type, and you expect this relationship to remain stable, inheritance fits naturally.&lt;/p&gt;

&lt;p&gt;For instance, a &lt;code&gt;Car&lt;/code&gt; extending a &lt;code&gt;Vehicle&lt;/code&gt;, or a &lt;code&gt;Circle&lt;/code&gt; extending an abstract &lt;code&gt;Shape&lt;/code&gt; class, makes sense. In both cases, the child shares a clear structure and purpose with the parent, while still providing its own implementation details.&lt;/p&gt;

&lt;p&gt;Inheritance also shines when you need &lt;strong&gt;polymorphism&lt;/strong&gt; — treating different subclasses under a single unified interface. A list of &lt;code&gt;Shape&lt;/code&gt; objects, for example, can hold circles, rectangles, or triangles, all of which can respond to the same &lt;code&gt;area()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Finally, inheritance is often used when frameworks or libraries enforce it, such as when extending &lt;code&gt;HttpServlet&lt;/code&gt; or &lt;code&gt;Activity&lt;/code&gt; in Java. In such cases, it’s part of the system’s design contract.&lt;/p&gt;

&lt;p&gt;However, you should avoid inheritance when your goal is merely to reuse code, when the base class might change frequently, or when the relationship doesn’t make real-world sense. Extending a class just because you need a few of its methods leads to tight coupling and fragile hierarchies that are difficult to maintain.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use Composition
&lt;/h2&gt;

&lt;p&gt;Composition is the better choice when you want flexibility, loose coupling, and reusability. Instead of saying “X is a Y,” composition says “X uses Y.” That subtle change allows you to modify, replace, or combine behaviors without touching your core classes.&lt;/p&gt;

&lt;p&gt;Let’s take a simple example. Instead of creating separate subclasses for &lt;code&gt;PetrolCar&lt;/code&gt;, &lt;code&gt;DieselCar&lt;/code&gt;, and &lt;code&gt;ElectricCar&lt;/code&gt;, you can let the &lt;code&gt;Car&lt;/code&gt; class hold an &lt;code&gt;Engine&lt;/code&gt; object. By defining an &lt;code&gt;Engine&lt;/code&gt; interface and implementing &lt;code&gt;PetrolEngine&lt;/code&gt;, &lt;code&gt;DieselEngine&lt;/code&gt;, and &lt;code&gt;ElectricEngine&lt;/code&gt;, the &lt;code&gt;Car&lt;/code&gt; can use any of them interchangeably. Even better, you can switch the engine type at runtime without changing the class structure.&lt;/p&gt;

&lt;p&gt;This approach keeps your code modular and open to extension. You can add new types of engines later without modifying the &lt;code&gt;Car&lt;/code&gt; class at all. Composition also helps encapsulate details — the &lt;code&gt;Car&lt;/code&gt; doesn’t need to know how the engine starts; it just delegates that responsibility to the engine object.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Example: Car and Engine
&lt;/h2&gt;

&lt;p&gt;Here’s what that looks like in practice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using inheritance
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; 
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PetrolCar&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting petrol car"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DieselCar&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting diesel car"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works, but quickly becomes messy. Every new engine type requires a new subclass, and the code becomes rigid and repetitive.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using composition
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; 
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PetrolEngine&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting petrol engine..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DieselEngine&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Starting diesel engine..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setEngine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the &lt;code&gt;Car&lt;/code&gt; class doesn’t care what kind of engine it has — it just knows that whatever engine it holds can start. You can pass a &lt;code&gt;PetrolEngine&lt;/code&gt;, change it to a &lt;code&gt;DieselEngine&lt;/code&gt; later, or even introduce a new &lt;code&gt;ElectricEngine&lt;/code&gt; class in the future without touching the &lt;code&gt;Car&lt;/code&gt; implementation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Choosing Between Them
&lt;/h2&gt;

&lt;p&gt;Inheritance creates tight coupling between classes. Changes in the parent can easily ripple through to all children. It’s good for modeling stable “type” relationships but becomes a burden when behavior needs to evolve. Composition, by contrast, keeps classes independent. It’s easier to test, extend, and refactor. It’s the preferred choice for most cases where you want flexibility and code reuse rather than a rigid type hierarchy.&lt;/p&gt;

&lt;p&gt;A simple way to remember it is this:&lt;br&gt;
Use &lt;strong&gt;inheritance&lt;/strong&gt; when you’re defining &lt;em&gt;what something is&lt;/em&gt;.&lt;br&gt;
Use &lt;strong&gt;composition&lt;/strong&gt; when you’re defining &lt;em&gt;what something does&lt;/em&gt; or &lt;em&gt;has&lt;/em&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Takeaway
&lt;/h2&gt;

&lt;p&gt;Both inheritance and composition have their place in good design. Inheritance gives you structure and identity, while composition gives you flexibility and adaptability. The most maintainable systems tend to combine both — using inheritance for stable abstractions and composition for dynamic behavior.&lt;/p&gt;

&lt;p&gt;In short:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Use inheritance for identity. Use composition for flexibility.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>designpatterns</category>
      <category>softwareengineering</category>
      <category>design</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Mindset while getting things done in software development</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Sat, 27 Jul 2024 16:13:22 +0000</pubDate>
      <link>https://forem.com/parthkamal/mindset-while-getting-things-done-in-software-development-1m2p</link>
      <guid>https://forem.com/parthkamal/mindset-while-getting-things-done-in-software-development-1m2p</guid>
      <description>&lt;p&gt;Absolutely, getting things done productively in software development is crucial for a fulfilling and stress-free life as a software engineer! Being able to execute tasks effortlessly, without any toll on your mental state, is key. Working productively will eliminate the possibility of stressful situations most of the time, and if they do come, you'll be better equipped to handle them.&lt;/p&gt;

&lt;p&gt;Understanding the context, let's get back to the main content.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Iterative Development Methodology&lt;/strong&gt;: Yeah, it may seem like a big complex term. Let's break it down. It simply means doing the simple small things. Obviously, they should be working. Just spend time on making an atomic function (let's say summing two numbers) which will be the building block of your overall goals/task/feature, etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Improve the quality of the code over time. Improve the quality of your code by continuous iteration of code refactoring.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reusable Functions (Paradigm of Functional Programming)&lt;/strong&gt;: Now after creating an atomic function, we are heading into developing bigger blocks!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Complications of Overdesign&lt;/strong&gt;: Never try to overdesign before doing anything! People fantasize about getting all the design and predicting all the cases in one hand (design the whole thing before developing one). It can never be perfect. Making a barebone design (working) solution and improving/modifying it through iterative development is the key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DRY (Principle)&lt;/strong&gt;: Understanding the concepts and really applying them (through try and test) is crucial. What I mean by this is that developing the code really involves working in the present. You should be able to analyze the code continuously and observe the possibility of repetition. As soon as you see it, make it scalable (yeah, I mean the design/paradigm, like object-oriented, functional—whatever the case, depends on your choice). If nothing comes to mind, make a reusable function out of that.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Importance of Working Solution&lt;/strong&gt;: Working code is ultimately praised. A simple, working code is always praised. :)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>productivity</category>
      <category>software</category>
      <category>development</category>
    </item>
    <item>
      <title>Directory Structure : Selenium Automation</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Tue, 18 Jun 2024 03:47:11 +0000</pubDate>
      <link>https://forem.com/parthkamal/directory-structure-selenium-automation-52ic</link>
      <guid>https://forem.com/parthkamal/directory-structure-selenium-automation-52ic</guid>
      <description>&lt;p&gt;if you are using selenium webdriver , to write automation tests for your javascript application , tracing the requirement , and writing functional tests for it , may end up getting a lot of tests, and each set of tests, may become very difficult to manage, because we have to  write code for each ui interaction in each test. &lt;/p&gt;

&lt;p&gt;soon you will find that managing test code is in itself a big task. soon you will start finding ways to modularize tests in selenium . &lt;/p&gt;

&lt;p&gt;modularizing tests in selenium webdriver in javascript , involved organinizing your test code into reusable, maintainable and scalable components.&lt;/p&gt;

&lt;p&gt;some of the common practices which are used to modularize are; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; page object model - pom is a desing pattern that helsps to create an object repository for web ui elements. &lt;/li&gt;
&lt;li&gt;test data management - keep your test data separate from your test scripts. &lt;/li&gt;
&lt;li&gt;utility functions - for common actions across the whole ui lik , logging in , taking screenshots, or waiting for elements. &lt;/li&gt;
&lt;li&gt;test suites - here we will write the code for tests. &lt;/li&gt;
&lt;li&gt;configuration - config files to manage, the environment variables, global variables, and configuration, which are common across all the tests, for eg; base url. &lt;/li&gt;
&lt;li&gt;logging and reporting - integrating loggign and reporting to keep track of test execution and results. &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>automation</category>
      <category>testing</category>
      <category>javascript</category>
      <category>selenium</category>
    </item>
    <item>
      <title>C++ Best Practices : Naming Conventions</title>
      <dc:creator>Parth Kamal</dc:creator>
      <pubDate>Sun, 16 Jun 2024 19:17:23 +0000</pubDate>
      <link>https://forem.com/parthkamal/c-best-practices-naming-conventions-235m</link>
      <guid>https://forem.com/parthkamal/c-best-practices-naming-conventions-235m</guid>
      <description>&lt;p&gt;There are various popular naming conventions which are common among various languages, the relevance of these conventions comes from , the legacy which they hold and other factors like readability, purpose, programming cababilities, for example , we most follow camelcase for java, for c++ which comes with great capabilities, we use lowercase with underscores, most of the time. &lt;/p&gt;

&lt;h2&gt;
  
  
  why important ?
&lt;/h2&gt;

&lt;p&gt;there are two main advantage which comes with this, &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;consistency in the code base - especially you are contributing open source. &lt;/li&gt;
&lt;li&gt;readability - with common coding style and following it becomes easy for you to understand the code. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  C++ naming convention -
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Variables - lowercase with underscores, &lt;/li&gt;
&lt;li&gt;Functions - lower case with underscores, &lt;/li&gt;
&lt;li&gt;Classes - Pascal case&lt;/li&gt;
&lt;li&gt;Constants - uppercase with underscores. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In gist : following the best practices , and guidelines, make the job   easier for everyone. &lt;/p&gt;

</description>
      <category>cpp</category>
      <category>convention</category>
      <category>language</category>
    </item>
  </channel>
</rss>
