<?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: Saras Growth Space</title>
    <description>The latest articles on DEV Community by Saras Growth Space (@saras_growth_space).</description>
    <link>https://dev.to/saras_growth_space</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%2F3805711%2F9d71d829-9e4b-42ad-919e-ef864f55f79d.png</url>
      <title>DEV Community: Saras Growth Space</title>
      <link>https://dev.to/saras_growth_space</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saras_growth_space"/>
    <language>en</language>
    <item>
      <title>LLD Domain Modeling: Aggregates (How Real Systems Protect Consistency)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Sat, 06 Jun 2026 17:30:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-domain-modeling-aggregates-how-real-systems-protect-consistency-59dd</link>
      <guid>https://dev.to/saras_growth_space/lld-domain-modeling-aggregates-how-real-systems-protect-consistency-59dd</guid>
      <description>&lt;p&gt;Once you understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Entities&lt;/li&gt;
&lt;li&gt;Value Objects&lt;/li&gt;
&lt;li&gt;business identity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;the next important question becomes:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“How do these objects behave together safely?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because real systems are not made of isolated objects.&lt;/p&gt;

&lt;p&gt;Objects interact.&lt;br&gt;
State changes.&lt;br&gt;
Rules must stay consistent.&lt;/p&gt;

&lt;p&gt;And without clear boundaries, systems slowly become:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fragile&lt;/li&gt;
&lt;li&gt;inconsistent&lt;/li&gt;
&lt;li&gt;difficult to maintain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the problem Aggregates are designed to solve.&lt;/p&gt;


&lt;h1&gt;
  
  
  The Real Problem Behind Aggregates
&lt;/h1&gt;

&lt;p&gt;Imagine a ride-sharing system.&lt;/p&gt;

&lt;p&gt;A Ride may contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;driver&lt;/li&gt;
&lt;li&gt;fare&lt;/li&gt;
&lt;li&gt;pickup location&lt;/li&gt;
&lt;li&gt;ride status&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who is allowed to change the ride state?&lt;/li&gt;
&lt;li&gt;Who validates transitions?&lt;/li&gt;
&lt;li&gt;Who prevents invalid updates?&lt;/li&gt;
&lt;li&gt;Who protects business consistency?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without boundaries, any part of the system can modify anything.&lt;/p&gt;

&lt;p&gt;That usually becomes dangerous very quickly.&lt;/p&gt;


&lt;h1&gt;
  
  
  What Is an Aggregate?
&lt;/h1&gt;

&lt;p&gt;An Aggregate is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;a controlled cluster of related domain objects treated as a single consistency boundary.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That sounds abstract initially.&lt;/p&gt;

&lt;p&gt;So let’s simplify it.&lt;/p&gt;


&lt;h1&gt;
  
  
  Think of Aggregates as Safety Boundaries
&lt;/h1&gt;

&lt;p&gt;An Aggregate groups related objects together and defines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;who controls them&lt;/li&gt;
&lt;li&gt;how updates happen&lt;/li&gt;
&lt;li&gt;where business consistency is enforced&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of exposing every internal object directly, the Aggregate protects them through a central entry point.&lt;/p&gt;


&lt;h1&gt;
  
  
  Aggregate Root (Very Important)
&lt;/h1&gt;

&lt;p&gt;Every Aggregate has a main controlling Entity called the Aggregate Root.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ride Aggregate
 ├── Ride (Root)
 ├── Fare
 ├── PickupLocation
 ├── DropLocation
 └── DriverReference
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ride is the Aggregate Root&lt;/li&gt;
&lt;li&gt;external systems interact through Ride&lt;/li&gt;
&lt;li&gt;internal consistency is protected by Ride&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Why This Matters
&lt;/h1&gt;

&lt;p&gt;Suppose any service could directly modify internal objects:&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="n"&gt;ride&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getFare&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;ride&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;COMPLETED&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rules can be bypassed&lt;/li&gt;
&lt;li&gt;invalid states appear&lt;/li&gt;
&lt;li&gt;debugging becomes difficult&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is no controlled consistency anymore.&lt;/p&gt;




&lt;h1&gt;
  
  
  Aggregates Create Controlled State Changes
&lt;/h1&gt;

&lt;p&gt;Instead, updates should happen through the Aggregate Root:&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="n"&gt;ride&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assignDriver&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;ride&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;startRide&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;ride&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;completeRide&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the Aggregate controls:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;validations&lt;/li&gt;
&lt;li&gt;lifecycle rules&lt;/li&gt;
&lt;li&gt;state transitions&lt;/li&gt;
&lt;li&gt;consistency guarantees&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is much safer.&lt;/p&gt;




&lt;h1&gt;
  
  
  Real Example — BookMyShow
&lt;/h1&gt;

&lt;p&gt;Consider a Show.&lt;/p&gt;

&lt;p&gt;A Show contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;seats&lt;/li&gt;
&lt;li&gt;availability state&lt;/li&gt;
&lt;li&gt;pricing&lt;/li&gt;
&lt;li&gt;booking status&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Critical rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the same seat must not be booked twice.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now ask:&lt;/p&gt;

&lt;p&gt;Who should control seat consistency?&lt;/p&gt;

&lt;p&gt;Not Booking.&lt;/p&gt;

&lt;p&gt;Not Payment.&lt;/p&gt;

&lt;p&gt;The Show Aggregate should own it.&lt;/p&gt;




&lt;h1&gt;
  
  
  Example Structure
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Show Aggregate
 ├── Show (Root)
 ├── SeatAvailability
 ├── Pricing
 └── Timing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Show becomes responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;locking seats&lt;/li&gt;
&lt;li&gt;confirming seats&lt;/li&gt;
&lt;li&gt;releasing seats&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This creates a clear ownership boundary.&lt;/p&gt;




&lt;h1&gt;
  
  
  Aggregates Are About Consistency, Not Grouping
&lt;/h1&gt;

&lt;p&gt;A common beginner misunderstanding is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Aggregates are just object containers.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not exactly.&lt;/p&gt;

&lt;p&gt;The real purpose is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;consistency protection&lt;/li&gt;
&lt;li&gt;business rule enforcement&lt;/li&gt;
&lt;li&gt;controlled modifications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The grouping exists only because consistency needs protection.&lt;/p&gt;




&lt;h1&gt;
  
  
  Strong Aggregates Usually Protect Invariants
&lt;/h1&gt;

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

&lt;ul&gt;
&lt;li&gt;Ride cannot complete before starting&lt;/li&gt;
&lt;li&gt;Booking cannot confirm unpaid seats&lt;/li&gt;
&lt;li&gt;Cart total must equal item totals&lt;/li&gt;
&lt;li&gt;Account balance cannot become invalid&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Aggregates help ensure these rules are never broken.&lt;/p&gt;




&lt;h1&gt;
  
  
  Aggregates Also Reduce Chaos in Large Systems
&lt;/h1&gt;

&lt;p&gt;Without Aggregates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;every object talks to every object&lt;/li&gt;
&lt;li&gt;state changes happen everywhere&lt;/li&gt;
&lt;li&gt;ownership becomes unclear&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eventually:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;services become bloated&lt;/li&gt;
&lt;li&gt;debugging becomes painful&lt;/li&gt;
&lt;li&gt;consistency bugs appear frequently&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Aggregates create structure.&lt;/p&gt;




&lt;h1&gt;
  
  
  A Common Beginner Mistake
&lt;/h1&gt;

&lt;p&gt;Many beginners either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;expose everything publicly
or&lt;/li&gt;
&lt;li&gt;create one massive Aggregate for the entire system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both are problematic.&lt;/p&gt;

&lt;p&gt;Very large Aggregates create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tight coupling&lt;/li&gt;
&lt;li&gt;performance issues&lt;/li&gt;
&lt;li&gt;difficult scaling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good Aggregates stay focused around consistency needs.&lt;/p&gt;




&lt;h1&gt;
  
  
  Strong LLD Thinking
&lt;/h1&gt;

&lt;p&gt;Weak thinking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“How should I connect classes together?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Strong thinking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Which objects must remain consistent together?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That question leads to better boundaries.&lt;/p&gt;

&lt;p&gt;And better boundaries lead to more maintainable systems.&lt;/p&gt;




&lt;h1&gt;
  
  
  Real Systems Depend on Good Aggregate Design
&lt;/h1&gt;

&lt;p&gt;In practice, Aggregates help define:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ownership&lt;/li&gt;
&lt;li&gt;consistency boundaries&lt;/li&gt;
&lt;li&gt;update rules&lt;/li&gt;
&lt;li&gt;transaction boundaries&lt;/li&gt;
&lt;li&gt;lifecycle control&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They are one of the most important concepts in domain modeling because they force systems to evolve safely instead of chaotically.&lt;/p&gt;

&lt;p&gt;And once consistency boundaries become clear, the next important question becomes:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What are the actual business rules that these boundaries are protecting?&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Domain Modeling: Business Rules &amp; Invariants (The Rules Your System Must Never Break)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Thu, 04 Jun 2026 16:32:15 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-domain-modeling-business-rules-invariants-the-rules-your-system-must-never-break-13o6</link>
      <guid>https://dev.to/saras_growth_space/lld-domain-modeling-business-rules-invariants-the-rules-your-system-must-never-break-13o6</guid>
      <description>&lt;p&gt;In the previous posts, we learned about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Entities&lt;/li&gt;
&lt;li&gt;Value Objects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These help us model the business world.&lt;/p&gt;

&lt;p&gt;But a question still remains:&lt;/p&gt;

&lt;p&gt;Why do these objects exist?&lt;/p&gt;

&lt;p&gt;The answer is simple:&lt;/p&gt;

&lt;p&gt;To enforce business behavior correctly.&lt;/p&gt;

&lt;p&gt;And business behavior is usually defined by:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Business Rules and Invariants.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding them is one of the biggest shifts from writing code to designing systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is a Business Rule?
&lt;/h2&gt;

&lt;p&gt;A Business Rule is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A rule that defines how the business expects the system to behave.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  BookMyShow
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A seat cannot be sold twice.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Uber
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A driver cannot complete a ride that never started.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Amazon
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The final order amount must be correct.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Banking
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;An account cannot withdraw more than its available balance.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These rules do not come from code.&lt;/p&gt;

&lt;p&gt;They come from the business.&lt;/p&gt;




&lt;h2&gt;
  
  
  Software Does Not Invent Rules
&lt;/h2&gt;

&lt;p&gt;Many beginners think:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Business Rules
↓
Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reality is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Business
↓
Business Rules
↓
Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The software's responsibility is not to create rules.&lt;/p&gt;

&lt;p&gt;The software's responsibility is to enforce them.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is an Invariant?
&lt;/h2&gt;

&lt;p&gt;An Invariant is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A business rule that must always remain true.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Think of it as a rule the business cannot afford to violate.&lt;/p&gt;




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

&lt;p&gt;Suppose a movie seat is booked.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;One seat
=
One booking
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Seat A1
→ Booking #101
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Seat A1
→ Booking #101

Seat A1
→ Booking #102
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The invariant has been violated.&lt;/p&gt;




&lt;h2&gt;
  
  
  Business Rule vs Invariant
&lt;/h2&gt;

&lt;p&gt;These terms are related but not identical.&lt;/p&gt;

&lt;h3&gt;
  
  
  Business Rule
&lt;/h3&gt;

&lt;p&gt;Describes expected behavior.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Customers can cancel orders before shipment.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Invariant
&lt;/h3&gt;

&lt;p&gt;Must never become false.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A shipped order cannot become unshipped.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Business Rules
        ↓
Some become critical enough to become
        ↓
Invariants
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every invariant is a business rule.&lt;/p&gt;

&lt;p&gt;Not every business rule is an invariant.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Invariants Matter More Than Features
&lt;/h2&gt;

&lt;p&gt;Imagine a payment system with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;beautiful UI&lt;/li&gt;
&lt;li&gt;excellent APIs&lt;/li&gt;
&lt;li&gt;fast processing&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Money randomly disappears.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Would anyone trust it?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Because correctness matters more than features.&lt;/p&gt;

&lt;p&gt;A system that is correct but simple is useful.&lt;/p&gt;

&lt;p&gt;A system that is feature-rich but incorrect is dangerous.&lt;/p&gt;




&lt;h2&gt;
  
  
  Every Domain Has Invariants
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Ride Sharing
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A driver cannot have two active rides simultaneously.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Food Delivery
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;An order cannot be delivered before it is accepted.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Banking
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Balance cannot become negative beyond allowed limits.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  E-Commerce
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order total must equal item total plus charges.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  BookMyShow
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A seat cannot be booked twice.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Different systems.&lt;/p&gt;

&lt;p&gt;Same concept.&lt;/p&gt;




&lt;h2&gt;
  
  
  Invariants Are Hidden Inside Requirements
&lt;/h2&gt;

&lt;p&gt;Requirements rarely say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Invariant:
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead they appear indirectly.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Users can book seats for a movie.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A beginner sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Seat
Movie
Booking
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An experienced designer asks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Can multiple users book the same seat?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They're searching for invariants.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strong Designers Look for "Never"
&lt;/h2&gt;

&lt;p&gt;A useful trick:&lt;/p&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What should NEVER happen?&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A payment should never be processed twice.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A cancelled ride should never become active again.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A delivered order should never return to CREATED state.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These statements often reveal invariants immediately.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real Example: Order Lifecycle
&lt;/h2&gt;

&lt;p&gt;Consider an order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATED
    ↓
PAID
    ↓
SHIPPED
    ↓
DELIVERED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One invariant might be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIVERED
cannot occur before
SHIPPED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another might be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PAID
cannot return to
CREATED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something important.&lt;/p&gt;

&lt;p&gt;The important part is not the fields.&lt;/p&gt;

&lt;p&gt;The important part is protecting valid business behavior.&lt;/p&gt;




&lt;h2&gt;
  
  
  Weak LLD Thinking
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What classes should I create?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Strong LLD Thinking
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What business rules must never break?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This question often leads to far better designs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Matters Later
&lt;/h2&gt;

&lt;p&gt;As systems become larger, a new question appears:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Who is responsible for protecting these invariants?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That question eventually leads us to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Aggregates
Consistency Boundaries
Aggregate Roots
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll explore those in upcoming posts.&lt;/p&gt;

&lt;p&gt;For now, focus on identifying invariants first.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Most Important Insight
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Entities describe:&lt;/strong&gt;&lt;br&gt;
Who exists&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Value Objects describe:&lt;/strong&gt;&lt;br&gt;
What something means&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Business Rules describe:&lt;/strong&gt;&lt;br&gt;
How the business operates&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Invariants describe:&lt;/strong&gt;&lt;br&gt;
What must never be violated&lt;/p&gt;

&lt;p&gt;The strongest domain models are built around protecting invariants.&lt;/p&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;p&gt;An invariant is a business rule that must always remain true, and discovering these rules is one of the most important skills in Domain Modeling.&lt;/p&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>codinginterview</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>LLD Domain Modeling: Value Objects (Objects Defined by Meaning, Not Identity)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Wed, 03 Jun 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-domain-modeling-value-objects-objects-defined-by-meaning-not-identity-84</link>
      <guid>https://dev.to/saras_growth_space/lld-domain-modeling-value-objects-objects-defined-by-meaning-not-identity-84</guid>
      <description>&lt;p&gt;In the previous post, we learned about Entities.&lt;/p&gt;

&lt;p&gt;Entities are objects whose identity matters.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order #123
Ride #567
Booking #ABC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if their data changes, they remain the same business object because their identity remains the same.&lt;/p&gt;

&lt;p&gt;But not every object in a system behaves this way.&lt;/p&gt;

&lt;p&gt;Some objects are important not because of who they are, but because of what they represent.&lt;/p&gt;

&lt;p&gt;These are called:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Value Objects&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Understanding the difference between Entities and Value Objects is one of the most important skills in Domain Modeling.&lt;/p&gt;




&lt;h2&gt;
  
  
  Start With a Simple Question
&lt;/h2&gt;

&lt;p&gt;Imagine two money objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do we care which specific ₹500 object it is?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;If the value is the same, they mean the same thing.&lt;/p&gt;

&lt;p&gt;Now compare this with Orders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order #101
Order #102
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if both have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Amount = ₹500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;they are still different orders.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because identity matters.&lt;/p&gt;

&lt;p&gt;This is the core distinction.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is a Value Object?
&lt;/h2&gt;

&lt;p&gt;A Value Object is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;an object whose meaning comes entirely from its values, not from its identity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If two value objects contain the same values:&lt;/p&gt;

&lt;p&gt;they are considered equal.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-Life Example
&lt;/h2&gt;

&lt;p&gt;Think about a postal address:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;221B Baker Street
London
NW1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If two address objects contain exactly the same information:&lt;/p&gt;

&lt;p&gt;they represent the same address.&lt;/p&gt;

&lt;p&gt;Nobody asks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Which Address object is this?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead they ask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What address does it represent?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That makes Address a Value Object.&lt;/p&gt;




&lt;h2&gt;
  
  
  Entity vs Value Object
&lt;/h2&gt;

&lt;p&gt;Consider this:&lt;/p&gt;

&lt;h3&gt;
  
  
  User
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User #123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Identity matters.&lt;/p&gt;

&lt;p&gt;Even if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;email changes&lt;/li&gt;
&lt;li&gt;phone changes&lt;/li&gt;
&lt;li&gt;address changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;it is still the same user.&lt;/p&gt;

&lt;p&gt;Entity.&lt;/p&gt;




&lt;h3&gt;
  
  
  Address
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Street
City
State
ZIP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Identity does not matter.&lt;/p&gt;

&lt;p&gt;Only values matter.&lt;/p&gt;

&lt;p&gt;Value Object.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Quick Test
&lt;/h2&gt;

&lt;p&gt;Whenever you encounter an object, ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If two instances contain exactly the same data, should the business consider them the same?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If YES:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Value Object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If NO:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Entity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Common Value Object Examples
&lt;/h2&gt;

&lt;p&gt;Across real systems:&lt;/p&gt;

&lt;h3&gt;
  
  
  Money
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usually represented by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;amount&lt;/li&gt;
&lt;li&gt;currency&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Address
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Street
City
Country
ZIP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Location
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Latitude
Longitude
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Coordinates
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x
y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Time Range
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start Time
End Time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Distance
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 km
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are usually Value Objects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Value Objects Matter
&lt;/h2&gt;

&lt;p&gt;Many beginners model everything as entities.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AddressEntity
MoneyEntity
LocationEntity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates unnecessary complexity.&lt;/p&gt;

&lt;p&gt;Because these objects do not need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;identity&lt;/li&gt;
&lt;li&gt;lifecycle&lt;/li&gt;
&lt;li&gt;tracking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They only need meaning.&lt;/p&gt;




&lt;h2&gt;
  
  
  Value Objects Make Models Simpler
&lt;/h2&gt;

&lt;p&gt;Imagine an Order:&lt;/p&gt;

&lt;p&gt;Without Value Objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order
    street
    city
    state
    zip
    amount
    currency
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The entity starts becoming cluttered.&lt;/p&gt;

&lt;p&gt;With Value Objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order
    Address
    Money
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model becomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cleaner&lt;/li&gt;
&lt;li&gt;more expressive&lt;/li&gt;
&lt;li&gt;easier to maintain&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Value Objects Should Usually Be Immutable
&lt;/h2&gt;

&lt;p&gt;This is one of their most important characteristics.&lt;/p&gt;

&lt;p&gt;Suppose we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Money(500, INR)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changing it into:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Money(1000, INR)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;creates a completely different value.&lt;/p&gt;

&lt;p&gt;Instead of modifying the existing object:&lt;/p&gt;

&lt;p&gt;create a new one.&lt;/p&gt;

&lt;p&gt;This prevents accidental side effects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Immutability Helps
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;being shared by multiple entities.&lt;/p&gt;

&lt;p&gt;If someone changes it unexpectedly:&lt;/p&gt;

&lt;p&gt;multiple objects may be affected.&lt;/p&gt;

&lt;p&gt;Immutable value objects avoid this problem.&lt;/p&gt;

&lt;p&gt;Their meaning never changes after creation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Equality Works Differently
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compare by identity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Value Objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compare by values
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Money(500, INR)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Money(500, INR)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;should be equal.&lt;/p&gt;

&lt;p&gt;Because their values are identical.&lt;/p&gt;




&lt;h2&gt;
  
  
  Ride Sharing Example
&lt;/h2&gt;

&lt;p&gt;Consider Uber.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ride #567
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Entity.&lt;/p&gt;




&lt;p&gt;Pickup Location:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Latitude
Longitude
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Value Object.&lt;/p&gt;




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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Amount
Currency
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Value Object.&lt;/p&gt;




&lt;p&gt;Notice:&lt;/p&gt;

&lt;p&gt;The Ride is tracked.&lt;/p&gt;

&lt;p&gt;The Location and Fare simply describe the Ride.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Beginner Mistake
&lt;/h2&gt;

&lt;p&gt;Treating every noun as an entity.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LocationEntity
PriceEntity
AddressEntity
DistanceEntity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This usually creates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;unnecessary IDs&lt;/li&gt;
&lt;li&gt;unnecessary databases&lt;/li&gt;
&lt;li&gt;unnecessary lifecycle management&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not everything needs identity.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strong Domain Modeling Thinking
&lt;/h2&gt;

&lt;p&gt;When designing a model:&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Does the business care who this object is,
or only what value it represents?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That single question often reveals whether you're looking at an Entity or a Value Object.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Most Important Insight
&lt;/h2&gt;

&lt;p&gt;Entities represent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;identity through time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Value Objects represent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;meaning through values
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding this distinction is one of the foundations of strong Low-Level Design.&lt;/p&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A Value Object is an object whose meaning comes entirely from its values, making identity irrelevant and equality based solely on the data it contains.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Domain Modeling: Entities (Objects That Have Identity and Lifecycle)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Tue, 02 Jun 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-domain-modeling-entities-objects-that-have-identity-and-lifecycle-f99</link>
      <guid>https://dev.to/saras_growth_space/lld-domain-modeling-entities-objects-that-have-identity-and-lifecycle-f99</guid>
      <description>&lt;p&gt;In the previous post, we learned that Domain Modeling is not about classes.&lt;/p&gt;

&lt;p&gt;It is about understanding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;business behavior&lt;/li&gt;
&lt;li&gt;business rules&lt;/li&gt;
&lt;li&gt;ownership&lt;/li&gt;
&lt;li&gt;lifecycle&lt;/li&gt;
&lt;li&gt;correctness&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we begin with the most fundamental building block of any domain model:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Entities&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Almost every real system revolves around entities.&lt;/p&gt;

&lt;p&gt;Orders.&lt;/p&gt;

&lt;p&gt;Bookings.&lt;/p&gt;

&lt;p&gt;Rides.&lt;/p&gt;

&lt;p&gt;Payments.&lt;/p&gt;

&lt;p&gt;Users.&lt;/p&gt;

&lt;p&gt;Understanding entities correctly is one of the biggest differences between beginner and strong LLD thinking.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Simplest Definition
&lt;/h2&gt;

&lt;p&gt;An Entity is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;an object that has a unique identity and must be tracked over time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The important phrase is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tracked over time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because identity is what makes an entity special.&lt;/p&gt;




&lt;h2&gt;
  
  
  Example: An Order
&lt;/h2&gt;

&lt;p&gt;Imagine an e-commerce system.&lt;/p&gt;

&lt;p&gt;A customer places an order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order #123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PAID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SHIPPED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIVERED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something important.&lt;/p&gt;

&lt;p&gt;Many attributes changed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;status&lt;/li&gt;
&lt;li&gt;shipment information&lt;/li&gt;
&lt;li&gt;timestamps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But it is still:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order #123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The identity remains the same.&lt;/p&gt;

&lt;p&gt;That is why Order is an Entity.&lt;/p&gt;




&lt;h2&gt;
  
  
  Identity Is More Important Than Data
&lt;/h2&gt;

&lt;p&gt;Beginners often focus on fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;
    &lt;span class="n"&gt;amount&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But entities are not defined by fields.&lt;/p&gt;

&lt;p&gt;They are defined by identity.&lt;/p&gt;

&lt;p&gt;Even if all fields change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Amount changes
Status changes
Address changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The business still considers it the same Order.&lt;/p&gt;

&lt;p&gt;Identity survives change.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-Life Analogy
&lt;/h2&gt;

&lt;p&gt;Think about a person.&lt;/p&gt;

&lt;p&gt;You may change:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hairstyle&lt;/li&gt;
&lt;li&gt;phone number&lt;/li&gt;
&lt;li&gt;address&lt;/li&gt;
&lt;li&gt;job&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But you remain the same individual.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because your identity is independent of those attributes.&lt;/p&gt;

&lt;p&gt;Entities behave similarly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Entity = Continuity Through Time
&lt;/h2&gt;

&lt;p&gt;This is one of the most important mental models.&lt;/p&gt;

&lt;p&gt;Entities represent:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;continuity through change.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;Ride Sharing System&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ride #567
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REQUESTED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DRIVER_ASSIGNED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IN_PROGRESS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COMPLETED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ride evolves.&lt;/p&gt;

&lt;p&gt;But it remains the same ride.&lt;/p&gt;

&lt;p&gt;That continuity is what makes it an entity.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Identify Entities
&lt;/h2&gt;

&lt;p&gt;Ask this question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does the business care about this object's identity?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If yes, it is probably an entity.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  User
&lt;/h3&gt;

&lt;p&gt;Business tracks specific user.&lt;/p&gt;

&lt;p&gt;Entity.&lt;/p&gt;




&lt;h3&gt;
  
  
  Order
&lt;/h3&gt;

&lt;p&gt;Business tracks specific order.&lt;/p&gt;

&lt;p&gt;Entity.&lt;/p&gt;




&lt;h3&gt;
  
  
  Booking
&lt;/h3&gt;

&lt;p&gt;Business tracks specific booking.&lt;/p&gt;

&lt;p&gt;Entity.&lt;/p&gt;




&lt;h3&gt;
  
  
  Ride
&lt;/h3&gt;

&lt;p&gt;Business tracks specific ride.&lt;/p&gt;

&lt;p&gt;Entity.&lt;/p&gt;




&lt;h3&gt;
  
  
  Payment
&lt;/h3&gt;

&lt;p&gt;Business tracks specific payment.&lt;/p&gt;

&lt;p&gt;Entity.&lt;/p&gt;




&lt;h2&gt;
  
  
  Another Important Question
&lt;/h2&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If two objects contain exactly the same data, are they still different?&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order #101
Amount: ₹500

Order #102
Amount: ₹500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same amount.&lt;/p&gt;

&lt;p&gt;Same fields.&lt;/p&gt;

&lt;p&gt;Still different orders.&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;p&gt;Entity.&lt;/p&gt;




&lt;h2&gt;
  
  
  Entity Lifecycle Matters
&lt;/h2&gt;

&lt;p&gt;Entities rarely stay static.&lt;/p&gt;

&lt;p&gt;They evolve.&lt;/p&gt;

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

&lt;p&gt;Booking&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATED
→ CONFIRMED
→ CANCELLED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Order&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATED
→ PAID
→ SHIPPED
→ DELIVERED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ride&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REQUESTED
→ ASSIGNED
→ STARTED
→ COMPLETED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This lifecycle is often more important than the fields themselves.&lt;/p&gt;




&lt;h2&gt;
  
  
  Entities Protect Business Meaning
&lt;/h2&gt;

&lt;p&gt;Consider a banking system.&lt;/p&gt;

&lt;p&gt;Account balance changes constantly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;₹10,000
₹15,000
₹8,000
₹25,000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The amount changes.&lt;/p&gt;

&lt;p&gt;But the account remains the same account.&lt;/p&gt;

&lt;p&gt;The business tracks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Account #A123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;not merely its current balance.&lt;/p&gt;

&lt;p&gt;Identity carries business meaning.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Beginner Mistake
&lt;/h2&gt;

&lt;p&gt;Many developers think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every noun becomes an entity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Wrong.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Address
Money
Coordinates
Distance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are important.&lt;/p&gt;

&lt;p&gt;But the business usually does not track their identity.&lt;/p&gt;

&lt;p&gt;They describe something else.&lt;/p&gt;

&lt;p&gt;These often become Value Objects.&lt;/p&gt;

&lt;p&gt;We will cover them in the next post.&lt;/p&gt;




&lt;h2&gt;
  
  
  Entity Responsibilities
&lt;/h2&gt;

&lt;p&gt;A strong entity is not merely a data holder.&lt;/p&gt;

&lt;p&gt;Bad design:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;
    &lt;span class="n"&gt;amount&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good design:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pay&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;cancel&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;ship&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;Why?&lt;/p&gt;

&lt;p&gt;Because the entity owns its lifecycle.&lt;/p&gt;

&lt;p&gt;Business rules should stay close to the entity whenever possible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Weak LLD Thinking
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Entity = Database Row
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Strong LLD Thinking
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Entity = Business Object With Identity and Lifecycle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This small mindset shift changes the quality of your design dramatically.&lt;/p&gt;




&lt;h2&gt;
  
  
  Examples of Entities Across Systems
&lt;/h2&gt;

&lt;p&gt;Ride Sharing&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rider&lt;/li&gt;
&lt;li&gt;Driver&lt;/li&gt;
&lt;li&gt;Ride&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;BookMyShow&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Show&lt;/li&gt;
&lt;li&gt;Booking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E-Commerce&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User&lt;/li&gt;
&lt;li&gt;Cart&lt;/li&gt;
&lt;li&gt;Order&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Banking&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer&lt;/li&gt;
&lt;li&gt;Account&lt;/li&gt;
&lt;li&gt;Transaction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Food Delivery&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Order&lt;/li&gt;
&lt;li&gt;Restaurant&lt;/li&gt;
&lt;li&gt;Delivery Partner&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of them have one thing in common:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the business cares about who they are, not just what data they contain.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  The Most Important Insight
&lt;/h2&gt;

&lt;p&gt;Fields can change.&lt;/p&gt;

&lt;p&gt;State can change.&lt;/p&gt;

&lt;p&gt;Behavior can change.&lt;/p&gt;

&lt;p&gt;But identity remains.&lt;/p&gt;

&lt;p&gt;That identity is what turns an ordinary object into an Entity.&lt;/p&gt;

&lt;p&gt;And once you start recognizing entities correctly, domain models become much easier to design.&lt;/p&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;An Entity is a business object whose identity matters more than its current data, allowing it to be tracked consistently throughout its lifecycle.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>softwareengineering</category>
      <category>systemdesign</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Domain Modeling: Why Domain Models Exist (Beyond Classes and Objects)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Mon, 01 Jun 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-domain-modeling-why-domain-models-exist-beyond-classes-and-objects-3m97</link>
      <guid>https://dev.to/saras_growth_space/lld-domain-modeling-why-domain-models-exist-beyond-classes-and-objects-3m97</guid>
      <description>&lt;p&gt;After learning Object-Oriented Design, most developers believe they are ready for Low-Level Design.&lt;/p&gt;

&lt;p&gt;They know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;classes&lt;/li&gt;
&lt;li&gt;objects&lt;/li&gt;
&lt;li&gt;inheritance&lt;/li&gt;
&lt;li&gt;composition&lt;/li&gt;
&lt;li&gt;interfaces&lt;/li&gt;
&lt;li&gt;dependency injection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And when given a problem, they start creating classes immediately.&lt;/p&gt;

&lt;p&gt;Yet many designs still feel wrong.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because good systems are not built from classes.&lt;/p&gt;

&lt;p&gt;They are built from understanding the business domain.&lt;/p&gt;

&lt;p&gt;This is where Domain Modeling begins.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Limitation of Thinking Only in Classes
&lt;/h2&gt;

&lt;p&gt;Imagine you're asked to design:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uber&lt;/li&gt;
&lt;li&gt;BookMyShow&lt;/li&gt;
&lt;li&gt;Amazon Cart&lt;/li&gt;
&lt;li&gt;Food Delivery System&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A beginner often starts like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
Driver
Ride

Movie
Seat
Booking

Cart
Order
Product
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then relationships are added:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User has Cart
Order has Payment
Ride has Driver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Technically, this is object-oriented design.&lt;/p&gt;

&lt;p&gt;But something important is still missing.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Question Classes Cannot Answer
&lt;/h2&gt;

&lt;p&gt;Suppose you have a BookMyShow system.&lt;/p&gt;

&lt;p&gt;You create:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Seat
Booking
Show
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks fine.&lt;/p&gt;

&lt;p&gt;Now answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can two users book the same seat?&lt;/li&gt;
&lt;li&gt;What happens if payment fails?&lt;/li&gt;
&lt;li&gt;Who controls seat locking?&lt;/li&gt;
&lt;li&gt;When does a seat become available again?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Classes alone don't answer these questions.&lt;/p&gt;

&lt;p&gt;Because these are business rules.&lt;/p&gt;

&lt;p&gt;And business rules are what actually define the system.&lt;/p&gt;




&lt;h2&gt;
  
  
  Software Exists to Solve Business Problems
&lt;/h2&gt;

&lt;p&gt;Users do not care about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;classes&lt;/li&gt;
&lt;li&gt;inheritance&lt;/li&gt;
&lt;li&gt;interfaces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They care about outcomes.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;In BookMyShow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I should not lose my seat while paying.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Uber:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A driver should not accept multiple active rides.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Amazon:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The final order amount should be correct.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are business expectations.&lt;/p&gt;

&lt;p&gt;Domain modeling exists to protect them.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is a Domain?
&lt;/h2&gt;

&lt;p&gt;A domain is simply:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the business area your software is trying to solve.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;p&gt;Ride Sharing → transportation domain&lt;/p&gt;

&lt;p&gt;Food Delivery → delivery domain&lt;/p&gt;

&lt;p&gt;Banking → financial domain&lt;/p&gt;

&lt;p&gt;E-Commerce → commerce domain&lt;/p&gt;

&lt;p&gt;Healthcare → medical domain&lt;/p&gt;

&lt;p&gt;Every domain has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;terminology&lt;/li&gt;
&lt;li&gt;rules&lt;/li&gt;
&lt;li&gt;workflows&lt;/li&gt;
&lt;li&gt;constraints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these is more important than creating classes.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Domain Modeling?
&lt;/h2&gt;

&lt;p&gt;Domain Modeling is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the process of understanding business behavior and representing it correctly in software.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Notice something important.&lt;/p&gt;

&lt;p&gt;It is NOT:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the process of creating classes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Classes come later.&lt;/p&gt;

&lt;p&gt;First we understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;business concepts&lt;/li&gt;
&lt;li&gt;business rules&lt;/li&gt;
&lt;li&gt;ownership&lt;/li&gt;
&lt;li&gt;responsibilities&lt;/li&gt;
&lt;li&gt;lifecycle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only then do we design structure.&lt;/p&gt;




&lt;h2&gt;
  
  
  Example: Ride Sharing
&lt;/h2&gt;

&lt;p&gt;Many beginners see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rider
Driver
Ride
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But domain modeling sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Driver availability
Ride lifecycle
Ride assignment rules
Cancellation rules
Fare calculation rules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The focus shifts from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;objects
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;business behavior
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a major mindset change.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Three Things Domain Modeling Cares About
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Identity
&lt;/h3&gt;

&lt;p&gt;Some things must be tracked over time.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order #123
Ride #567
Booking #ABC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are not just data.&lt;/p&gt;

&lt;p&gt;They have identity.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Lifecycle
&lt;/h3&gt;

&lt;p&gt;Business objects evolve.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order

CREATED
→ PAID
→ SHIPPED
→ DELIVERED
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding this lifecycle is often more important than designing the class itself.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Business Correctness
&lt;/h3&gt;

&lt;p&gt;Every system has rules that must never break.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;No double booking

No duplicate payment

No invalid ride completion
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Protecting these rules becomes a primary design goal.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Domain Modeling Comes After OOD
&lt;/h2&gt;

&lt;p&gt;Object-Oriented Design teaches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How to structure software
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Domain Modeling teaches:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What should be structured
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OOD gives tools.&lt;/p&gt;

&lt;p&gt;Domain Modeling tells you where and why to use them.&lt;/p&gt;

&lt;p&gt;Think of it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OOD = Construction Techniques

Domain Modeling = Understanding the Building Blueprint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without the blueprint, even excellent construction skills are wasted.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Strong Engineers Think
&lt;/h2&gt;

&lt;p&gt;Weak approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Requirements
→ Classes
→ Methods
→ Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strong approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Requirements
→ Business Behavior
→ Rules
→ Lifecycle
→ Domain Model
→ Classes
→ Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that classes appear much later.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Domain Modeling Matters in Interviews
&lt;/h2&gt;

&lt;p&gt;Most LLD interview failures happen because candidates focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;syntax&lt;/li&gt;
&lt;li&gt;patterns&lt;/li&gt;
&lt;li&gt;APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Interviewers are often evaluating:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;understanding of the problem&lt;/li&gt;
&lt;li&gt;ownership of responsibilities&lt;/li&gt;
&lt;li&gt;business correctness&lt;/li&gt;
&lt;li&gt;system behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Domain modeling directly improves all of these.&lt;/p&gt;




&lt;h2&gt;
  
  
  What We Will Learn Next
&lt;/h2&gt;

&lt;p&gt;In the upcoming posts we will explore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Entities&lt;/li&gt;
&lt;li&gt;Value Objects&lt;/li&gt;
&lt;li&gt;Invariants&lt;/li&gt;
&lt;li&gt;State Machines&lt;/li&gt;
&lt;li&gt;Aggregates&lt;/li&gt;
&lt;li&gt;Bounded Contexts&lt;/li&gt;
&lt;li&gt;Domain Services&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These concepts help transform business behavior into maintainable software.&lt;/p&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Object-Oriented Design teaches you how to build classes. Domain Modeling teaches you what the system actually is before the classes are built.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Object-Oriented Design: From Requirements to Classes (Bridging Thinking to Domain Modeling)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Sun, 31 May 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-object-oriented-design-from-requirements-to-classes-bridging-thinking-to-domain-modeling-ko9</link>
      <guid>https://dev.to/saras_growth_space/lld-object-oriented-design-from-requirements-to-classes-bridging-thinking-to-domain-modeling-ko9</guid>
      <description>&lt;p&gt;After learning core object-oriented concepts like encapsulation, inheritance, polymorphism, interfaces, dependency injection, factories, and system layering, one critical skill still remains:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How do we convert a problem statement into actual classes and relationships?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the point where most beginners struggle in Low-Level Design interviews.&lt;/p&gt;

&lt;p&gt;They understand concepts individually, but fail to transform requirements into a clean object model.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Real Problem in LLD
&lt;/h2&gt;

&lt;p&gt;Given a question like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Design a parking lot&lt;/li&gt;
&lt;li&gt;Design Uber&lt;/li&gt;
&lt;li&gt;Design a food delivery system&lt;/li&gt;
&lt;li&gt;Design Splitwise&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most people immediately start writing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;classes&lt;/li&gt;
&lt;li&gt;methods&lt;/li&gt;
&lt;li&gt;APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But real design does not begin with code.&lt;/p&gt;

&lt;p&gt;It begins with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;understanding the domain&lt;/li&gt;
&lt;li&gt;identifying responsibilities&lt;/li&gt;
&lt;li&gt;discovering system structure&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 1: Start with Requirements, Not Classes
&lt;/h2&gt;

&lt;p&gt;Before thinking about objects, first understand:&lt;/p&gt;

&lt;h3&gt;
  
  
  What does the system need to do?
&lt;/h3&gt;

&lt;p&gt;These are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;functional requirements&lt;/li&gt;
&lt;li&gt;user actions&lt;/li&gt;
&lt;li&gt;system capabilities&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  What constraints exist?
&lt;/h3&gt;

&lt;p&gt;These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;scale&lt;/li&gt;
&lt;li&gt;business rules&lt;/li&gt;
&lt;li&gt;operational limitations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without requirement clarity:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;classes become random&lt;/li&gt;
&lt;li&gt;abstractions become weak&lt;/li&gt;
&lt;li&gt;systems become inconsistent&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 2: Extract Candidate Entities from Requirements
&lt;/h2&gt;

&lt;p&gt;A simple but powerful approach:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Convert problem statements into nouns.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Example: Food Delivery System
&lt;/h2&gt;

&lt;p&gt;From requirements, you may identify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User&lt;/li&gt;
&lt;li&gt;Restaurant&lt;/li&gt;
&lt;li&gt;Dish&lt;/li&gt;
&lt;li&gt;Cart&lt;/li&gt;
&lt;li&gt;Order&lt;/li&gt;
&lt;li&gt;Payment&lt;/li&gt;
&lt;li&gt;DeliveryPartner&lt;/li&gt;
&lt;li&gt;Vehicle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These become:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;candidate entities&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not all will become final classes, but this gives a strong starting point.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Filter Entities Using Responsibility Thinking
&lt;/h2&gt;

&lt;p&gt;Not every noun deserves to become a class.&lt;/p&gt;

&lt;p&gt;The important question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does this entity own meaningful state or behavior?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Strong entity candidates
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Order&lt;/li&gt;
&lt;li&gt;Cart&lt;/li&gt;
&lt;li&gt;Payment&lt;/li&gt;
&lt;li&gt;DeliveryPartner&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;state&lt;/li&gt;
&lt;li&gt;lifecycle&lt;/li&gt;
&lt;li&gt;behavior&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Possible supporting objects
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Address&lt;/li&gt;
&lt;li&gt;Coordinates&lt;/li&gt;
&lt;li&gt;Money&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These may become:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;value objects&lt;/li&gt;
&lt;li&gt;supporting models&lt;/li&gt;
&lt;li&gt;reusable structures&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 4: Assign Responsibilities
&lt;/h2&gt;

&lt;p&gt;This is one of the most important steps in OOD.&lt;/p&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What should this entity be responsible for?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Order
&lt;/h3&gt;

&lt;p&gt;Responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;maintaining order state&lt;/li&gt;
&lt;li&gt;tracking lifecycle&lt;/li&gt;
&lt;li&gt;validating status transitions&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  PaymentService
&lt;/h3&gt;

&lt;p&gt;Responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;processing payments&lt;/li&gt;
&lt;li&gt;handling payment methods&lt;/li&gt;
&lt;li&gt;communicating with gateways&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  DeliveryPartner
&lt;/h3&gt;

&lt;p&gt;Responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;accepting deliveries&lt;/li&gt;
&lt;li&gt;updating delivery progress&lt;/li&gt;
&lt;li&gt;managing availability state&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 5: Identify Relationships Between Entities
&lt;/h2&gt;

&lt;p&gt;Now think about interactions.&lt;/p&gt;

&lt;p&gt;Questions to ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Who owns whom?&lt;/li&gt;
&lt;li&gt;Who depends on whom?&lt;/li&gt;
&lt;li&gt;Which objects collaborate together?&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example Relationships
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cart → belongs to User&lt;/li&gt;
&lt;li&gt;Order → created from Cart&lt;/li&gt;
&lt;li&gt;Order → uses PaymentService&lt;/li&gt;
&lt;li&gt;DeliveryPartner → assigned to Order&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this stage:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;focus on relationships, not implementation&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 6: Discover Behaviors Before Methods
&lt;/h2&gt;

&lt;p&gt;Do not immediately think in code syntax.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;What actions happen in the system?&lt;/li&gt;
&lt;li&gt;Which object should own those actions?&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Instead of randomly writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;process_payment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;who should process payment?&lt;/li&gt;
&lt;li&gt;PaymentService?&lt;/li&gt;
&lt;li&gt;Order?&lt;/li&gt;
&lt;li&gt;external gateway?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This improves responsibility placement.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 7: Convert Structure into Classes
&lt;/h2&gt;

&lt;p&gt;Only now should you move toward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;classes&lt;/li&gt;
&lt;li&gt;interfaces&lt;/li&gt;
&lt;li&gt;services&lt;/li&gt;
&lt;li&gt;dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point, the design becomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;intentional&lt;/li&gt;
&lt;li&gt;structured&lt;/li&gt;
&lt;li&gt;easier to evolve&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Mental Model
&lt;/h2&gt;

&lt;p&gt;Good LLD follows this flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Requirements
→ Entities
→ Responsibilities
→ Relationships
→ Behaviors
→ Classes
→ Code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Skipping steps leads to weak design.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes
&lt;/h2&gt;

&lt;p&gt;Beginners often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;jump directly into coding&lt;/li&gt;
&lt;li&gt;force design patterns early&lt;/li&gt;
&lt;li&gt;create unnecessary classes&lt;/li&gt;
&lt;li&gt;ignore responsibility ownership&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This creates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;poor abstractions&lt;/li&gt;
&lt;li&gt;tight coupling&lt;/li&gt;
&lt;li&gt;confusing systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why This Skill Matters
&lt;/h2&gt;

&lt;p&gt;This requirement-to-design mapping skill is the foundation of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Domain Modeling&lt;/li&gt;
&lt;li&gt;Real-world system design&lt;/li&gt;
&lt;li&gt;LLD interviews&lt;/li&gt;
&lt;li&gt;scalable architecture thinking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;designs remain mechanical&lt;/li&gt;
&lt;li&gt;systems feel artificial&lt;/li&gt;
&lt;li&gt;interviews become difficult&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Real Insight
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The hardest part of object-oriented design is not writing classes — it is discovering the right responsibilities from the problem itself.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Great LLD starts by understanding the domain deeply enough to discover the right objects, responsibilities, and relationships before writing code.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Object-Oriented Design: Design Mistakes That Break Systems (and How to Avoid Them)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Sat, 30 May 2026 07:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-object-oriented-design-design-mistakes-that-break-systems-and-how-to-avoid-them-km</link>
      <guid>https://dev.to/saras_growth_space/lld-object-oriented-design-design-mistakes-that-break-systems-and-how-to-avoid-them-km</guid>
      <description>&lt;p&gt;After learning how to structure systems with layers and handle cross-cutting concerns, the next step is understanding what &lt;em&gt;goes wrong in real designs&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Most LLD failures are not because of missing knowledge, but because of &lt;strong&gt;poor design choices that accumulate over time&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Fat Services (God Objects)
&lt;/h2&gt;

&lt;p&gt;One of the most common problems in LLD:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A single service doing too many things.&lt;/p&gt;
&lt;/blockquote&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;place_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update_inventory&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send_notification&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Why this is bad:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;violates Single Responsibility Principle&lt;/li&gt;
&lt;li&gt;becomes hard to test&lt;/li&gt;
&lt;li&gt;any change risks breaking everything&lt;/li&gt;
&lt;li&gt;logic cannot be reused&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Correct Approach:
&lt;/h3&gt;

&lt;p&gt;Split responsibilities into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PaymentService&lt;/li&gt;
&lt;li&gt;InventoryService&lt;/li&gt;
&lt;li&gt;NotificationService&lt;/li&gt;
&lt;li&gt;OrderService (orchestrator only)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. Wrong Inheritance Usage
&lt;/h2&gt;

&lt;p&gt;Inheritance is often misused for convenience.&lt;/p&gt;




&lt;h3&gt;
  
  
  Bad Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Bird&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Penguin cannot fly&lt;/li&gt;
&lt;li&gt;but is forced to inherit behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This breaks system correctness.&lt;/p&gt;




&lt;h3&gt;
  
  
  Fix:
&lt;/h3&gt;

&lt;p&gt;Use composition or interfaces instead of forced inheritance.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Anemic Domain Models
&lt;/h2&gt;

&lt;p&gt;This happens when:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Classes only hold data but contain no behavior.&lt;/p&gt;
&lt;/blockquote&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All logic is outside the class.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;business rules are scattered&lt;/li&gt;
&lt;li&gt;objects are just data containers&lt;/li&gt;
&lt;li&gt;system becomes procedural instead of object-oriented&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Better Approach:
&lt;/h3&gt;

&lt;p&gt;Put behavior inside entities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Order should manage its own state transitions&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Tight Coupling Between Layers
&lt;/h2&gt;

&lt;p&gt;When services directly depend on concrete implementations:&lt;/p&gt;




&lt;h3&gt;
  
  
  Bad Design
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Service directly calls database&lt;/li&gt;
&lt;li&gt;Service directly calls external APIs&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;changes ripple across system&lt;/li&gt;
&lt;li&gt;testing becomes difficult&lt;/li&gt;
&lt;li&gt;system becomes rigid&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Fix:
&lt;/h3&gt;

&lt;p&gt;Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;interfaces&lt;/li&gt;
&lt;li&gt;dependency injection&lt;/li&gt;
&lt;li&gt;abstraction layers&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Breaking Layer Boundaries
&lt;/h2&gt;

&lt;p&gt;A very common mistake:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Business logic leaking into controllers or infrastructure.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;validation inside controllers&lt;/li&gt;
&lt;li&gt;DB logic inside services&lt;/li&gt;
&lt;li&gt;external API logic inside entities&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Result:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;unclear responsibilities&lt;/li&gt;
&lt;li&gt;messy architecture&lt;/li&gt;
&lt;li&gt;hard to scale system&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Over-Engineering Early
&lt;/h2&gt;

&lt;p&gt;Another major mistake:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;adding factories everywhere&lt;/li&gt;
&lt;li&gt;using patterns without need&lt;/li&gt;
&lt;li&gt;creating unnecessary abstractions&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Problem:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;complexity increases without benefit&lt;/li&gt;
&lt;li&gt;code becomes hard to read&lt;/li&gt;
&lt;li&gt;development slows down&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Core Insight
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Good design is not about adding patterns—it is about placing responsibilities correctly.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  How to Identify Bad Design Early
&lt;/h2&gt;

&lt;p&gt;Ask these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does one class have too many responsibilities?&lt;/li&gt;
&lt;li&gt;Can I change one part without affecting others?&lt;/li&gt;
&lt;li&gt;Are rules scattered or centralized?&lt;/li&gt;
&lt;li&gt;Is behavior inside the correct object?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If answers are unclear, design is likely flawed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real System Impact
&lt;/h2&gt;

&lt;p&gt;Bad design leads to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;slow feature development&lt;/li&gt;
&lt;li&gt;frequent bugs&lt;/li&gt;
&lt;li&gt;difficulty scaling system&lt;/li&gt;
&lt;li&gt;high maintenance cost&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good design leads to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;predictable behavior&lt;/li&gt;
&lt;li&gt;easy extensions&lt;/li&gt;
&lt;li&gt;clean separation of concerns&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Design Principle
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A system is well-designed when each component has a clear, minimal, and well-defined responsibility.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Most system failures come from unclear responsibilities, not missing code.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>softwareengineering</category>
      <category>systemdesign</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Object-Oriented Design: Cross-Cutting Concerns (Logging, Retry, Monitoring)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Fri, 29 May 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-object-oriented-design-cross-cutting-concerns-logging-retry-monitoring-2e4i</link>
      <guid>https://dev.to/saras_growth_space/lld-object-oriented-design-cross-cutting-concerns-logging-retry-monitoring-2e4i</guid>
      <description>&lt;p&gt;Once your system is properly layered, a new problem appears:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some responsibilities do not belong to a single layer, but still affect the entire system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These are called &lt;strong&gt;cross-cutting concerns&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What are Cross-Cutting Concerns?
&lt;/h2&gt;

&lt;p&gt;They are features that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;span multiple layers&lt;/li&gt;
&lt;li&gt;affect many classes or services&lt;/li&gt;
&lt;li&gt;are not part of core business logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;logging&lt;/li&gt;
&lt;li&gt;error handling&lt;/li&gt;
&lt;li&gt;retry mechanisms&lt;/li&gt;
&lt;li&gt;monitoring&lt;/li&gt;
&lt;li&gt;authentication&lt;/li&gt;
&lt;li&gt;caching (sometimes)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why They Are a Problem
&lt;/h2&gt;

&lt;p&gt;If handled incorrectly, they lead to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;duplicated code everywhere&lt;/li&gt;
&lt;li&gt;cluttered business logic&lt;/li&gt;
&lt;li&gt;hard-to-maintain services&lt;/li&gt;
&lt;li&gt;inconsistent behavior&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Bad Design: Mixing Concerns Everywhere
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;place_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;LOG: placing order&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# business logic
&lt;/span&gt;            &lt;span class="k"&gt;pass&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;LOG: error occurred&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Problems:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;logging repeated in every service&lt;/li&gt;
&lt;li&gt;retry logic scattered&lt;/li&gt;
&lt;li&gt;business logic polluted&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Good Design: Separation of Concerns
&lt;/h2&gt;

&lt;p&gt;Cross-cutting logic should be handled separately from core business logic.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Logging as a Separate Layer
&lt;/h2&gt;

&lt;p&gt;Instead of writing logs everywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use logging utilities&lt;/li&gt;
&lt;li&gt;or interceptors&lt;/li&gt;
&lt;li&gt;or decorators&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;LOG: start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;LOG: end&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. Retry Mechanism
&lt;/h2&gt;

&lt;p&gt;Retry logic should not be inside business services.&lt;/p&gt;

&lt;p&gt;It should be abstracted.&lt;/p&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;pass&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wrapper&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. Monitoring &amp;amp; Observability
&lt;/h2&gt;

&lt;p&gt;Monitoring should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;track system behavior&lt;/li&gt;
&lt;li&gt;not interfere with business logic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It runs alongside execution, not inside it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Insight
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Cross-cutting concerns should surround business logic, not mix with it.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  How to Handle Cross-Cutting Concerns
&lt;/h2&gt;

&lt;p&gt;There are multiple approaches:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Decorators (simple systems)
&lt;/h3&gt;

&lt;p&gt;Wrap behavior externally.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Middleware (web systems)
&lt;/h3&gt;

&lt;p&gt;Intercept requests and responses.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Aspect-Oriented Design (advanced systems)
&lt;/h3&gt;

&lt;p&gt;Inject behavior at runtime.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real System Example
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Order Service Without Clean Separation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;logging inside service&lt;/li&gt;
&lt;li&gt;retry inside service&lt;/li&gt;
&lt;li&gt;error handling repeated&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Order Service With Clean Separation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;service handles only business logic&lt;/li&gt;
&lt;li&gt;logging handled externally&lt;/li&gt;
&lt;li&gt;retry handled by wrapper&lt;/li&gt;
&lt;li&gt;monitoring handled by system layer&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why This Matters in LLD
&lt;/h2&gt;

&lt;p&gt;Without proper separation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;services become bloated&lt;/li&gt;
&lt;li&gt;logic becomes unreadable&lt;/li&gt;
&lt;li&gt;debugging becomes complex&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With proper separation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;business logic stays clean&lt;/li&gt;
&lt;li&gt;system behavior is consistent&lt;/li&gt;
&lt;li&gt;scaling becomes easier&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Design Principle
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Cross-cutting concerns should be handled outside core business logic to keep systems clean and maintainable.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Logging, retry, and monitoring should wrap around business logic, not live inside it.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Object-Oriented Design: System Layers (Structuring Real Systems)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Thu, 28 May 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-object-oriented-design-system-layers-structuring-real-systems-38mk</link>
      <guid>https://dev.to/saras_growth_space/lld-object-oriented-design-system-layers-structuring-real-systems-38mk</guid>
      <description>&lt;p&gt;After understanding objects, relationships, dependency injection, and factories, the next step is learning how to organize everything into a real system.&lt;/p&gt;

&lt;p&gt;Because writing good classes is not enough.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A system becomes maintainable only when responsibilities are correctly distributed across layers.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Why System Layers Exist
&lt;/h2&gt;

&lt;p&gt;In real applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;business logic grows&lt;/li&gt;
&lt;li&gt;external integrations increase&lt;/li&gt;
&lt;li&gt;complexity spreads quickly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without structure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;everything gets mixed&lt;/li&gt;
&lt;li&gt;changes become risky&lt;/li&gt;
&lt;li&gt;debugging becomes difficult&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;System layers solve this by separating responsibilities.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Layered Architecture
&lt;/h2&gt;

&lt;p&gt;A typical LLD system is divided into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Controller Layer&lt;/li&gt;
&lt;li&gt;Service Layer&lt;/li&gt;
&lt;li&gt;Domain Layer (Entities)&lt;/li&gt;
&lt;li&gt;Infrastructure Layer&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  1. Controller Layer
&lt;/h2&gt;

&lt;p&gt;The controller is the entry point.&lt;/p&gt;

&lt;p&gt;It is responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;receiving input&lt;/li&gt;
&lt;li&gt;validating basic data&lt;/li&gt;
&lt;li&gt;calling services&lt;/li&gt;
&lt;/ul&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;place_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;order_service&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;place_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Key Rule
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Controllers should NOT contain business logic.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  2. Service Layer
&lt;/h2&gt;

&lt;p&gt;The service layer contains business logic.&lt;/p&gt;

&lt;p&gt;It is responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;orchestrating workflows&lt;/li&gt;
&lt;li&gt;applying business rules&lt;/li&gt;
&lt;li&gt;coordinating multiple components&lt;/li&gt;
&lt;/ul&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;place_order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mark_created&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. Domain Layer (Entities)
&lt;/h2&gt;

&lt;p&gt;Entities represent core business objects.&lt;/p&gt;

&lt;p&gt;They:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hold state&lt;/li&gt;
&lt;li&gt;enforce rules&lt;/li&gt;
&lt;li&gt;manage their own behavior&lt;/li&gt;
&lt;/ul&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;mark_created&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CREATED&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;blockquote&gt;
&lt;p&gt;Entities are not just data holders — they are behavior-rich models.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  4. Infrastructure Layer
&lt;/h2&gt;

&lt;p&gt;This layer handles external systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;databases&lt;/li&gt;
&lt;li&gt;APIs&lt;/li&gt;
&lt;li&gt;messaging systems&lt;/li&gt;
&lt;li&gt;external services&lt;/li&gt;
&lt;/ul&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentGateway&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Why Layering is Important
&lt;/h2&gt;

&lt;p&gt;Without layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;services become bloated&lt;/li&gt;
&lt;li&gt;logic becomes scattered&lt;/li&gt;
&lt;li&gt;dependencies become unclear&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;responsibilities are isolated&lt;/li&gt;
&lt;li&gt;code becomes predictable&lt;/li&gt;
&lt;li&gt;scaling becomes easier&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Flow of Control
&lt;/h2&gt;

&lt;p&gt;A typical system flow looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Controller → Service → Entity → Infrastructure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each layer has a clear responsibility boundary.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Design Insight
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Each layer should depend only on the layer below it, not across unrelated layers.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Common Mistake
&lt;/h2&gt;

&lt;p&gt;Beginners often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;put business logic in controllers&lt;/li&gt;
&lt;li&gt;mix infrastructure calls inside services&lt;/li&gt;
&lt;li&gt;make entities passive data structures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This leads to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tight coupling&lt;/li&gt;
&lt;li&gt;low cohesion&lt;/li&gt;
&lt;li&gt;difficult maintenance&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Clean Design Principle
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Separate what the system does (business logic) from how it interacts with external systems (infrastructure).&lt;/p&gt;
&lt;/blockquote&gt;




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

&lt;p&gt;Think of a restaurant:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer → Controller&lt;/li&gt;
&lt;li&gt;Chef → Service&lt;/li&gt;
&lt;li&gt;Recipe → Entity&lt;/li&gt;
&lt;li&gt;Suppliers → Infrastructure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each has a clear role and responsibility.&lt;/p&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;System layers organize responsibilities so that each part of the system has a single, clear purpose.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Object-Oriented Design: Factories &amp; Object Creation (Managing Complexity of Creation)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Wed, 27 May 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-object-oriented-design-factories-object-creation-managing-complexity-of-creation-5elm</link>
      <guid>https://dev.to/saras_growth_space/lld-object-oriented-design-factories-object-creation-managing-complexity-of-creation-5elm</guid>
      <description>&lt;p&gt;After Dependency Injection, a natural question appears in real systems:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If classes don’t create objects, and dependencies are injected externally, who is responsible for creating them?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is where Factories come in.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem: Object Creation Becomes Complex
&lt;/h2&gt;

&lt;p&gt;At first, object creation looks simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CardPayment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in real systems, creation logic becomes complex:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;multiple implementations exist&lt;/li&gt;
&lt;li&gt;selection depends on input or configuration&lt;/li&gt;
&lt;li&gt;dependencies also need to be constructed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If this logic is scattered everywhere, the system becomes inconsistent.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bad Design: Creation Inside Business Logic
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;card&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CardPayment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;upi&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UpiPayment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  What’s wrong here?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;creation logic is mixed with business logic&lt;/li&gt;
&lt;li&gt;violates Open-Closed Principle&lt;/li&gt;
&lt;li&gt;adding new types requires modifying existing code&lt;/li&gt;
&lt;li&gt;duplication across services is likely&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What is a Factory?
&lt;/h2&gt;

&lt;p&gt;A factory is responsible for:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Centralizing and controlling object creation logic.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It hides complexity and returns ready-to-use objects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Simple Factory Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;card&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;CardPayment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;upi&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;UpiPayment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PaymentFactory&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;card&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What Changed?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;object creation is centralized&lt;/li&gt;
&lt;li&gt;business logic is clean&lt;/li&gt;
&lt;li&gt;new implementations can be added in one place&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why Factories Matter
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Centralized Creation Logic
&lt;/h3&gt;

&lt;p&gt;All object creation rules live in one place.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Better Maintainability
&lt;/h3&gt;

&lt;p&gt;Changes do not spread across the system.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Controlled Instantiation
&lt;/h3&gt;

&lt;p&gt;Ensures correct object construction.&lt;/p&gt;




&lt;h2&gt;
  
  
  Factory vs Dependency Injection
&lt;/h2&gt;

&lt;p&gt;These are often confused, but they solve different problems:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Dependency Injection&lt;/th&gt;
&lt;th&gt;Factory&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;provides objects&lt;/td&gt;
&lt;td&gt;creates objects&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;focuses on usage&lt;/td&gt;
&lt;td&gt;focuses on creation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;reduces coupling&lt;/td&gt;
&lt;td&gt;centralizes instantiation&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Real System Example
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Notification System
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;EmailNotifier&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sms&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SmsNotifier&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now services don’t care how objects are created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;notifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;notifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Key Design Insight
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Factories control object creation, ensuring consistency and isolating construction logic from business logic.&lt;/p&gt;
&lt;/blockquote&gt;




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

&lt;p&gt;Use factories when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;multiple implementations exist&lt;/li&gt;
&lt;li&gt;object creation involves logic or conditions&lt;/li&gt;
&lt;li&gt;construction is complex or dependent on configuration&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  When NOT to Use Factories
&lt;/h2&gt;

&lt;p&gt;Avoid factories when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;object creation is trivial&lt;/li&gt;
&lt;li&gt;no variation exists&lt;/li&gt;
&lt;li&gt;abstraction adds unnecessary complexity&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Common Mistake
&lt;/h2&gt;

&lt;p&gt;Beginners often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;put business logic inside factories&lt;/li&gt;
&lt;li&gt;overuse factories for simple objects&lt;/li&gt;
&lt;li&gt;confuse factory with dependency injection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Factories should only handle creation, nothing else.&lt;/p&gt;




&lt;h2&gt;
  
  
  Design Principle
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Separate object creation from object usage to maintain clean and scalable architecture.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Factories centralize object creation logic, keeping business code clean and focused on behavior.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Object-Oriented Design: Dependency Injection (Decoupling System Design)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Tue, 26 May 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-object-oriented-design-dependency-injection-decoupling-system-design-5d0n</link>
      <guid>https://dev.to/saras_growth_space/lld-object-oriented-design-dependency-injection-decoupling-system-design-5d0n</guid>
      <description>&lt;p&gt;After defining contracts using interfaces and abstract classes, the next problem appears naturally in real systems:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How do we connect all these components without tightly coupling them?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is where Dependency Injection (DI) becomes essential.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem Without Dependency Injection
&lt;/h2&gt;

&lt;p&gt;When a class creates its own dependencies, it becomes tightly coupled.&lt;/p&gt;




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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CardPayment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  What’s wrong here?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;OrderService depends on a specific implementation&lt;/li&gt;
&lt;li&gt;You cannot easily switch payment methods&lt;/li&gt;
&lt;li&gt;Testing becomes difficult&lt;/li&gt;
&lt;li&gt;Changes in one class affect others&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This creates rigid systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is Dependency Injection?
&lt;/h2&gt;

&lt;p&gt;Dependency Injection means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Providing dependencies from the outside instead of creating them inside the class.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Correct Design
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the dependency is &lt;strong&gt;injected externally&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CardPayment&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UpiPayment&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What Changed?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;OrderService no longer decides what payment system to use&lt;/li&gt;
&lt;li&gt;responsibility is moved outside&lt;/li&gt;
&lt;li&gt;system becomes flexible&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why Dependency Injection Matters
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Loose Coupling
&lt;/h3&gt;

&lt;p&gt;Classes do not depend on concrete implementations.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Easy Testing
&lt;/h3&gt;

&lt;p&gt;Mock objects can replace real dependencies.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Flexibility
&lt;/h3&gt;

&lt;p&gt;You can change behavior without modifying core logic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real System Example
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Notification System
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;notifier&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;notifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notifier&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;notifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now different implementations can be injected:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;EmailNotifier&lt;/li&gt;
&lt;li&gt;SmsNotifier&lt;/li&gt;
&lt;li&gt;PushNotifier&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Design Insight
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Classes should focus on what they do, not what they depend on.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Dependency Injection vs Object Creation
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Without DI&lt;/th&gt;
&lt;th&gt;With DI&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;class creates dependencies&lt;/td&gt;
&lt;td&gt;dependencies passed externally&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tight coupling&lt;/td&gt;
&lt;td&gt;loose coupling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;hard to test&lt;/td&gt;
&lt;td&gt;easy to test&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rigid design&lt;/td&gt;
&lt;td&gt;flexible design&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Where Object Creation Should Happen
&lt;/h2&gt;

&lt;p&gt;If classes don’t create dependencies, then who does?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Caller&lt;/li&gt;
&lt;li&gt;Factory&lt;/li&gt;
&lt;li&gt;Composition Root (central setup layer)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This separation is critical for clean architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistake
&lt;/h2&gt;

&lt;p&gt;Beginners often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mix creation logic inside business classes&lt;/li&gt;
&lt;li&gt;overuse DI without structure&lt;/li&gt;
&lt;li&gt;confuse DI with factories&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Important Clarification
&lt;/h2&gt;

&lt;p&gt;Dependency Injection is NOT:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a framework&lt;/li&gt;
&lt;li&gt;a design pattern for creation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is a &lt;strong&gt;design principle for decoupling&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Design Principle
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Depend on abstractions, not concrete implementations — and receive dependencies instead of creating them.&lt;/p&gt;
&lt;/blockquote&gt;




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

&lt;p&gt;Think of a mobile phone:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it does not create a SIM card internally&lt;/li&gt;
&lt;li&gt;SIM is inserted externally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The phone works regardless of SIM provider.&lt;/p&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Dependency Injection decouples system components by moving responsibility of object creation outside the class.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
      <category>codinginterview</category>
    </item>
    <item>
      <title>LLD Object-Oriented Design: Interfaces &amp; Abstract Classes (Designing Contracts)</title>
      <dc:creator>Saras Growth Space</dc:creator>
      <pubDate>Mon, 25 May 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/saras_growth_space/lld-object-oriented-design-interfaces-abstract-classes-designing-contracts-138f</link>
      <guid>https://dev.to/saras_growth_space/lld-object-oriented-design-interfaces-abstract-classes-designing-contracts-138f</guid>
      <description>&lt;p&gt;After understanding composition, inheritance, and polymorphism, the next important step is learning how to define &lt;strong&gt;clear behavioral contracts&lt;/strong&gt; between components.&lt;/p&gt;

&lt;p&gt;This is where interfaces and abstract classes become essential.&lt;/p&gt;

&lt;p&gt;They help answer one of the most important questions in system design:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“What should this component be capable of doing?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;without tightly coupling the system to a specific implementation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Contracts Matter in System Design
&lt;/h2&gt;

&lt;p&gt;In real systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;multiple implementations exist&lt;/li&gt;
&lt;li&gt;components evolve independently&lt;/li&gt;
&lt;li&gt;implementations may change over time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If systems depend directly on concrete classes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;flexibility decreases&lt;/li&gt;
&lt;li&gt;testing becomes difficult&lt;/li&gt;
&lt;li&gt;coupling increases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Contracts solve this problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is an Interface?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;An interface defines:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A capability contract that implementing classes must follow.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It specifies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what operations are available&lt;/li&gt;
&lt;li&gt;not how they are implemented&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any implementation must provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;pay()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But implementation details remain independent.&lt;/p&gt;




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

&lt;blockquote&gt;
&lt;p&gt;Interfaces define capability expectations, not implementation details.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is an Abstract Class?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;An abstract class provides:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Partial implementation along with abstract behavior contracts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is useful when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;some logic is common across implementations&lt;/li&gt;
&lt;li&gt;some behavior varies by subclass&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Notification&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Logging notification&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;log()&lt;/code&gt; → shared behavior&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;send()&lt;/code&gt; → implementation-specific behavior&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Interface vs Abstract Class
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Interface&lt;/th&gt;
&lt;th&gt;Abstract Class&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;defines contract&lt;/td&gt;
&lt;td&gt;defines partial behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;focuses on capability&lt;/td&gt;
&lt;td&gt;focuses on shared structure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;minimal implementation&lt;/td&gt;
&lt;td&gt;can contain implementation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;enables loose coupling&lt;/td&gt;
&lt;td&gt;enables reuse + extension&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Key Mental Model
&lt;/h2&gt;

&lt;p&gt;Think of it this way:&lt;/p&gt;

&lt;h3&gt;
  
  
  Interface
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“What can this object do?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Abstract Class
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“What common behavior already exists?”&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Real System Example
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Payment System&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Contract
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Implementations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CardPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Processing card payment&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UpiPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Processing UPI payment&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Payment&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the system depends on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;abstraction
not:&lt;/li&gt;
&lt;li&gt;concrete implementation&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why Interfaces Matter in LLD
&lt;/h2&gt;

&lt;p&gt;Interfaces help achieve:&lt;/p&gt;




&lt;h3&gt;
  
  
  1. Loose Coupling
&lt;/h3&gt;

&lt;p&gt;Components communicate through contracts instead of implementations.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Extensibility
&lt;/h3&gt;

&lt;p&gt;New implementations can be added safely.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Testability
&lt;/h3&gt;

&lt;p&gt;Mock implementations become easy.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;MockPayment&lt;/li&gt;
&lt;li&gt;FakeNotificationService&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  4. System Scalability
&lt;/h3&gt;

&lt;p&gt;Independent teams can build implementations separately while following the same contract.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Abstract Classes Matter
&lt;/h2&gt;

&lt;p&gt;Abstract classes help when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;logic is partially shared&lt;/li&gt;
&lt;li&gt;duplication needs reduction&lt;/li&gt;
&lt;li&gt;related classes have common behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They provide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;controlled reuse&lt;/li&gt;
&lt;li&gt;shared structure&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Common Mistake
&lt;/h2&gt;

&lt;p&gt;Beginners often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create interfaces for everything&lt;/li&gt;
&lt;li&gt;or avoid abstractions entirely&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both create problems.&lt;/p&gt;




&lt;h3&gt;
  
  
  Over-Abstraction
&lt;/h3&gt;

&lt;p&gt;Too many unnecessary interfaces lead to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;complexity&lt;/li&gt;
&lt;li&gt;indirection&lt;/li&gt;
&lt;li&gt;unreadable systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Under-Abstraction
&lt;/h3&gt;

&lt;p&gt;No contracts lead to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tight coupling&lt;/li&gt;
&lt;li&gt;rigid systems&lt;/li&gt;
&lt;li&gt;difficult testing&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Design Principle
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Depend on abstractions for flexibility, but introduce abstractions only when variation or decoupling is actually needed.&lt;/p&gt;
&lt;/blockquote&gt;




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

&lt;p&gt;Think of a charging socket.&lt;/p&gt;

&lt;p&gt;The socket defines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;voltage standard&lt;/li&gt;
&lt;li&gt;connection contract&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Different chargers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Apple&lt;/li&gt;
&lt;li&gt;Samsung&lt;/li&gt;
&lt;li&gt;Laptop adapters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;all work because they follow the same contract.&lt;/p&gt;

&lt;p&gt;The socket does not care about internal implementation.&lt;/p&gt;




&lt;h2&gt;
  
  
  One-Line Takeaway
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Interfaces define what a component must be capable of doing, while abstract classes help share common behavior across related implementations.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>lld</category>
      <category>systemdesign</category>
      <category>softwareengineering</category>
      <category>codinginterview</category>
    </item>
  </channel>
</rss>
