0
0
LLDsystem_design~7 mins

UML class diagrams basics in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When designing software, teams often struggle to communicate the structure of classes and their relationships clearly. Without a common visual language, misunderstandings arise, leading to inconsistent implementations and harder maintenance.
Solution
UML class diagrams provide a simple, standardized way to visualize classes, their attributes, methods, and relationships. This visual map helps everyone understand the system's structure before coding, reducing errors and improving collaboration.
Architecture
┌─────────────┐      ┌─────────────┐
│   Person    │      │   Address   │
├─────────────┤      ├─────────────┤
│ - name      │      │ - street    │
│ - age       │      │ - city      │
├─────────────┤      ├─────────────┤
│ + getName() │      │ + getCity() │
│ + getAge()  │      └─────────────┘
└─────┬───────┘           ▲
      │ 1                 │ 1
      │                   │
      │                   │
      └───────────────────┘
         has an address

This diagram shows two classes, Person and Address, with their attributes and methods. It also shows a one-to-one association where a Person has an Address.

Trade-offs
✓ Pros
Provides a clear, visual representation of class structure and relationships.
Standardized notation understood by many developers and architects.
Helps identify design issues early before coding.
Improves team communication and documentation.
✗ Cons
Can become complex and hard to read for very large systems.
Requires learning UML notation which may be new to some team members.
Static diagrams do not show runtime behavior or dynamic interactions.
Use UML class diagrams when designing object-oriented systems with multiple classes and relationships, especially in teams needing clear communication and documentation.
Avoid using UML class diagrams for very small projects with few classes or when rapid prototyping is prioritized over upfront design.
Real World Examples
Amazon
Amazon uses UML class diagrams to design their order processing system, clarifying relationships between orders, customers, and products before implementation.
LinkedIn
LinkedIn applies UML class diagrams to model user profiles and connections, ensuring consistent data structures across services.
Uber
Uber uses UML class diagrams to visualize driver, rider, and trip classes, helping coordinate development across teams.
Code Example
The before code mixes address details inside Person, making it hard to manage. The after code separates Address as its own class, showing a clear one-to-one relationship. This matches the UML class diagram structure and improves clarity and reusability.
LLD
### Before: No clear class structure or relationships
class Person:
    def __init__(self, name, age, street, city):
        self.name = name
        self.age = age
        self.street = street
        self.city = city

### After: Clear classes with attributes and relationships
class Address:
    def __init__(self, street: str, city: str):
        self.street = street
        self.city = city

    def get_city(self) -> str:
        return self.city

class Person:
    def __init__(self, name: str, age: int, address: Address):
        self.name = name
        self.age = age
        self.address = address

    def get_name(self) -> str:
        return self.name

    def get_age(self) -> int:
        return self.age

    def get_address_city(self) -> str:
        return self.address.get_city()
OutputSuccess
Alternatives
Entity-Relationship Diagrams (ERD)
ERDs focus on database entities and their relationships rather than object-oriented classes and methods.
Use when: Choose ERDs when designing database schemas rather than application class structures.
Object Role Modeling (ORM)
ORM models data semantics and constraints more expressively but is less widely adopted than UML.
Use when: Use ORM when detailed semantic modeling of data is required beyond class structure.
Summary
UML class diagrams visually represent classes and their relationships to improve design clarity.
They help teams communicate and document object-oriented structures before coding.
While powerful, they focus on static structure and can become complex if overused.