<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Bugged & Blessed]]></title><description><![CDATA[Full stack developer, Freelancer and a Person with a strong believe in hardwork and knowledge sharing.]]></description><link>https://blog.techwithaditya.in</link><generator>RSS for Node</generator><lastBuildDate>Sun, 10 May 2026 16:07:38 GMT</lastBuildDate><atom:link href="https://blog.techwithaditya.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[The Factory Pattern .]]></title><description><![CDATA[Have you ever found yourself writing too many new statements all over your code — creating objects manually every time you need them?
If yes, then you’ve probably faced this problem:

“Every time I add a new type of object, I need to change multiple ...]]></description><link>https://blog.techwithaditya.in/the-factory-pattern</link><guid isPermaLink="true">https://blog.techwithaditya.in/the-factory-pattern</guid><category><![CDATA[design patterns]]></category><category><![CDATA[System Design]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Sun, 26 Oct 2025 17:22:06 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1761203644244/64cd1b0d-22ea-4cc5-836a-1599e07fcb86.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-have-you-ever-found-yourself-writing-too-many-new-statements-all-over-your-code-creating-objects-manually-every-time-you-need-them">Have you ever found yourself writing <strong>too many</strong> <code>new</code> statements all over your code — creating objects manually every time you need them?</h3>
<p>If yes, then you’ve probably faced this problem:</p>
<blockquote>
<p>“Every time I add a new type of object, I need to change multiple places in the code. It becomes messy and tightly coupled.”</p>
</blockquote>
<p>This is a common pain point in software design. The <strong>Factory Design Pattern</strong> is here to fix that.</p>
<pre><code class="lang-java"><span class="hljs-comment">// Step 1: Common Interface</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Notification</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">send</span><span class="hljs-params">(String message)</span></span>;
}

<span class="hljs-comment">// Step 2: Concrete Classes</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EmailNotification</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Notification</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">send</span><span class="hljs-params">(String message)</span> </span>{
        System.out.println(<span class="hljs-string">"Sending EMAIL: "</span> + message);
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SMSNotification</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Notification</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">send</span><span class="hljs-params">(String message)</span> </span>{
        System.out.println(<span class="hljs-string">"Sending SMS: "</span> + message);
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PushNotification</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Notification</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">send</span><span class="hljs-params">(String message)</span> </span>{
        System.out.println(<span class="hljs-string">"Sending PUSH: "</span> + message);
    }
}

<span class="hljs-comment">// Step 3: Factory Class</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NotificationFactory</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Notification <span class="hljs-title">createNotification</span><span class="hljs-params">(String type)</span> </span>{
        <span class="hljs-keyword">if</span> (type == <span class="hljs-keyword">null</span> || type.isEmpty()) <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">switch</span> (type.toUpperCase()) {
            <span class="hljs-keyword">case</span> <span class="hljs-string">"EMAIL"</span>: <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> EmailNotification();
            <span class="hljs-keyword">case</span> <span class="hljs-string">"SMS"</span>: <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> SMSNotification();
            <span class="hljs-keyword">case</span> <span class="hljs-string">"PUSH"</span>: <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> PushNotification();
            <span class="hljs-keyword">default</span>: <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">"Unknown notification type "</span> + type);
        }
    }
}

<span class="hljs-comment">// Step 4: Client Code</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        Notification notification = NotificationFactory.createNotification(<span class="hljs-string">"EMAIL"</span>);
        notification.send(<span class="hljs-string">"Factory Pattern in Action!"</span>);
    }
}
</code></pre>
<h2 id="heading-what-just-happened">🧠 What Just Happened?</h2>
<p>Now, your <code>Main</code> or <code>NotificationService</code> doesn’t need to know <strong>how</strong> notifications are created — it just <strong>asks the factory</strong> for one.</p>
<p>If tomorrow you add <code>WhatsAppNotification</code>, you just update the <strong>factory</strong>, not every place in your app.</p>
<h1 id="heading-important-for-the-spring-boot-developer"><mark>Important for the Spring Boot developer</mark></h1>
<h2 id="heading-your-question">💬 Your Question:</h2>
<blockquote>
<p>“If we’re using frameworks like Spring, then we don’t face the problem of using <code>new</code> everywhere. So, do we still need the Factory Pattern?”</p>
</blockquote>
<p>Let’s unpack this clearly — because this is <em>exactly</em> where most developers get confused when learning design patterns in the age of frameworks like <strong>Spring Boot</strong>.</p>
<p>Spring itself <em>already uses</em> the Factory Pattern internally — that’s why <em>you</em> don’t have to use <code>new</code>.<br />But understanding the Factory Pattern is still essential — because Spring’s entire dependency injection system is <em>built on top of it.</em></p>
<h2 id="heading-without-spring-manual-object-creation">🧩 Without Spring — Manual Object Creation</h2>
<p>When you don’t use Spring, you do something like this:</p>
<pre><code class="lang-java">Notification notification = <span class="hljs-keyword">new</span> EmailNotification();
notification.send(<span class="hljs-string">"Hello!"</span>);
</code></pre>
<p>or</p>
<pre><code class="lang-java"><span class="hljs-keyword">if</span> (type.equals(<span class="hljs-string">"SMS"</span>)) {
    notification = <span class="hljs-keyword">new</span> SMSNotification();
}
</code></pre>
<p>👉 You’re <strong>manually</strong> creating objects with <code>new</code>.<br />If you add a new type tomorrow, you have to modify your code — it’s tightly coupled.</p>
<hr />
<h2 id="heading-with-spring-factory-in-action-but-hidden">🏭 With Spring — Factory in Action (But Hidden)</h2>
<p>When you use Spring:</p>
<pre><code class="lang-java"><span class="hljs-meta">@Service</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NotificationService</span> </span>{
    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> Notification notification;
}
</code></pre>
<p>You never write <code>new EmailNotification()</code> — right?</p>
<p>That’s because the <strong>Spring Container (ApplicationContext)</strong> acts like a <strong>Super Factory</strong>:</p>
<ul>
<li><p>It creates objects for you (beans).</p>
</li>
<li><p>It decides <em>which implementation</em> to inject.</p>
</li>
<li><p>It manages their lifecycle (singleton, prototype, etc.).</p>
</li>
</ul>
<h3 id="heading-under-the-hood">Under the Hood</h3>
<p>Spring uses:</p>
<ul>
<li><p><strong>BeanFactory</strong> → The root factory that creates and manages beans.</p>
</li>
<li><p><strong>ApplicationContext</strong> → A more advanced factory that supports AOP, events, and message sources.</p>
</li>
</ul>
<p>So yes — the <em>Factory Pattern</em> is at the <strong>core of Spring’s architecture</strong>.<br />You don’t see it, but it’s running behind every <code>@Autowired</code>.</p>
<hr />
<h2 id="heading-why-you-should-still-learn-the-factory-pattern">🧠 Why You Should Still Learn the Factory Pattern</h2>
<p>Even though Spring handles object creation for you, understanding the <strong>Factory Pattern</strong> helps you:</p>
<ol>
<li><p><strong>Understand Spring’s internals</strong><br /> → You’ll know how <code>BeanFactory</code>, <code>ApplicationContext</code>, and dependency injection work.</p>
</li>
<li><p><strong>Write cleaner custom factories</strong><br /> → Sometimes, you still need to decide at runtime which object to create based on user input or configuration.<br /> Example: Payment gateway selector — Stripe, Razorpay, or PayPal.</p>
<pre><code class="lang-java"> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">PaymentGateway</span> </span>{ <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">()</span></span>; }

 <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StripeGateway</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">PaymentGateway</span> </span>{ <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">()</span> </span>{ ... } }
 <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RazorpayGateway</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">PaymentGateway</span> </span>{ <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">()</span> </span>{ ... } }

 <span class="hljs-meta">@Component</span>
 <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PaymentFactory</span> </span>{
     <span class="hljs-function"><span class="hljs-keyword">public</span> PaymentGateway <span class="hljs-title">getGateway</span><span class="hljs-params">(String type)</span> </span>{
         <span class="hljs-keyword">return</span> <span class="hljs-keyword">switch</span> (type) {
             <span class="hljs-keyword">case</span> <span class="hljs-string">"STRIPE"</span> -&gt; <span class="hljs-keyword">new</span> StripeGateway();
             <span class="hljs-keyword">case</span> <span class="hljs-string">"RAZORPAY"</span> -&gt; <span class="hljs-keyword">new</span> RazorpayGateway();
             <span class="hljs-keyword">default</span> -&gt; <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">"Unknown gateway: "</span> + type);
         };
     }
 }
</code></pre>
<p> You can still inject this factory into services, but the <strong>decision logic</strong> is yours.</p>
</li>
<li><p><strong>Design framework-agnostic systems</strong><br /> → In interviews or low-level architecture design, you may be asked to design without frameworks.<br /> Understanding the pattern helps you explain and design the logic yourself.</p>
</li>
</ol>
<h3 id="heading-final-thought">🏁 Final Thought</h3>
<blockquote>
<p>“If you find yourself changing the same class every time you add a new type of object — it’s time to use the Factory Pattern.”</p>
</blockquote>
<p>The Factory Design Pattern helps you build <strong>clean, maintainable, and scalable</strong> systems by letting your code <strong>focus on behavior</strong>, not object creation.</p>
]]></content:encoded></item><item><title><![CDATA[What , Why & When Builder Pattern]]></title><description><![CDATA[Ever written a constructor so long it looked like a grocery list?Yeah, me too.
You start with a simple class — maybe a User. It only needs a name and email.
Then the next feature arrives.
Now you need phone, address, gender, profile photo, preference...]]></description><link>https://blog.techwithaditya.in/what-why-and-when-builder-pattern</link><guid isPermaLink="true">https://blog.techwithaditya.in/what-why-and-when-builder-pattern</guid><category><![CDATA[design patterns]]></category><category><![CDATA[System Design]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Wed, 15 Oct 2025 21:35:17 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1760563425180/d412ebc6-3c6b-4b10-8bcc-18be83ac881d.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Ever written a constructor so long it looked like a grocery list?<br />Yeah, me too.</p>
<p>You start with a simple class — maybe a <code>User</code>. It only needs a name and email.</p>
<p>Then the next feature arrives.</p>
<p>Now you need phone, address, gender, profile photo, preferences, and whatnot.</p>
<p>Before you realize it, your constructor looks like this 👇</p>
<pre><code class="lang-java">User user = <span class="hljs-keyword">new</span> User(<span class="hljs-string">"Aditya"</span>, <span class="hljs-string">"aditya@gmail.com"</span>, <span class="hljs-string">"9876543210"</span>, <span class="hljs-string">"Male"</span>, <span class="hljs-string">"Bhopal"</span>,
 <span class="hljs-string">"image.png"</span>, <span class="hljs-keyword">true</span>, <span class="hljs-keyword">false</span>, LocalDate.now());
</code></pre>
<p>And the next developer who opens your class?<br />They’ll cry first, then quit. 😩</p>
<h2 id="heading-the-problem-the-telescoping-constructor-nightmare">⚙️ The Problem: The <em>Telescoping Constructor</em> Nightmare</h2>
<p>This is called the <strong>Telescoping Constructor Problem</strong> —<br />when your class grows with too many optional parameters, you end up with:</p>
<ul>
<li><p><strong>Huge constructors</strong></p>
</li>
<li><p><strong>Confusing parameter order</strong></p>
</li>
<li><p><strong>Hard-to-read code</strong></p>
</li>
<li><p><strong>Nightmare when adding new fields</strong></p>
</li>
</ul>
<h2 id="heading-the-need-for-a-better-way">🧩 The Need for a Better Way</h2>
<p>We need a way to create objects like:</p>
<pre><code class="lang-java">User user = <span class="hljs-keyword">new</span> UserBuilder()
    .setName(<span class="hljs-string">"Aditya"</span>)
    .setEmail(<span class="hljs-string">"aditya@gmail.com"</span>)
    .setPhone(<span class="hljs-string">"9876543210"</span>)
    .build();
</code></pre>
<p>Clean. Readable. Flexible.<br />That’s where the <strong>Builder Pattern</strong> comes in.</p>
<h2 id="heading-what-is-the-builder-pattern">What Is the Builder Pattern?</h2>
<blockquote>
<p>The <strong>Builder Pattern</strong> is a <em>creational design pattern</em> that lets you <strong>construct complex objects step by step</strong>.<br />It separates <strong>object construction</strong> from its <strong>representation</strong>, allowing you to create different types or variations of objects using the same construction process.</p>
</blockquote>
<p>In simple words:</p>
<blockquote>
<p>Instead of passing 10 parameters into a constructor,<br />you “build” the object step-by-step using meaningful methods.</p>
</blockquote>
<h3 id="heading-without-builder-pattern-constructor-hell">❌ Without Builder Pattern (Constructor Hell)</h3>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> </span>{
    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> String email;
    <span class="hljs-keyword">private</span> String phone;
    <span class="hljs-keyword">private</span> String address;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">User</span><span class="hljs-params">(String name, String email, String phone, String address)</span> </span>{
        <span class="hljs-keyword">this</span>.name = name;
        <span class="hljs-keyword">this</span>.email = email;
        <span class="hljs-keyword">this</span>.phone = phone;
        <span class="hljs-keyword">this</span>.address = address;
    }
}
</code></pre>
<p>Creating a user:</p>
<pre><code class="lang-java">User user = <span class="hljs-keyword">new</span> User(<span class="hljs-string">"Aditya"</span>, <span class="hljs-string">"aditya@gmail.com"</span>, <span class="hljs-string">"989654XXXX"</span>, <span class="hljs-string">"Bhopal"</span>);
</code></pre>
<p>Now imagine you add 4 more parameters later. You’ll end up with constructor overloads and confusion.</p>
<hr />
<h3 id="heading-with-builder-pattern">✅ With Builder Pattern</h3>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> </span>{
    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> String email;
    <span class="hljs-keyword">private</span> String phone;
    <span class="hljs-keyword">private</span> String address;

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">User</span><span class="hljs-params">(UserBuilder builder)</span> </span>{
        <span class="hljs-keyword">this</span>.name = builder.name;
        <span class="hljs-keyword">this</span>.email = builder.email;
        <span class="hljs-keyword">this</span>.phone = builder.phone;
        <span class="hljs-keyword">this</span>.address = builder.address;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserBuilder</span> </span>{
        <span class="hljs-keyword">private</span> String name;
        <span class="hljs-keyword">private</span> String email;
        <span class="hljs-keyword">private</span> String phone;
        <span class="hljs-keyword">private</span> String address;

        <span class="hljs-function"><span class="hljs-keyword">public</span> UserBuilder <span class="hljs-title">setName</span><span class="hljs-params">(String name)</span> </span>{
            <span class="hljs-keyword">this</span>.name = name;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> UserBuilder <span class="hljs-title">setEmail</span><span class="hljs-params">(String email)</span> </span>{
            <span class="hljs-keyword">this</span>.email = email;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> UserBuilder <span class="hljs-title">setPhone</span><span class="hljs-params">(String phone)</span> </span>{
            <span class="hljs-keyword">this</span>.phone = phone;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> UserBuilder <span class="hljs-title">setAddress</span><span class="hljs-params">(String address)</span> </span>{
            <span class="hljs-keyword">this</span>.address = address;
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> User <span class="hljs-title">build</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> User(<span class="hljs-keyword">this</span>);
        }
    }
}
</code></pre>
<p>Now creation looks like:</p>
<pre><code class="lang-java">User user = <span class="hljs-keyword">new</span> User.UserBuilder()
    .setName(<span class="hljs-string">"Aditya Singh Rajput"</span>)
    .setEmail(<span class="hljs-string">"aditya@gmail.com"</span>)
    .setPhone(<span class="hljs-string">"9876543210"</span>)
    .setAddress(<span class="hljs-string">"Bhopal"</span>)
    .build();
</code></pre>
<p>✅ Readable<br />✅ Flexible<br />✅ Easy to add new fields later</p>
<hr />
<h2 id="heading-real-life-use-case">🏗️ Real-Life Use Case</h2>
<p>Think about creating an <strong>Order</strong> or <strong>Product</strong> entity in an eCommerce system.<br />Some fields are mandatory, while others are optional (like discounts, coupons, etc.).</p>
<p>Using the Builder Pattern makes object creation clear, controlled, and scalable.<br />That’s why frameworks like <strong>Lombok’s , @Builder</strong>, <strong>Spring Boot DTOs</strong>, and even <strong>Java Streams</strong> internally follow similar principles.</p>
]]></content:encoded></item><item><title><![CDATA[Why to learn Design Patterns 🤔 ?]]></title><description><![CDATA[“You don’t learn design patterns because books say so —
you feel their need when your code becomes a tangled mess.Let’s look at how one extra feature request can turn clean code into chaos — and how a pattern can save you.”
💭 Start with a Real Devel...]]></description><link>https://blog.techwithaditya.in/why-to-learn-design-patterns</link><guid isPermaLink="true">https://blog.techwithaditya.in/why-to-learn-design-patterns</guid><category><![CDATA[design patterns]]></category><category><![CDATA[System Design]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Tue, 14 Oct 2025 11:31:07 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1760441561159/d0743ec4-555f-48ff-b9aa-fb90b5188e9f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-you-dont-learn-design-patterns-because-books-say-so">“You don’t <em>learn</em> design patterns because books say so —</h2>
<p>you <em>feel</em> their need when your code becomes a tangled mess.<br />Let’s look at how one extra feature request can turn clean code into chaos — and how a pattern can save you.”</p>
<h2 id="heading-start-with-a-real-developers-struggle">💭 Start with a Real Developer’s Struggle</h2>
<p>Let’s take a <strong>very relatable example</strong> from my Java or Flutter experience.<br />Imagine this situation 👇</p>
<h3 id="heading-scenario-the-spaghetti-code-factory-problem">🧩 Scenario: The “Spaghetti Code” Factory Problem</h3>
<p>You’re building an eCommerce app.<br />At first, you only support <strong>Card Payments</strong>.</p>
<p>So, you write:</p>
<pre><code class="lang-java"><span class="hljs-keyword">if</span> (paymentType.equals(<span class="hljs-string">"CARD"</span>)) {
    processCardPayment();
}
</code></pre>
<p>All good ✅<br />But later, you add:</p>
<ul>
<li><p>UPI Payment</p>
</li>
<li><p>Wallet Payment</p>
</li>
<li><p>Cash on Delivery</p>
</li>
</ul>
<p>Your code becomes:</p>
<pre><code class="lang-java"><span class="hljs-keyword">if</span> (paymentType.equals(<span class="hljs-string">"CARD"</span>)) {
    processCardPayment();
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (paymentType.equals(<span class="hljs-string">"UPI"</span>)) {
    processUPIPayment();
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (paymentType.equals(<span class="hljs-string">"WALLET"</span>)) {
    processWalletPayment();
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (paymentType.equals(<span class="hljs-string">"COD"</span>)) {
    processCODPayment();
}
</code></pre>
<p>Looks okay?<br />Wait till the next requirement hits you…</p>
<blockquote>
<p>“We need to add Crypto payment next week, and refund feature later.”</p>
</blockquote>
<p>Now every time a new payment method comes, you’ll:</p>
<ul>
<li><p>Modify this code again 😩</p>
</li>
<li><p>Risk breaking something else 😩</p>
</li>
<li><p>Have to test everything again 😩</p>
</li>
</ul>
<p>This is <strong>code that grows horizontally</strong> (more <code>if-else</code>) — it violates the <strong>Open/Closed Principle</strong>.<br />Your code is <em>open for modification</em> when it should be <em>open for extension</em>.</p>
<h3 id="heading-and-here-comes-the-realization">🧠 And here comes the realization…</h3>
<p>You pause and think:</p>
<blockquote>
<p>“Why can’t I just have a flexible system that lets me add new payment methods without touching the core logic?”</p>
</blockquote>
<p>That’s when you realize you need a <strong>Design Pattern</strong>.</p>
<p>⚙️ Solution: Factory Method Pattern</p>
<p>You apply a <strong>Factory Method Pattern</strong>, which creates payment objects dynamically:</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Payment</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">process</span><span class="hljs-params">()</span></span>;
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CardPayment</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Payment</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">process</span><span class="hljs-params">()</span> </span>{ System.out.println(<span class="hljs-string">"Processing Card Payment"</span>); }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UPIPayment</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Payment</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">process</span><span class="hljs-params">()</span> </span>{ System.out.println(<span class="hljs-string">"Processing UPI Payment"</span>); }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PaymentFactory</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Payment <span class="hljs-title">getPayment</span><span class="hljs-params">(String type)</span> </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">switch</span> (type) {
            <span class="hljs-keyword">case</span> <span class="hljs-string">"CARD"</span> -&gt; <span class="hljs-keyword">new</span> CardPayment();
            <span class="hljs-keyword">case</span> <span class="hljs-string">"UPI"</span> -&gt; <span class="hljs-keyword">new</span> UPIPayment();
            <span class="hljs-keyword">default</span> -&gt; <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">"Invalid payment type"</span>);
        };
    }
}
</code></pre>
<p>Now your main logic becomes:</p>
<pre><code class="lang-java">Payment payment = PaymentFactory.getPayment(<span class="hljs-string">"CARD"</span>);
payment.process();
</code></pre>
<p>If you add a new payment method tomorrow (like Crypto),<br />you <strong>create a new class</strong>, not change old ones.</p>
<p>✅ Code is extensible<br />✅ Clean and maintainable<br />✅ No repeated conditionals</p>
<h2 id="heading-another-realistic-example-the-singleton-problem">🔥 Another Realistic Example: The Singleton Problem</h2>
<h3 id="heading-scenario">Scenario:</h3>
<p>You’re building a logging system in a backend app.<br />You notice that multiple parts of your app are writing to logs —<br />but different parts create their own logger objects.</p>
<p>You end up with:</p>
<ul>
<li><p>Multiple log files,</p>
</li>
<li><p>Duplicate messages,</p>
</li>
<li><p>Messy output.</p>
</li>
</ul>
<p>Then you realize —<br />you actually only need <strong>one</strong> logging instance shared across the entire app.</p>
<p>That’s when you use a <strong>Singleton pattern.</strong></p>
]]></content:encoded></item><item><title><![CDATA[Mastering "D" in S.O.L.I.D]]></title><description><![CDATA[“High-level modules should not depend on low-level modules”
Don’t tightly couple your main code with details.Both should depend on abstractions (like interfaces or abstract classes).“
Daily Life Example
Imagine:

High-level module = TV Remote

Low-le...]]></description><link>https://blog.techwithaditya.in/mastering-d-in-solid</link><guid isPermaLink="true">https://blog.techwithaditya.in/mastering-d-in-solid</guid><category><![CDATA[design patterns]]></category><category><![CDATA[System Design]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Fri, 10 Oct 2025 18:27:31 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1760168113897/19559a39-806a-438f-a4db-6310538119a8.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-high-level-modules-should-not-depend-on-low-level-modules">“<strong>High-level modules should not depend on low-level modules”</strong></h3>
<p>Don’t tightly couple your main code with details.<br /><strong>Both should depend on abstractions</strong> (like interfaces or abstract classes).“</p>
<h3 id="heading-daily-life-example"><strong>Daily Life Example</strong></h3>
<p>Imagine:</p>
<ul>
<li><p><strong>High-level module</strong> = TV Remote</p>
</li>
<li><p><strong>Low-level module</strong> = TV</p>
</li>
</ul>
<h4 id="heading-without-dependency-inversion">Without Dependency Inversion:</h4>
<blockquote>
<p>Remote directly knows about SamsungTV and its buttons</p>
</blockquote>
<ul>
<li><p>This means the Remote will only work with a Samsung TV.</p>
</li>
<li><p>If you bring an LG TV, you’ll need to change the Remote code.</p>
</li>
<li><p>This is tight coupling and bad design.</p>
</li>
</ul>
<h4 id="heading-with-dependency-inversion">With Dependency Inversion:</h4>
<ul>
<li>We create a <strong>TV interface</strong> that defines basic functions:</li>
</ul>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">TV</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">turnOn</span><span class="hljs-params">()</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">turnOff</span><span class="hljs-params">()</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">changeChannel</span><span class="hljs-params">(<span class="hljs-keyword">int</span> ch)</span></span>;
}
</code></pre>
<ul>
<li><p>Now, <strong>SamsungTV</strong> and <strong>LGTV</strong> both implement this interface.</p>
</li>
<li><p>Remote depends on the <strong>TV interface</strong>, not a specific TV.</p>
</li>
</ul>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Remote</span> </span>{
    TV tv;  <span class="hljs-comment">// depends on abstraction</span>
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">pressPower</span><span class="hljs-params">()</span> </span>{ tv.turnOn(); }
}
</code></pre>
<ul>
<li><p>Now the Remote can work with any TV.</p>
</li>
<li><p>If you change the TV, Remote code does <strong>not</strong> need to change. ✅</p>
</li>
</ul>
<h3 id="heading-summary"><strong>Summary</strong></h3>
<ul>
<li><p>DIP means: <strong>"High-level code should not depend on low-level code. Work via abstractions."</strong></p>
</li>
<li><p>Real-life example: <strong>Remote depends on TV interface, not Samsung or LG TV.</strong></p>
</li>
<li><p>Benefit: Flexible, maintainable, easy to extend in the future.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Mastering "I" in S.O.L.I.D]]></title><description><![CDATA[“A class should not be forced to implement interfaces it does not use.“
👀 Real Life Example
Let’s imagine we are designing an app for different types of workers:
❌ Bad Example (Violating ISP)
public interface Worker {
    void work();
    void eat()...]]></description><link>https://blog.techwithaditya.in/mastering-i-in-solid</link><guid isPermaLink="true">https://blog.techwithaditya.in/mastering-i-in-solid</guid><category><![CDATA[design patterns]]></category><category><![CDATA[SOLID principles]]></category><category><![CDATA[System Design]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Tue, 07 Oct 2025 18:57:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1759863327140/abb5c8b1-e1c0-4896-956b-ea1d3224b3ee.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-a-class-should-not-be-forced-to-implement-interfaces-it-does-not-use">“A class should not be forced to implement interfaces it does not use.“</h2>
<h3 id="heading-real-life-example">👀 Real Life Example</h3>
<p>Let’s imagine we are designing an app for different types of workers:</p>
<h4 id="heading-bad-example-violating-isp">❌ Bad Example (Violating ISP)</h4>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Worker</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">work</span><span class="hljs-params">()</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">eat</span><span class="hljs-params">()</span></span>;
}
</code></pre>
<p>Now we have:</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HumanWorker</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Worker</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">work</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Human working..."</span>);
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">eat</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Human eating..."</span>);
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RobotWorker</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Worker</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">work</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Robot working..."</span>);
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">eat</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// ❌ Robot doesn’t eat — but it’s forced to implement this</span>
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> UnsupportedOperationException(<span class="hljs-string">"Robot can't eat"</span>);
    }
}
</code></pre>
<p>👉 Here, the <strong>RobotWorker</strong> is <strong>forced to implement</strong> <code>eat()</code> even though it doesn’t make sense — violating <strong>Interface Segregation Principle</strong>.</p>
<h3 id="heading-good-example-following-isp">✅ Good Example (Following ISP)</h3>
<p>We break the interface into <strong>smaller, more specific</strong> ones:</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Workable</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">work</span><span class="hljs-params">()</span></span>;
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Eatable</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">eat</span><span class="hljs-params">()</span></span>;
}
</code></pre>
<p>Now we can do:</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HumanWorker</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Workable</span>, <span class="hljs-title">Eatable</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">work</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Human working..."</span>);
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">eat</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Human eating..."</span>);
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RobotWorker</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Workable</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">work</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Robot working..."</span>);
    }
}
</code></pre>
<p>✅ Each class now implements <strong>only what it needs</strong>.</p>
<h3 id="heading-in-real-world-development">⚙️ <strong>In Real-World Development</strong></h3>
<ul>
<li><p>This principle makes your code <strong>modular and flexible</strong>.</p>
</li>
<li><p>It becomes <strong>easier to extend</strong> and <strong>maintain</strong>.</p>
</li>
<li><p>Commonly used in <strong>Spring Boot</strong>, where services implement <strong>specific small interfaces</strong> rather than one huge one.</p>
</li>
</ul>
<p>The <strong>Interface Segregation Principle (I in SOLID)</strong> is <strong>mostly used to solve problems related to “fat interfaces”</strong> — that is, when <strong>interfaces become too large and force classes to implement unnecessary methods.</strong></p>
<p>Let’s break it down clearly 👇</p>
<hr />
<h3 id="heading-main-issue-it-solves">⚠️ <strong>Main Issue It Solves</strong></h3>
<blockquote>
<p><strong>“Too many responsibilities or unnecessary methods in one interface.”</strong></p>
</blockquote>
<p>In short —<br />It solves <strong>tight coupling</strong> and <strong>unnecessary implementation</strong> problems between <strong>interfaces and classes</strong>.</p>
]]></content:encoded></item><item><title><![CDATA[Mastering "L" in S.O.L.I.D]]></title><description><![CDATA[“Objects of a superclass should be replaceable with objects of its subclasses without breaking the application’s behavior.”
💡 Real-Life Example
Think of this:

You have a class Bird that can fly().

You create a subclass Sparrow (it can fly ✅).

The...]]></description><link>https://blog.techwithaditya.in/mastering-l-in-solid</link><guid isPermaLink="true">https://blog.techwithaditya.in/mastering-l-in-solid</guid><category><![CDATA[design patterns]]></category><category><![CDATA[System Design]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Mon, 06 Oct 2025 10:43:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1759749355655/9f70bd0b-52a2-4b52-9f5e-42f82f609d20.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-objects-of-a-superclass-should-be-replaceable-with-objects-of-its-subclasses-without-breaking-the-applications-behavior"><em>“Objects of a superclass should be replaceable with objects of its subclasses without breaking the application’s behavior.”</em></h2>
<h3 id="heading-real-life-example">💡 Real-Life Example</h3>
<p>Think of this:</p>
<ul>
<li><p>You have a class <strong>Bird</strong> that can <strong>fly()</strong>.</p>
</li>
<li><p>You create a subclass <strong>Sparrow</strong> (it can fly ✅).</p>
</li>
<li><p>Then you create another subclass <strong>Ostrich</strong> (it cannot fly ❌).</p>
</li>
</ul>
<p>If your program expects all <strong>Birds to fly</strong>, using <strong>Ostrich</strong> will break your logic — which <strong>violates LSP</strong>.</p>
<p>So, <strong>Ostrich should not extend Bird</strong> if Bird assumes “can fly”.</p>
<pre><code class="lang-java"><span class="hljs-comment">// ❌ Violates LSP</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bird</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fly</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Flying..."</span>);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sparrow</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Bird</span> </span>{}  <span class="hljs-comment">// OK</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Ostrich</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Bird</span> </span>{   <span class="hljs-comment">// Problem</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fly</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> UnsupportedOperationException(<span class="hljs-string">"Ostrich can't fly!"</span>);
    }
}

<span class="hljs-comment">// ✅ Correct Approach</span>
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Bird</span> </span>{}
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">FlyingBird</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Bird</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">fly</span><span class="hljs-params">()</span></span>;
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sparrow</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">FlyingBird</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fly</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Flying..."</span>);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Ostrich</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Bird</span> </span>{
    <span class="hljs-comment">// Ostrich doesn't fly, but still a bird — no LSP violation.</span>
}
</code></pre>
<h3 id="heading-more-examples">More Examples:-</h3>
<h3 id="heading-1-payment-system-example-e-commerce-app">🧾 1. Payment System Example (E-commerce App)</h3>
<p>Imagine you have a <strong>PaymentService</strong> interface that processes payments.</p>
<p>You have classes like:</p>
<ul>
<li><p><strong>CreditCardPayment</strong></p>
</li>
<li><p><strong>PayPalPayment</strong></p>
</li>
<li><p><strong>UPIPayment</strong></p>
</li>
</ul>
<p>Each one should behave <strong>like a payment service</strong> — if one fails to behave differently (e.g., throws error on same method), it <strong>violates LSP</strong>.</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">PaymentService</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">(<span class="hljs-keyword">double</span> amount)</span></span>;
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CreditCardPayment</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">PaymentService</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">(<span class="hljs-keyword">double</span> amount)</span> </span>{
        System.out.println(<span class="hljs-string">"Paid "</span> + amount + <span class="hljs-string">" using Credit Card"</span>);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PayPalPayment</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">PaymentService</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">(<span class="hljs-keyword">double</span> amount)</span> </span>{
        System.out.println(<span class="hljs-string">"Paid "</span> + amount + <span class="hljs-string">" using PayPal"</span>);
    }
}

<span class="hljs-comment">// ❌ Violating LSP</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CashOnDeliveryPayment</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">PaymentService</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">(<span class="hljs-keyword">double</span> amount)</span> </span>{
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> UnsupportedOperationException(<span class="hljs-string">"Cash payment not supported online!"</span>);
    }
}
</code></pre>
<p>Here, <code>CashOnDeliveryPayment</code> breaks <strong>LSP</strong> because it <strong>cannot be used</strong> wherever a <code>PaymentService</code> is expected.<br />It will crash the program.</p>
<p>✅ <strong>Fix</strong>: Don’t force it into the same inheritance; maybe separate interfaces:</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">OnlinePaymentService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">PaymentService</span> </span>{}
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">OfflinePaymentService</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">collectCash</span><span class="hljs-params">()</span></span>;
}
</code></pre>
<h3 id="heading-2-inventory-system-example">📦 2. Inventory System Example</h3>
<h4 id="heading-english">🔹 English:</h4>
<p>Suppose your app has a base class <code>InventoryItem</code>.<br />You create child classes like:</p>
<ul>
<li><p><code>PerishableItem</code> (milk, fruits)</p>
</li>
<li><p><code>NonPerishableItem</code> (phone, laptop)</p>
</li>
</ul>
<p>If the parent class has a method like <code>calculateExpiryDate()</code>, and non-perishable items can’t have expiry, that’s an <strong>LSP violation</strong>.</p>
<p>✅ Fix → Move expiry logic to a subclass or new interface.</p>
]]></content:encoded></item><item><title><![CDATA[Mastering "O" in S.O.L.I.D 😜]]></title><description><![CDATA[A class should be open for extension but closed for modification.
Nai aya samajh 🤔 ?
💡 Simple Meaning:

You can add new behavior to the class

But you should not change its existing code


Agaya samajh but Abhi clear nai hua example se dekhte hai…
...]]></description><link>https://blog.techwithaditya.in/mastering-o-in-solid</link><guid isPermaLink="true">https://blog.techwithaditya.in/mastering-o-in-solid</guid><category><![CDATA[design patterns]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Productivity]]></category><category><![CDATA[Developer]]></category><category><![CDATA[architecture]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Tue, 15 Apr 2025 05:31:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1744694144323/4374210e-6716-4143-a50b-767888e8efb9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-a-class-should-be-open-for-extension-but-closed-for-modification">A class should be <strong>open for extension</strong> but <strong>closed for modification</strong>.</h2>
<p>Nai aya samajh 🤔 ?</p>
<h3 id="heading-simple-meaning">💡 Simple Meaning:</h3>
<ul>
<li><p>You can <strong>add new behavior</strong> to the class</p>
</li>
<li><p>But you should <strong>not change its existing code</strong></p>
</li>
</ul>
<p>Agaya samajh but Abhi clear nai hua example se dekhte hai…</p>
<h3 id="heading-real-life-example">🧍‍♂️ Real-Life Example:</h3>
<p>Imagine you have a <strong>mobile phone</strong>.<br />Now, you want to:</p>
<ul>
<li><p>Add a new <strong>back cover</strong></p>
</li>
<li><p>Add a <strong>screen guard</strong></p>
</li>
</ul>
<p>You don't <strong>open the mobile’s internal parts</strong>, right?<br />You just <strong>add things on top of it</strong> → that’s <strong>extension without modification</strong> ✅</p>
<p>❌ Bad Java Example (Breaks OCP):</p>
<pre><code class="lang-java">
 <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PaymentService</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">makePayment</span><span class="hljs-params">(String paymentType)</span> </span>{
        <span class="hljs-keyword">if</span> (paymentType.equals(<span class="hljs-string">"creditcard"</span>)) {
            System.out.println(<span class="hljs-string">"Paid with Credit Card"</span>);
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (paymentType.equals(<span class="hljs-string">"paypal"</span>)) {
            System.out.println(<span class="hljs-string">"Paid with PayPal"</span>);
        }
    }
}
</code></pre>
<h3 id="heading-problem">😵 Problem:</h3>
<ul>
<li>Every time a <strong>new payment method</strong> comes (like UPI, Bitcoin), you have to <strong>edit this class</strong>.</li>
</ul>
<p>➡️ <strong>Code is not closed for modification</strong> ❌</p>
<p>✅ Good Java Example (Follows OCP):</p>
<p>Step 1: Create an interface</p>
<pre><code class="lang-java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">PaymentMethod</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">()</span></span>;
}
</code></pre>
<p>Step 2: Implement different payment methods</p>
<pre><code class="lang-java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CreditCardPayment</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">PaymentMethod</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Paid with Credit Card"</span>);
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PayPalPayment</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">PaymentMethod</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Paid with PayPal"</span>);
    }
}
</code></pre>
<p>Step 3: Use them without changing logic</p>
<pre><code class="lang-java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PaymentService</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">makePayment</span><span class="hljs-params">(PaymentMethod method)</span> </span>{
        method.pay();  <span class="hljs-comment">// No if-else, no changing code</span>
    }
}
</code></pre>
<p>✅ Now, to add a new method like UPI:</p>
<pre><code class="lang-java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UpiPayment</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">PaymentMethod</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pay</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"Paid with UPI"</span>);
    }
}
</code></pre>
<p>No need to touch <code>PaymentService</code> — just <strong>extend</strong> the code with a new class 🚀.</p>
<h3 id="heading-hinglish-explanation">💡 Hinglish Explanation:</h3>
<p>Jab hum koi class banate hain, toh hum chahte hain ki:</p>
<ul>
<li><p><strong>Naya feature add</strong> kar sakein (open for extension ✅)</p>
</li>
<li><p><strong>Purani code ko change na karna pade</strong> (closed for modification ✅)</p>
</li>
</ul>
<p>Matlab:</p>
<ul>
<li><p>Jab naye requirement aaye, toh <strong>nayi class banao</strong> ya <strong>existing class ko extend karo</strong></p>
</li>
<li><p><strong>Lekin purane code ko touch mat karo</strong>, warna bugs aa sakte hain</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Mastering "S" in S.O.L.I.D 😎.]]></title><description><![CDATA[A class should have only one reason to change, meaning it should only do one job/responsibility.
💡 Simple Explanation:
If one class handles too many things, any change in one part might accidentally break other parts. That’s risky and messy.
💻 Java...]]></description><link>https://blog.techwithaditya.in/mastering-s-in-solid</link><guid isPermaLink="true">https://blog.techwithaditya.in/mastering-s-in-solid</guid><category><![CDATA[design patterns]]></category><category><![CDATA[System Design]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Tue, 15 Apr 2025 05:11:41 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1744659674998/80a92842-3336-4811-8a24-3ea3628a12d0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>A class should have only one reason to change</strong>, meaning it should only do <strong>one job/responsibility</strong>.</p>
<h3 id="heading-simple-explanation">💡 Simple Explanation:</h3>
<p>If one class handles <strong>too many things</strong>, any change in one part might <strong>accidentally break other parts</strong>. That’s risky and messy.</p>
<p>💻 Java Example (❌ Bad - Violates SRP):</p>
<pre><code class="lang-java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Employee</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">calculateSalary</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// Logic to calculate salary</span>
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">saveToDatabase</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// Logic to save employee data</span>
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">generateReport</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// Logic to generate report</span>
    }
}
</code></pre>
<p>❌ Here, the <code>Employee</code> class is doing <strong>too many things</strong>:</p>
<ul>
<li><p>Salary logic</p>
</li>
<li><p>Database logic</p>
</li>
<li><p>Report generation</p>
</li>
</ul>
<p>✅ Good Code (Follows SRP):</p>
<pre><code class="lang-java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Employee</span> </span>{
    <span class="hljs-keyword">private</span> String name;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">double</span> salary;

    <span class="hljs-comment">// Only employee-related properties</span>
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SalaryCalculator</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">calculateSalary</span><span class="hljs-params">(Employee employee)</span> </span>{
        <span class="hljs-comment">// Logic to calculate salary</span>
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EmployeeRepository</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">saveToDatabase</span><span class="hljs-params">(Employee employee)</span> </span>{
        <span class="hljs-comment">// Logic to save employee data</span>
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ReportGenerator</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">generateReport</span><span class="hljs-params">(Employee employee)</span> </span>{
        <span class="hljs-comment">// Logic to generate report</span>
    }
}
</code></pre>
<p>✅ Now, each class has <strong>one job</strong>:</p>
<ul>
<li><p><code>Employee</code> → holds employee data</p>
</li>
<li><p><code>SalaryCalculator</code> → calculates salary</p>
</li>
<li><p><code>EmployeeRepository</code> → saves to database</p>
</li>
<li><p><code>ReportGenerator</code> → creates reports</p>
</li>
</ul>
<h3 id="heading-trick-to-check-srp-hinglish-for-better-understanding">✅ Trick to Check SRP (Hinglish for better understanding):</h3>
<ol>
<li><p><strong>Class ka naam padhke</strong> socho:<br /> "Kya ye class sirf ek kaam ka representative lag rahi hai?"</p>
</li>
<li><p><strong>Uske methods dekh ke</strong> socho:<br /> "Kya sabhi methods ek hi type ka kaam kar rahe hain?"</p>
</li>
</ol>
<p>👉 Is class ko kab kab change karna padega?</p>
<ul>
<li><p>Business ne bola calculation logic change karo → <code>calculateTotal()</code></p>
</li>
<li><p>Printing format change hua → <code>printInvoice()</code></p>
</li>
<li><p>Database change hua → <code>saveToDatabase()</code></p>
</li>
</ul>
<p>➡️ <strong>Teen alag-alag reason</strong> se change karna pad raha hai. So, <strong>SRP break ho gaya</strong>.</p>
]]></content:encoded></item><item><title><![CDATA[What the Fcuk is S.O.L.I.D ?]]></title><description><![CDATA[SOLID is an acronym for 5 principles that help you write better, cleaner, and maintainable code, especially in object-oriented programming.Think of them like rules for good architecture — just like building a house needs a strong base and clear desig...]]></description><link>https://blog.techwithaditya.in/what-the-fcuk-is-solid</link><guid isPermaLink="true">https://blog.techwithaditya.in/what-the-fcuk-is-solid</guid><category><![CDATA[design patterns]]></category><category><![CDATA[SOLID principles]]></category><category><![CDATA[System Design]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Mon, 14 Apr 2025 18:38:57 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1744655420274/73d8cb8e-2274-482c-b052-b7846e301dd5.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>SOLID</strong> is an acronym for <strong>5 principles</strong> that help you write <strong>better, cleaner, and maintainable code</strong>, especially in object-oriented programming.<br />Think of them like <strong>rules for good architecture</strong> — just like building a house needs a strong base and clear design, software needs the same.</p>
<hr />
<h2 id="heading-why-use-solid">🧠 Why use SOLID?</h2>
<ul>
<li><p>Code becomes <strong>easy to understand</strong></p>
</li>
<li><p><strong>Fewer bugs</strong></p>
</li>
<li><p><strong>Easier to change and update</strong></p>
</li>
<li><p>Promotes <strong>reusability</strong></p>
</li>
</ul>
<hr />
<h2 id="heading-lets-break-down-solid-one-by-one-with-daily-life-examples">🔠 Let’s break down SOLID one-by-one with daily life examples:</h2>
<hr />
<h3 id="heading-1-s-single-responsibility-principle">1. <strong>S - Single Responsibility Principle</strong></h3>
<p><strong>One class should have only one job/responsibility.</strong></p>
<h4 id="heading-example-a-housemaid">🧠 Example: A Housemaid</h4>
<ul>
<li><p>One maid for <strong>cooking</strong></p>
</li>
<li><p>Another for <strong>cleaning</strong></p>
</li>
<li><p>Another for <strong>washing clothes</strong></p>
</li>
</ul>
<p>If 1 maid does everything, it’s messy. Just like that, a class should <strong>only focus on one thing</strong>.</p>
<blockquote>
<p>✅ So if something goes wrong in cooking, we don’t touch the cleaning part.</p>
</blockquote>
<hr />
<h3 id="heading-2-o-openclosed-principle">2. <strong>O - Open/Closed Principle</strong></h3>
<p><strong>Software should be open for extension, but closed for modification.</strong></p>
<h4 id="heading-example-mobile-phone-cases">🧠 Example: Mobile Phone Cases</h4>
<ul>
<li><p>Your phone can add a <strong>new case</strong> (extend)</p>
</li>
<li><p>But the phone’s <strong>internal structure</strong> stays the same (not modified)</p>
</li>
</ul>
<blockquote>
<p>✅ You should be able to add new features <strong>without changing old code.</strong></p>
</blockquote>
<hr />
<h3 id="heading-3-l-liskov-substitution-principle">3. <strong>L - Liskov Substitution Principle</strong></h3>
<p><strong>Subclasses should be able to replace their parent classes without breaking anything.</strong></p>
<h4 id="heading-example-charging-devices">🧠 Example: Charging Devices</h4>
<ul>
<li>If you have a charger slot that fits a <strong>phone charger</strong>, it should also support any <strong>new phone model charger</strong> (as long as it’s a phone).</li>
</ul>
<blockquote>
<p>✅ Child class should behave properly when used in place of the parent class.</p>
</blockquote>
<hr />
<h3 id="heading-4-i-interface-segregation-principle">4. <strong>I - Interface Segregation Principle</strong></h3>
<p><strong>Don’t force a class to implement things it doesn’t use.</strong></p>
<h4 id="heading-example-remote-controls">🧠 Example: Remote Controls</h4>
<ul>
<li><p>A <strong>TV remote</strong> has TV buttons</p>
</li>
<li><p>A <strong>Fan remote</strong> doesn’t need volume buttons</p>
</li>
</ul>
<blockquote>
<p>❌ Don’t give a remote buttons it never uses<br />✅ Create small, separate interfaces for different jobs</p>
</blockquote>
<hr />
<h3 id="heading-5-d-dependency-inversion-principle">5. <strong>D - Dependency Inversion Principle</strong></h3>
<p><strong>Depend on abstractions, not on concrete classes.</strong></p>
<h4 id="heading-example-electricity-plug">🧠 Example: Electricity Plug</h4>
<ul>
<li><p>You plug your phone, fridge, or washing machine into the same <strong>socket</strong></p>
</li>
<li><p>The <strong>socket doesn't care</strong> what device it is – it just gives power</p>
</li>
</ul>
<blockquote>
<p>✅ Your code should depend on <strong>general things (like interfaces)</strong> not specific ones (like exact classes)</p>
<h3 id="heading-for-now">✅ For Now…</h3>
<p>We have learned <strong>what SOLID principles are all about</strong> and also saw some <strong>daily life examples</strong> to understand them in a simple way.</p>
<p>But still, things might not be <strong>fully clear</strong>, right?</p>
<p>Don’t worry!</p>
<p>To get complete clarity, we’ll now go <strong>one-by-one</strong> through each SOLID principle with a <strong>real Java code example</strong> so that you understand:</p>
<ul>
<li><p>What the principle means</p>
</li>
<li><p>What happens if we break it</p>
</li>
<li><p>How to follow it correctly in Java</p>
</li>
</ul>
</blockquote>
]]></content:encoded></item><item><title><![CDATA[My Blogging Journey Begins ✍️]]></title><description><![CDATA[Hey there! 👋I'm Aditya Singh Rajput, and I'm absolutely thrilled to publish my very first blog here on Hashnode!
👨‍💻 Who Am I?
I'm a Computer Science Engineering student from Sagar Institute of Science and Technology, Bhopal, and I’m deeply passio...]]></description><link>https://blog.techwithaditya.in/my-blogging-journey-begins</link><guid isPermaLink="true">https://blog.techwithaditya.in/my-blogging-journey-begins</guid><category><![CDATA[#codenewbies]]></category><category><![CDATA[backend]]></category><category><![CDATA[DSA]]></category><category><![CDATA[Flutter]]></category><category><![CDATA[Devops]]></category><category><![CDATA[Developer]]></category><dc:creator><![CDATA[Aditya Singh Rajput]]></dc:creator><pubDate>Thu, 10 Apr 2025 08:38:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1744292589186/242d2516-30e8-4c90-a064-ee859cb1f511.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey there! 👋<br />I'm <strong>Aditya Singh Rajput</strong>, and I'm absolutely thrilled to publish my very first blog here on <strong>Hashnode</strong>!</p>
<h3 id="heading-who-am-i">👨‍💻 Who Am I?</h3>
<p>I'm a <strong>Computer Science Engineering</strong> student from <strong>Sagar Institute of Science and Technology, Bhopal</strong>, and I’m deeply passionate about <strong>Java development</strong>, <strong>backend systems</strong>, and building real-world projects that make an impact.</p>
<p>Over the past 1.5 years, I've immersed myself in Java and Spring Boot, worked on exciting eCommerce backends, explored Flutter, and even implemented authentication systems like JWT and OAuth2. I'm always hungry to learn, build, and grow — and now, I’m stepping into the world of blogging to share that journey with you!</p>
<h3 id="heading-why-i-started-blogging">🧠 Why I Started Blogging?</h3>
<p>To be honest, I've always believed in learning out loud. Whether it’s a bug I fixed after hours of struggle or a new concept I mastered, I feel there's always something worth sharing. So here’s why I started this blog:</p>
<ul>
<li><p>✅ To document my learning journey 📚</p>
</li>
<li><p>✅ To help others avoid the roadblocks I faced 🚧</p>
</li>
<li><p>✅ To grow as a developer by writing and reflecting 💡</p>
</li>
<li><p>✅ To connect with like-minded folks from the dev community 🤝</p>
</li>
</ul>
<h3 id="heading-what-can-you-expect-from-this-blog">💡 What Can You Expect From This Blog?</h3>
<p>I'll be sharing:</p>
<ul>
<li><p>My learnings in <strong>Java, Spring Boot</strong>, and <strong>backend architectures</strong></p>
</li>
<li><p>Projects I'm building — including code, architecture decisions, and challenges</p>
</li>
<li><p>Guides on topics like <strong>JWT Auth</strong>, <strong>OAuth2 login</strong>, and <strong>API design</strong></p>
</li>
<li><p>My journey learning <strong>React</strong>, <strong>Flutter</strong>, and mastering <strong>DSA</strong></p>
</li>
<li><p>Insights from internships, coding routines, productivity tips, and more!</p>
</li>
</ul>
<p>Whether you're a beginner developer, a student like me, or someone curious about tech, I hope you’ll find something valuable here.</p>
<h3 id="heading-lets-learn-and-grow-together">🌱 Let's Learn and Grow Together</h3>
<p>Blogging is new to me, and I’m excited (and a little nervous) to be honest 😅 — but I know this is going to be a rewarding journey.</p>
<p>If you're reading this, thank you so much for being here. Your support means the world to me. 🙌</p>
<p>Let’s connect, learn, and grow — one blog at a time. 🚀<br />If you have any thoughts, suggestions, or just want to say hi, drop a comment below! 💬</p>
<p>See you in the next post! 😊</p>
<p><strong>— Aditya Singh Rajput</strong></p>
]]></content:encoded></item></channel></rss>