<?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>DEV Community: Syed Yakoob Shiraz</title>
    <description>The latest articles on DEV Community by Syed Yakoob Shiraz (@syedyshiraz).</description>
    <link>https://dev.to/syedyshiraz</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%2F3062220%2Ff41f7bd7-3d6e-4fad-9601-9329bdb4dc19.jpeg</url>
      <title>DEV Community: Syed Yakoob Shiraz</title>
      <link>https://dev.to/syedyshiraz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/syedyshiraz"/>
    <language>en</language>
    <item>
      <title>Adapter Pattern – The Translator Of Code</title>
      <dc:creator>Syed Yakoob Shiraz</dc:creator>
      <pubDate>Thu, 29 May 2025 17:23:11 +0000</pubDate>
      <link>https://dev.to/syedyshiraz/adapter-pattern-the-translator-of-code-3d0d</link>
      <guid>https://dev.to/syedyshiraz/adapter-pattern-the-translator-of-code-3d0d</guid>
      <description>&lt;h2&gt;
  
  
  📘 Why This Blog?
&lt;/h2&gt;

&lt;p&gt;I once tried plugging a new payment system into my existing wallet app.&lt;/p&gt;

&lt;p&gt;Sounds simple, right?&lt;/p&gt;

&lt;p&gt;Except the new payment API looked nothing like the one I already had.&lt;/p&gt;

&lt;p&gt;I ended up rewriting half my code just to make it &lt;em&gt;fit&lt;/em&gt;. 🤯&lt;/p&gt;

&lt;p&gt;That’s when the &lt;strong&gt;Adapter Pattern&lt;/strong&gt; walked in—like a translator between two worlds.&lt;/p&gt;

&lt;p&gt;This blog is my no-fluff, real-world breakdown of the Adapter Pattern—the way I wish someone had explained it when I was pulling my hair out.&lt;/p&gt;




&lt;h2&gt;
  
  
  📖 What is the Adapter Pattern?
&lt;/h2&gt;

&lt;p&gt;The Adapter Pattern lets you make &lt;em&gt;incompatible interfaces&lt;/em&gt; work together.&lt;/p&gt;

&lt;p&gt;In simple terms: You wrap one class inside another to make it “look like” what your code expects.&lt;/p&gt;

&lt;p&gt;Like an electric plug adapter—it doesn’t change the electricity, just the shape of the socket.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 Spotting an Adapter
&lt;/h2&gt;

&lt;p&gt;You're probably in Adapter territory if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You’re integrating with an external API or legacy system&lt;/li&gt;
&lt;li&gt;You find yourself writing conversion code over and over&lt;/li&gt;
&lt;li&gt;Your new class just won’t “fit” the interface your system expects&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧪 Code Example – Payment Gateway Adapter
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: The Expected Interface
&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;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IPaymentProcessor&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;pay&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;amount&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;
  
  
  Step 2: An Existing Payment Gateway (Incompatible)
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FancyPaySDK&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;makeTransaction&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;valueInDollars&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;"Transaction of $"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;valueInDollars&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" done via FancyPay"&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;
  
  
  Step 3: The Adapter
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FancyPayAdapter&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;IPaymentProcessor&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;final&lt;/span&gt; &lt;span class="nc"&gt;FancyPaySDK&lt;/span&gt; &lt;span class="n"&gt;fancyPay&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;FancyPayAdapter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FancyPaySDK&lt;/span&gt; &lt;span class="n"&gt;fancyPay&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;fancyPay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fancyPay&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Assume 1 unit = 1 dollar for simplicity&lt;/span&gt;
        &lt;span class="n"&gt;fancyPay&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makeTransaction&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&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;
  
  
  Step 4: Client Code
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Checkout&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;final&lt;/span&gt; &lt;span class="nc"&gt;IPaymentProcessor&lt;/span&gt; &lt;span class="n"&gt;processor&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;Checkout&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;IPaymentProcessor&lt;/span&gt; &lt;span class="n"&gt;processor&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;processor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;processor&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;completeOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;processor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pay&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&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;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;FancyPaySDK&lt;/span&gt; &lt;span class="n"&gt;fancyPay&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;FancyPaySDK&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;IPaymentProcessor&lt;/span&gt; &lt;span class="n"&gt;adapter&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;FancyPayAdapter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fancyPay&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;Checkout&lt;/span&gt; &lt;span class="n"&gt;checkout&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;Checkout&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;checkout&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;completeOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// $100 via FancyPay&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;h2&gt;
  
  
  ⚠ Important Notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You’re not changing &lt;code&gt;FancyPaySDK&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You're wrapping it in an Adapter to fit the &lt;code&gt;IPaymentProcessor&lt;/code&gt; interface&lt;/li&gt;
&lt;li&gt;This keeps your client code clean and decoupled&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🤔 When to Use Adapter
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Integrating with legacy systems or third-party libraries&lt;/li&gt;
&lt;li&gt;Making two interfaces compatible without modifying either&lt;/li&gt;
&lt;li&gt;Bridging between new and old code in large systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Helps reuse existing code without modifying it&lt;/li&gt;
&lt;li&gt;Promotes interface-driven design&lt;/li&gt;
&lt;li&gt;Makes integration painless and clean&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ❗ Caution
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Too many adapters can lead to complexity and confusion&lt;/li&gt;
&lt;li&gt;If you're writing lots of tiny adapters, rethink your abstractions&lt;/li&gt;
&lt;li&gt;Avoid adapting things that should be redesigned instead&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔗 More Examples
&lt;/h2&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/syedyshiraz/DesignPatterns/tree/main/StructuralDesignPatterns/AdapterDesignPattern" rel="noopener noreferrer"&gt;Adapter Pattern on GitHub&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🥵 TL;DR
&lt;/h2&gt;

&lt;p&gt;The Adapter Pattern is like a plug converter.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“I’ll rewrite my system to match this new class…”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“I’ll wrap it so it fits my system.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Adapt. Don’t force. Write clean bridges between old and new.&lt;/p&gt;

&lt;p&gt;Be the Integrator, not the Imitator. ⚙️&lt;/p&gt;

&lt;p&gt;Made with ❤️ by &lt;a href="https://github.com/syedyshiraz" rel="noopener noreferrer"&gt;syedyshiraZ&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Abstract Factory Pattern – A DevBlog You’ll Actually Read</title>
      <dc:creator>Syed Yakoob Shiraz</dc:creator>
      <pubDate>Fri, 23 May 2025 10:56:03 +0000</pubDate>
      <link>https://dev.to/syedyshiraz/abstract-factory-pattern-a-devblog-youll-actually-read-21n2</link>
      <guid>https://dev.to/syedyshiraz/abstract-factory-pattern-a-devblog-youll-actually-read-21n2</guid>
      <description>&lt;h2&gt;
  
  
  📘 Why This Blog?
&lt;/h2&gt;

&lt;p&gt;I used to think switching UIs was just about &lt;code&gt;if-else&lt;/code&gt;:&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="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;equals&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="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="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MacButton&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But soon I had checkboxes, sliders, dropdowns—dozens of components.&lt;/p&gt;

&lt;p&gt;My code turned into an &lt;code&gt;if-else&lt;/code&gt; jungle 🌴.&lt;/p&gt;

&lt;p&gt;That’s when the &lt;strong&gt;Abstract Factory Pattern&lt;/strong&gt; showed up—like a design pattern with a machete.&lt;/p&gt;

&lt;p&gt;This blog is my no-BS, real-world explanation of Abstract Factory—the way I wish someone had explained it.&lt;/p&gt;




&lt;h2&gt;
  
  
  📖 What is the Abstract Factory Pattern?
&lt;/h2&gt;

&lt;p&gt;The Abstract Factory Pattern lets you create families of related objects &lt;em&gt;without specifying their concrete classes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In simpler terms:&lt;br&gt;
You ask a &lt;strong&gt;factory&lt;/strong&gt; to give you &lt;strong&gt;related things&lt;/strong&gt; that all work well &lt;strong&gt;together&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You don’t say &lt;em&gt;how&lt;/em&gt; they’re made—you just get what you need.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 Spotting an Abstract Factory
&lt;/h2&gt;

&lt;p&gt;You’re likely looking at an Abstract Factory if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to switch between families of related objects (like Mac vs Windows UI)&lt;/li&gt;
&lt;li&gt;You find lots of &lt;code&gt;new SomeClass()&lt;/code&gt; scattered around&lt;/li&gt;
&lt;li&gt;Your components break when you swap themes or platforms&lt;/li&gt;
&lt;li&gt;You hear people whispering “cross-platform UI kit” or “theme engine”&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧪 Code Example – Cross-Platform UI Factory
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Define Product Interfaces
&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;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IButton&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;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ICheckbox&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;check&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;
  
  
  Step 2: Create Concrete Products
&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;public&lt;/span&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;IButton&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 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;public&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;IButton&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 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;public&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;ICheckbox&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;check&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;"Checking 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;public&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;ICheckbox&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;check&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;"Checking 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;h3&gt;
  
  
  Step 3: Abstract Factory Interface
&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;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IUIFactory&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;IButton&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;ICheckbox&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;h3&gt;
  
  
  Step 4: Concrete Factories
&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;public&lt;/span&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;IUIFactory&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;IButton&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="nf"&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;ICheckbox&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="nf"&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;public&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;IUIFactory&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;IButton&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="nf"&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;ICheckbox&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="nf"&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;h3&gt;
  
  
  Step 5: The Client Code
&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;public&lt;/span&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="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;IButton&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="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ICheckbox&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;IUIFactory&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&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;renderUI&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;render&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;check&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;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;IUIFactory&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="c1"&gt;// Or new MacFactory()&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;renderUI&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;h2&gt;
  
  
  ⚠ Important Notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You’re not hardcoding specific classes like &lt;code&gt;new MacButton()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You’re using factories to &lt;em&gt;inject&lt;/em&gt; dependencies&lt;/li&gt;
&lt;li&gt;This makes it &lt;strong&gt;easy to switch families of products&lt;/strong&gt; (like themes, OS, DB drivers)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧐 When to Use Abstract Factory
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cross-platform toolkits (JavaFX, Qt, etc.)&lt;/li&gt;
&lt;li&gt;Theme engines for apps or games&lt;/li&gt;
&lt;li&gt;When you want to isolate object creation from usage&lt;/li&gt;
&lt;li&gt;When products must work &lt;em&gt;together as a family&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Groups related objects into families&lt;/li&gt;
&lt;li&gt;Easy to swap out one product family for another&lt;/li&gt;
&lt;li&gt;Decouples client code from specific classes&lt;/li&gt;
&lt;li&gt;Encourages consistency across related components&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ❗ Caution
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Can lead to lots of boilerplate if not used carefully&lt;/li&gt;
&lt;li&gt;Too many abstract layers can make debugging painful&lt;/li&gt;
&lt;li&gt;Overkill for simple cases—use only when families of products are &lt;em&gt;really&lt;/em&gt; needed&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔗 More Examples
&lt;/h2&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/syedyshiraz/DesignPatterns/tree/main/CreationalDesignPatterns/AbstractFactoryDesignPattern" rel="noopener noreferrer"&gt;Abstract Factory Pattern on GitHub&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🧵 TL;DR
&lt;/h2&gt;

&lt;p&gt;Abstract Factory is like asking a bakery for a matching set: cake + icing + box—without baking it yourself.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“new WindowsButton, new MacCheckbox...”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Give me a Mac UI Kit.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Use factories. Get consistent families. Write cleaner code.&lt;/p&gt;

&lt;p&gt;Be the Architect, not the Assembler. 🏗️&lt;/p&gt;

&lt;p&gt;Made with ❤️ by &lt;a href="https://github.com/syedyshiraz" rel="noopener noreferrer"&gt;syedyshiraz&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>designpatterns</category>
      <category>abstractfactorypattern</category>
    </item>
    <item>
      <title>Understanding the Prototype Pattern – Ctrl+C the Right Way</title>
      <dc:creator>Syed Yakoob Shiraz</dc:creator>
      <pubDate>Sat, 17 May 2025 16:20:06 +0000</pubDate>
      <link>https://dev.to/syedyshiraz/understanding-the-prototype-pattern-ctrlc-the-right-way-50hc</link>
      <guid>https://dev.to/syedyshiraz/understanding-the-prototype-pattern-ctrlc-the-right-way-50hc</guid>
      <description>&lt;p&gt;📘 &lt;strong&gt;Why This Blog?&lt;/strong&gt;&lt;br&gt;
I used to think cloning objects was just a matter of writing &lt;code&gt;new SomeObject(existingObj)&lt;/code&gt;. But then came a monster object—with nested data, configurations, and legacy complexity.&lt;br&gt;
I realized: &lt;strong&gt;"Deep copying this thing feels like surgery!"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That’s when the &lt;strong&gt;Prototype Design Pattern&lt;/strong&gt; came to the rescue.&lt;/p&gt;

&lt;p&gt;This devblog is my simple, no-jargon guide to the Prototype Pattern—the way I wish I had learned it.&lt;/p&gt;




&lt;p&gt;📖 &lt;strong&gt;What is the Prototype Pattern?&lt;/strong&gt;&lt;br&gt;
The &lt;strong&gt;Prototype Pattern&lt;/strong&gt; lets you create new objects by cloning existing ones—instead of creating them from scratch.&lt;/p&gt;

&lt;p&gt;In simple terms: &lt;strong&gt;You don’t build it, you copy it.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;🔍 &lt;strong&gt;Spotting a Prototype Pattern&lt;/strong&gt;&lt;br&gt;
You’re probably looking at a Prototype Pattern if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your object creation is &lt;strong&gt;expensive or complex&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You want to &lt;strong&gt;avoid reinitializing an object&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You have a method like &lt;code&gt;clone()&lt;/code&gt; or &lt;code&gt;copy()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You hear developers mumble things like “deep copy” or “shallow copy”&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🧪 &lt;strong&gt;Code Example – Shape Cloning System&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: The Prototype Interface
&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;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IShape&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Cloneable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Shape&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Concrete Prototypes
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;IShape&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;radius&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;color&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;Circle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;radius&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;color&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;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;radius&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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;color&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;Circle&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;Circle&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;radius&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;color&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;draw&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;"Drawing a "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" circle with radius "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;radius&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;
  
  
  Step 3: Using the Prototype
&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;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;Circle&lt;/span&gt; &lt;span class="n"&gt;original&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;Circle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Red"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="n"&gt;clone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;original&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="n"&gt;original&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;draw&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Drawing a Red circle with radius 10&lt;/span&gt;
        &lt;span class="n"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;draw&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;     &lt;span class="c1"&gt;// Output: Drawing a Red circle with radius 10&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;"Same object? "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;clone&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// false&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;⚠ &lt;strong&gt;Important Notes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;clone()&lt;/code&gt; should return a &lt;strong&gt;new object&lt;/strong&gt;, not the same reference.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;deep copy&lt;/strong&gt; if your object has nested objects or mutable fields.&lt;/li&gt;
&lt;li&gt;You can implement &lt;code&gt;Cloneable&lt;/code&gt; in Java, but it’s often better to write your own &lt;code&gt;clone()&lt;/code&gt; method—it gives you full control.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🧐 &lt;strong&gt;When to Use Prototype Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need many similar objects, like enemies in a game, form fields, or settings templates.&lt;/li&gt;
&lt;li&gt;Object creation is &lt;strong&gt;performance-heavy&lt;/strong&gt; (e.g., parsing configs, reading files).&lt;/li&gt;
&lt;li&gt;You want to &lt;strong&gt;abstract the creation process&lt;/strong&gt; from the user.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;✅ &lt;strong&gt;Benefits&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Saves time and resources by cloning instead of rebuilding&lt;/li&gt;
&lt;li&gt;Decouples the client code from the class constructors&lt;/li&gt;
&lt;li&gt;Handy when working with complex or legacy classes&lt;/li&gt;
&lt;li&gt;You can register and clone pre-configured "prototypes"&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;❗ &lt;strong&gt;Caution&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Be careful with &lt;strong&gt;deep vs shallow copy&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Overusing prototypes may lead to subtle bugs if internal state is shared incorrectly&lt;/li&gt;
&lt;li&gt;Java’s built-in &lt;code&gt;Object.clone()&lt;/code&gt; is broken for many use cases—prefer custom logic&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🔗 &lt;strong&gt;More Examples&lt;/strong&gt;&lt;br&gt;
👉 &lt;a href="https://github.com/syedyshiraz/DesignPatterns/tree/main/CreationalDesignPatterns/PrototypeDesignPattern" rel="noopener noreferrer"&gt;Prototype Pattern on GitHub&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;🧵 &lt;strong&gt;TL;DR&lt;/strong&gt;&lt;br&gt;
The Prototype Pattern is all about &lt;strong&gt;cloning smartly&lt;/strong&gt;.&lt;br&gt;
Instead of creating new objects from zero, you copy an existing one and tweak it if needed.&lt;/p&gt;

&lt;p&gt;It’s like using "Save As" in MS Word—you get the same thing, now ready for a new purpose.&lt;/p&gt;

&lt;p&gt;Copy smart. Clone clean. Be the Prototype Jedi. 🌟&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Made with ❤️ by syedyshiraz&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>designpatterns</category>
      <category>prototypedesignpattern</category>
    </item>
    <item>
      <title>Build a Version-Controlled Second Brain with Joplin and GitHub</title>
      <dc:creator>Syed Yakoob Shiraz</dc:creator>
      <pubDate>Sun, 11 May 2025 17:52:57 +0000</pubDate>
      <link>https://dev.to/syedyshiraz/build-a-version-controlled-second-brain-with-joplin-and-github-2ci3</link>
      <guid>https://dev.to/syedyshiraz/build-a-version-controlled-second-brain-with-joplin-and-github-2ci3</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Take notes in Markdown. Save them automatically to Git. Back them up like a developer.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Every developer needs a place to store notes — ideas, docs, bug fixes, commands, bookmarks, and those elusive "read later" links. But what if your note-taking app worked like GitHub?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Joplin&lt;/strong&gt; is a privacy-first, open-source note-taking app built for power users. And with one plugin, you can turn it into a Git-backed knowledge base — syncing your notes as Markdown files with version control.&lt;/p&gt;

&lt;p&gt;In this article, we’ll show you how to set up Joplin, install the Git sync plugin, and connect it to GitHub — step by step.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Install Joplin
&lt;/h2&gt;

&lt;p&gt;Joplin is available for Windows, macOS, and Linux.&lt;/p&gt;

&lt;h3&gt;
  
  
  🔗 Download:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Visit: &lt;a href="https://joplinapp.org" rel="noopener noreferrer"&gt;https://joplinapp.org&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Choose your platform and install the desktop app.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Joplin also has mobile apps and a browser web clipper — but we’ll focus on the &lt;strong&gt;desktop app&lt;/strong&gt; here.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 2: Install the "Save Note as Markdown and Commit to Git" Plugin
&lt;/h2&gt;

&lt;p&gt;This plugin automatically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Saves each note as a Markdown file&lt;/li&gt;
&lt;li&gt;Commits it to a Git repo with a timestamped message&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🛠 How to Install:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open Joplin.&lt;/li&gt;
&lt;li&gt;Go to &lt;code&gt;Tools &amp;gt; Options &amp;gt; Plugins&lt;/code&gt; (on &lt;strong&gt;Windows/Linux&lt;/strong&gt;)
or &lt;code&gt;Joplin &amp;gt; Settings &amp;gt; Plugins&lt;/code&gt; (on &lt;strong&gt;macOS&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;“Search”&lt;/strong&gt; and type:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Save Note as Markdown and Commit to Git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Click &lt;strong&gt;Install&lt;/strong&gt; next to the plugin.&lt;/li&gt;
&lt;li&gt;Restart Joplin when prompted.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🔗 Plugin Page: &lt;a href="https://joplinapp.org/plugins/plugin/io.sagaii.export2git/" rel="noopener noreferrer"&gt;https://joplinapp.org/plugins/plugin/io.sagaii.export2git/&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Create a GitHub Repository
&lt;/h2&gt;

&lt;p&gt;Before we push your notes, let’s create a GitHub repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  🖥️ How to Create a GitHub Repo:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Log in to your GitHub account.&lt;/li&gt;
&lt;li&gt;Click the &lt;strong&gt;"+"&lt;/strong&gt; icon in the top right corner, and select &lt;strong&gt;"New repository"&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Give your repository a name, like &lt;code&gt;joplin-notes&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set it to &lt;strong&gt;Public&lt;/strong&gt; or &lt;strong&gt;Private&lt;/strong&gt; as per your preference.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do not&lt;/strong&gt; initialize the repository with a README or .gitignore file, as you will be pushing your local notes.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create repository&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 4: Configure the Plugin – Set Up Local Git Repo
&lt;/h2&gt;

&lt;p&gt;Now that you have your GitHub repository, let’s configure Joplin to sync with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  🧭 Where to Find the Plugin Settings
&lt;/h3&gt;

&lt;h4&gt;
  
  
  On &lt;strong&gt;Windows/Linux&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Tools &amp;gt; Options &amp;gt; Save Note as Markdown and Commit to Git&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  On &lt;strong&gt;macOS&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Joplin &amp;gt; Settings &amp;gt; Save Note as Markdown and Commit to Git&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  📁 Set the Local GitRepos Folder Path
&lt;/h3&gt;

&lt;p&gt;In the plugin settings, set:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Local GitRepos Folder Path
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To something like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;macOS/Linux: &lt;code&gt;/Users/yourname/JoplinGitNotes&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Windows: &lt;code&gt;C:\Users\yourname\Documents\JoplinGitNotes&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the folder doesn’t exist yet, create it manually.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optional Recommended Settings
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Recommended Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Auto Commit on Save&lt;/td&gt;
&lt;td&gt;✅ Enabled&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Commit Message Template&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Update: {{noteTitle}} at {{date}}&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Author Name / Email&lt;/td&gt;
&lt;td&gt;Your Git identity&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Step 5: Push Your Local Git Repo to GitHub
&lt;/h2&gt;

&lt;p&gt;Once you've set up your local folder and initialized Git, let’s connect it to GitHub.&lt;/p&gt;

&lt;h3&gt;
  
  
  🖥️ Connect Your Local Repo to GitHub
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open your terminal or Git Bash.&lt;/li&gt;
&lt;li&gt;Navigate to your local Joplin Git folder:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;cd&lt;/span&gt; /path/to/JoplinGitNotes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Initialize the Git repository:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add the remote GitHub repository:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git remote add origin https://github.com/your-username/joplin-notes.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add all files to Git:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Commit the files:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Initial commit"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Push to GitHub:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git push &lt;span class="nt"&gt;-u&lt;/span&gt; origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, your local Git repo is connected to GitHub, and your notes are backed up!&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 6: Export and Commit Notes to GitHub with One Click
&lt;/h2&gt;

&lt;p&gt;Once your folder is set and Git is initialized, pushing notes is simple.&lt;/p&gt;

&lt;h3&gt;
  
  
  🚀 Exporting a Note
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Select the note in Joplin.&lt;/li&gt;
&lt;li&gt;Click the &lt;strong&gt;“Export as Markdown and Commit to Git”&lt;/strong&gt; button on the toolbar.

&lt;ul&gt;
&lt;li&gt;It may look like a file with a Git icon.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rqjdn21sccxvt7da3tb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rqjdn21sccxvt7da3tb.png" alt="Export as markdown and commit to git button" width="800" height="134"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The plugin:

&lt;ul&gt;
&lt;li&gt;Saves the note as a &lt;code&gt;.md&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Commits it using your configured message&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqqhymnwa648hniapvsv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqqhymnwa648hniapvsv.png" alt="github image" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  🟡 Manually Push to GitHub
&lt;/h3&gt;

&lt;p&gt;If this is your first time, initialize and push:&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;cd&lt;/span&gt; /path/to/JoplinGitNotes
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  (Optional) Step 7: Automate Git Push
&lt;/h2&gt;

&lt;p&gt;You can push changes on a schedule.&lt;/p&gt;

&lt;h3&gt;
  
  
  On macOS/Linux:
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;crontab -e&lt;/code&gt; to add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;0 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; /path/to/JoplinGitNotes &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  On Windows:
&lt;/h3&gt;

&lt;p&gt;Use Task Scheduler or a &lt;code&gt;.bat&lt;/code&gt; script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd C:\path\to\JoplinGitNotes
git push
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;With Joplin and this plugin, you now have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A local-first, Markdown-based note system&lt;/li&gt;
&lt;li&gt;Git version control for tracking every update&lt;/li&gt;
&lt;li&gt;Optional cloud sync with GitHub for backup and access anywhere&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You’ve just built your own &lt;strong&gt;version-controlled second brain&lt;/strong&gt; — and it runs on free, open tools.&lt;/p&gt;

&lt;p&gt;Happy documenting! 🧠 💾&lt;/p&gt;

&lt;p&gt;
  Made with ❤️ by &lt;a href="https://github.com/syedyshiraz" rel="noopener noreferrer"&gt;syedyshiraZ&lt;/a&gt;
&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding the Singleton Pattern – One Instance to Rule Them All</title>
      <dc:creator>Syed Yakoob Shiraz</dc:creator>
      <pubDate>Sun, 11 May 2025 05:35:24 +0000</pubDate>
      <link>https://dev.to/syedyshiraz/understanding-the-singleton-pattern-one-instance-to-rule-them-all-2f1k</link>
      <guid>https://dev.to/syedyshiraz/understanding-the-singleton-pattern-one-instance-to-rule-them-all-2f1k</guid>
      <description>&lt;h2&gt;
  
  
  📘 Why This Blog?
&lt;/h2&gt;

&lt;p&gt;When I first encountered the Singleton Design Pattern, I thought, &lt;em&gt;"Why do we need this? Can’t we just use a static class?"&lt;/em&gt;&lt;br&gt;
It felt like extra ceremony for something simple—until I built a multithreaded logging utility and saw chaos unfold.&lt;/p&gt;

&lt;p&gt;That’s when the Singleton Pattern &lt;em&gt;clicked&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This devblog is my honest take on helping you understand the Singleton Pattern—the way I wish someone had explained it to me.&lt;/p&gt;




&lt;h2&gt;
  
  
  📖 What is the Singleton Pattern?
&lt;/h2&gt;

&lt;p&gt;The Singleton Pattern ensures that a class has &lt;strong&gt;only one instance&lt;/strong&gt; throughout the application lifecycle—and provides a global point of access to it.&lt;/p&gt;

&lt;p&gt;In simple terms: &lt;em&gt;You create an object once and reuse it everywhere.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 Spotting a Singleton Pattern
&lt;/h2&gt;

&lt;p&gt;You might be looking at a Singleton Pattern if you see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class with a &lt;strong&gt;private constructor&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;static method&lt;/strong&gt; like &lt;code&gt;getInstance()&lt;/code&gt; or &lt;code&gt;getLogger()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;static field&lt;/strong&gt; that holds a single instance&lt;/li&gt;
&lt;li&gt;Code like:
&lt;code&gt;Logger logger = Logger.getInstance();&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧪 Code Example – Logger Singleton
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Trivial Singleton (Not Thread-Safe)
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Logger&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;Logger&lt;/span&gt; &lt;span class="n"&gt;instance&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;Logger&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;"Logger initialized!"&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;Logger&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&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;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;instance&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;Logger&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ⚠ Not thread-safe!&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;instance&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;log&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;message&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;"[LOG] "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;message&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;⚠ Drawback: This implementation is not safe in multithreaded environments. If two threads call getInstance() at the same time when instance is null, they might both create separate instances—breaking the singleton guarantee.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 2: Thread-Safe Singleton (Synchronized)
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Logger&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;Logger&lt;/span&gt; &lt;span class="n"&gt;instance&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;Logger&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;synchronized&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&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;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;instance&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;Logger&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;instance&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;log&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;message&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;"[LOG] "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;message&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;⚠ Drawback: While this approach is thread-safe, the synchronized keyword makes the method slower because every call to getInstance() acquires a lock—even after the instance is initialized. This can cause performance bottlenecks in high-concurrency applications.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 3: Optimized Singleton (Double-Checked Locking)
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Logger&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="kd"&gt;volatile&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt; &lt;span class="n"&gt;instance&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;Logger&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;Logger&lt;/span&gt; &lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&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;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&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;instance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;instance&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;Logger&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ✅ Thread-safe and efficient&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&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;log&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;message&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;"[LOG] "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;message&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;
  
  
  Step 4: Use the Singleton in Your Code
&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;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="c1"&gt;// Creating multiple threads to simulate race condition&lt;/span&gt;
        &lt;span class="nc"&gt;Runnable&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Logger&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;log&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Logging from thread: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&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="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread1&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Thread-1"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="n"&gt;thread2&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;Thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Thread-2"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;thread1&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="n"&gt;thread2&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="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚠ Warning: If you're using the trivial (non-thread-safe) Singleton, running this code may lead to two separate instances being created, violating the Singleton principle. This showcases why thread safety is crucial in multi-threaded environments.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Personal Notes to Apply the Singleton Pattern
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Always make the constructor &lt;code&gt;private&lt;/code&gt; to prevent direct instantiation.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;volatile&lt;/code&gt; for the instance when applying double-checked locking.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;synchronized&lt;/code&gt; wisely—only when needed.&lt;/li&gt;
&lt;li&gt;Singleton is a &lt;em&gt;global resource&lt;/em&gt;—use it for shared services like logging, configuration, or caching.&lt;/li&gt;
&lt;li&gt;Avoid abusing it as a workaround for dependency injection.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Guarantees a single instance of a class&lt;/li&gt;
&lt;li&gt;Saves memory for expensive or shared resources&lt;/li&gt;
&lt;li&gt;Ensures consistent state across the application&lt;/li&gt;
&lt;li&gt;Great for logging, configuration managers, or connection pools&lt;/li&gt;
&lt;li&gt;Easily accessible from anywhere in your codebase&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ❗ Caution
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Overusing Singleton can lead to &lt;strong&gt;hidden dependencies&lt;/strong&gt; and &lt;strong&gt;tight coupling&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Makes &lt;strong&gt;unit testing harder&lt;/strong&gt; due to global state&lt;/li&gt;
&lt;li&gt;Avoid using it for data classes or business logic that needs multiple instances&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔗 More Examples
&lt;/h2&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/syedyshiraz/DesignPatterns/tree/main/CreationalDesignPatterns/SingletonDesignPattern" rel="noopener noreferrer"&gt;Singleton Design Pattern on GitHub&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🧵 TL;DR
&lt;/h2&gt;

&lt;p&gt;The Singleton Pattern is your go-to when you need &lt;strong&gt;just one object&lt;/strong&gt;—and you want it safely shared across the system.&lt;/p&gt;

&lt;p&gt;Start simple, go thread-safe, and optimize with double-checked locking if performance matters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One instance to rule them all.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Made with ❤️ by syedyshiraz&lt;/p&gt;

</description>
      <category>java</category>
      <category>designpatterns</category>
      <category>singleton</category>
    </item>
    <item>
      <title>Understanding the Builder Pattern – One Brick at a Time</title>
      <dc:creator>Syed Yakoob Shiraz</dc:creator>
      <pubDate>Mon, 05 May 2025 18:05:55 +0000</pubDate>
      <link>https://dev.to/syedyshiraz/understanding-the-builder-pattern-one-brick-at-a-time-ffm</link>
      <guid>https://dev.to/syedyshiraz/understanding-the-builder-pattern-one-brick-at-a-time-ffm</guid>
      <description>&lt;h2&gt;
  
  
  📘 Why This Blog?
&lt;/h2&gt;

&lt;p&gt;When I first encountered the Builder Design Pattern, it felt over-engineered—like too many classes just to build an object!&lt;br&gt;
But then I worked on a project where constructors became monstrous, with too many parameters, many of them optional. That’s when the Builder Pattern &lt;em&gt;clicked&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This devblog is my honest take on helping you understand the Builder Pattern—the way I wish someone had explained it to me.&lt;/p&gt;




&lt;h2&gt;
  
  
  📖 What is the Builder Pattern?
&lt;/h2&gt;

&lt;p&gt;The Builder Pattern helps construct complex objects step by step.&lt;br&gt;
Instead of stuffing a constructor with dozens of parameters (and trying to remember their order), the builder lets you build an object in a readable and flexible way.&lt;/p&gt;

&lt;p&gt;In simple terms: &lt;em&gt;You control the construction process, one method at a time.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🔍 Spotting a Builder Pattern
&lt;/h2&gt;

&lt;p&gt;You might be looking at a Builder Pattern if you see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class with many constructor parameters (some optional).&lt;/li&gt;
&lt;li&gt;Code like:
&lt;code&gt;IUser user = new User.Builder().setName("Alice").setAge(30).build();&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A nested static &lt;code&gt;Builder&lt;/code&gt; class or a separate builder class that returns the same object.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧪 Code Example – IUser Builder
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Create the Target Class
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;IUser&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;final&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="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&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;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;address&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;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;phone&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;User&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;name&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;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;age&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;age&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;address&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;address&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;phone&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;phone&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;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&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;address&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;phone&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;setName&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="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;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;setAge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&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;setAddress&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;address&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;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;address&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;setPhone&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;phone&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;phone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;phone&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;IUser&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;User&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="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printDetails&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;"User: "&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="s"&gt;", "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;age&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="n"&gt;address&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="n"&gt;phone&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;
  
  
  Step 2: Create the Interface
&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;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IUser&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;printDetails&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;
  
  
  Step 3: Use the Builder in Your Code
&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;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;IUser&lt;/span&gt; &lt;span class="n"&gt;user&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;User&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;setName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAddress&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"42 Wonderland Lane"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPhone&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"123-456-7890"&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="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printDetails&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;h2&gt;
  
  
  🧠 Personal Notes to Apply the Builder Pattern
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Start with:
&lt;code&gt;IUser user = new User.Builder().setName(...).build();&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Then define the builder inside your class or in a separate class.&lt;/li&gt;
&lt;li&gt;Use method chaining to allow clean, fluent-style code.&lt;/li&gt;
&lt;li&gt;Keep constructors private to force controlled creation via builder.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  ✅ Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Makes object creation readable and flexible&lt;/li&gt;
&lt;li&gt;Eliminates the need for telescoping constructors&lt;/li&gt;
&lt;li&gt;Avoids nulls and messy constructor overloads&lt;/li&gt;
&lt;li&gt;Immutability: you can build once and freeze the object&lt;/li&gt;
&lt;li&gt;Easy to maintain when more fields are added later&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔗 More Examples
&lt;/h2&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/syedyshiraz/DesignPatterns/tree/main/CreationalDesignPatterns/BuilderDesignPattern" rel="noopener noreferrer"&gt;Builder Design Pattern on GitHub&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🧵 TL;DR
&lt;/h2&gt;

&lt;p&gt;The Builder Pattern is your friend when constructors get bloated.&lt;br&gt;
It gives you &lt;strong&gt;control, readability&lt;/strong&gt;, and &lt;strong&gt;clean code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You don’t pass everything upfront—you build the object piece by piece.&lt;/p&gt;

&lt;p&gt;Made with ❤️ by syedyshiraz&lt;/p&gt;

</description>
      <category>java</category>
      <category>designpatterns</category>
      <category>development</category>
      <category>builderdesignpattern</category>
    </item>
    <item>
      <title>Factory Design Pattern – Creating Objects the Smart Way</title>
      <dc:creator>Syed Yakoob Shiraz</dc:creator>
      <pubDate>Wed, 23 Apr 2025 16:30:01 +0000</pubDate>
      <link>https://dev.to/syedyshiraz/factory-design-pattern-creating-objects-the-smart-way-1d44</link>
      <guid>https://dev.to/syedyshiraz/factory-design-pattern-creating-objects-the-smart-way-1d44</guid>
      <description>&lt;h2&gt;
  
  
  📘 Why This Blog?
&lt;/h2&gt;

&lt;p&gt;When I first came across the &lt;strong&gt;Factory Design Pattern&lt;/strong&gt;, it felt like magic. But with a bit of practical exploration, I realized it’s just a smart way to decouple object creation from usage.&lt;br&gt;
This devblog is my attempt to help you understand the &lt;strong&gt;Factory Pattern&lt;/strong&gt; the way I wish someone explained it to me.&lt;/p&gt;


&lt;h2&gt;
  
  
  📖 What is the Factory Pattern?
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Factory Pattern&lt;/strong&gt; provides a way to delegate the instantiation logic to a separate method or class. Instead of using &lt;code&gt;new&lt;/code&gt; in multiple places, we delegate it to a &lt;code&gt;Factory&lt;/code&gt; that knows how to create different objects based on some input.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In simple terms: &lt;strong&gt;You say what you want, not how to create it.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  🔍 Spotting a Factory Pattern
&lt;/h2&gt;

&lt;p&gt;You might be looking at a Factory Pattern if you see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code like: &lt;code&gt;IEnemy enemy = EnemyFactory.getEnemy("orc");&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;One interface or abstract class, but many concrete implementations.&lt;/li&gt;
&lt;li&gt;A centralized class responsible for returning the appropriate implementation.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  🧪 Code Example – Enemy Factory
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Step 1: Create an interface
&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;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IEnemy&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;attack&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;
  
  
  Step 2: Implement the interface in multiple classes
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Orc&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;IEnemy&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;attack&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;"Orc attacks with axe!"&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;Goblin&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;IEnemy&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;attack&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;"Goblin throws dagger!"&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;
  
  
  Step 3: Create the Factory Class
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EnemyFactory&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;IEnemy&lt;/span&gt; &lt;span class="nf"&gt;getEnemy&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;enemyType&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&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;enemyType&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;"orc"&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;Orc&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="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enemyType&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;"goblin"&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;Goblin&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Unknown enemy type"&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;
  
  
  Step 4: Use it in your code
&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Game&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;IEnemy&lt;/span&gt; &lt;span class="n"&gt;enemy1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EnemyFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEnemy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"orc"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;enemy1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;attack&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="nc"&gt;IEnemy&lt;/span&gt; &lt;span class="n"&gt;enemy2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EnemyFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEnemy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"goblin"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;enemy2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;attack&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;h2&gt;
  
  
  🧠 Personal Notes to Apply Factory Pattern
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Start with usage:&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnemy&lt;/span&gt; &lt;span class="n"&gt;enemy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EnemyFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEnemy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"orc"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then build everything around it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;code&gt;IEnemy&lt;/code&gt; interface&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;Orc&lt;/code&gt;, &lt;code&gt;Goblin&lt;/code&gt;, etc. classes that implement &lt;code&gt;IEnemy&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;EnemyFactory&lt;/code&gt; to return &lt;code&gt;IEnemy&lt;/code&gt; objects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep the logic of &lt;strong&gt;what to return&lt;/strong&gt; in one place – the factory.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Centralized object creation logic&lt;/li&gt;
&lt;li&gt;Clean, readable code&lt;/li&gt;
&lt;li&gt;Easy to add new object types&lt;/li&gt;
&lt;li&gt;Loose coupling between client and concrete classes&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔗 More Examples
&lt;/h2&gt;

&lt;p&gt;Check out more hands-on examples of the Factory Pattern here:&lt;br&gt;&lt;br&gt;
👉 &lt;a href="https://github.com/syedyshiraz/DesignPatterns/tree/main/CreationalDesignPatterns/FactoryDesignPattern" rel="noopener noreferrer"&gt;Factory Design Pattern on GitHub&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  🧵 TL;DR
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Factory Pattern is a smart way to delegate object creation.&lt;br&gt;&lt;br&gt;
You don’t worry about &lt;code&gt;new&lt;/code&gt; – just ask the factory for what you need.&lt;/p&gt;
&lt;/blockquote&gt;





&lt;p&gt;
  Made with ❤️ by &lt;a href="https://github.com/syedyshiraz" rel="noopener noreferrer"&gt;syedyshiraz&lt;/a&gt;
&lt;/p&gt;

</description>
      <category>java</category>
      <category>designpatterns</category>
      <category>factorydesignpattern</category>
      <category>development</category>
    </item>
    <item>
      <title>Starting with Design Patterns — Learning One Pattern at a Time</title>
      <dc:creator>Syed Yakoob Shiraz</dc:creator>
      <pubDate>Fri, 18 Apr 2025 09:49:01 +0000</pubDate>
      <link>https://dev.to/syedyshiraz/starting-with-design-patterns-learning-one-pattern-at-a-time-23mp</link>
      <guid>https://dev.to/syedyshiraz/starting-with-design-patterns-learning-one-pattern-at-a-time-23mp</guid>
      <description>&lt;h2&gt;
  
  
  Hey there! 👋
&lt;/h2&gt;

&lt;p&gt;I’ve recently started diving into design patterns, and I’ll be honest—I’m not an expert yet. But that’s exactly why I’m writing this. I want to understand design patterns better, and the best way I learn is by sharing what I learn.&lt;/p&gt;

&lt;p&gt;I aim to explore at least one new design pattern every day (or at the very least, every week). This blog will serve as a journal of that journey, and I hope it helps others who are starting out too.&lt;/p&gt;




&lt;h2&gt;
  
  
  💭 What Are Design Patterns?
&lt;/h2&gt;

&lt;p&gt;Design patterns are common solutions to common problems in software design. They aren’t code recipes you copy-paste, but rather, reusable ideas and structures for solving software challenges in a clean, proven way.&lt;/p&gt;

&lt;p&gt;They help you think better, design better, and communicate better as a developer.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Why Design Patterns Matter
&lt;/h2&gt;

&lt;p&gt;🧩 Help you solve problems in structured, reusable ways&lt;/p&gt;

&lt;p&gt;📚 Build a strong foundation in system design and architecture&lt;/p&gt;

&lt;p&gt;🤝 Make team discussions easier using shared vocabulary&lt;/p&gt;

&lt;p&gt;🚀 Improve your code quality, readability, and flexibility&lt;/p&gt;




&lt;h2&gt;
  
  
  📂 Categories of Design Patterns
&lt;/h2&gt;

&lt;p&gt;✅ Official "Gang of Four" (GoF) Categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Creational Patterns – Handle object creation&lt;br&gt;
Examples: Singleton, Factory, Builder, Prototype&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Structural Patterns – Handle object composition&lt;br&gt;
Examples: Adapter, Composite, Facade, Decorator&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Behavioral Patterns – Handle object interaction&lt;br&gt;
Examples: Observer, Strategy, Command, State&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;❗ Other Useful Categories (Not GoF):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Concurrency Patterns – Handle multi-threading and parallelism&lt;br&gt;
Examples: Thread Pool, Producer-Consumer, Read-Write&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Architectural Patterns – Handle high-level system structure&lt;br&gt;
Examples: MVC, Microservices, MVVM&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;📌 Join the Journey&lt;/p&gt;

&lt;p&gt;I’m building a collection of easy-to-understand Java examples for each pattern I learn, and I’d love for you to follow along.&lt;br&gt;
Check out the GitHub repo:&lt;br&gt;
👉 &lt;a href="//github.com/syedyshiraz/design-patterns"&gt;{github.com/syedyshiraz/design-patterns}&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Star it, fork it, and grow with me as I explore one pattern at a time.&lt;br&gt;
Let’s keep learning—one pattern, one problem, one post at a time.&lt;/p&gt;

&lt;p&gt;Syed.Y.Shiraz&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>java</category>
      <category>development</category>
    </item>
  </channel>
</rss>
