Bird
0
0
LLDsystem_design~7 mins

Class design (Book, Member, Librarian, Loan) in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When designing a library management system, poor class design leads to tightly coupled code, making it hard to maintain or extend. Without clear responsibilities, classes become bloated or overlap, causing bugs and confusion.
Solution
Divide the system into clear classes representing real-world entities: Book, Member, Librarian, and Loan. Each class handles its own data and behavior, communicating through well-defined interfaces. This separation keeps code organized and easier to update or expand.
Architecture
┌─────────┐       ┌───────────┐       ┌────────────┐       ┌─────────┐
│  Member │──────▶│    Loan   │◀──────│    Book    │       │Librarian│
└─────────┘       └───────────┘       └────────────┘       └─────────┘
       │                                                        ▲
       │                                                        │
       └────────────────────────────────────────────────────────┘

This diagram shows the relationships: Members borrow Books via Loans, Librarians manage the system, and Loans connect Members and Books.

Trade-offs
✓ Pros
Clear separation of concerns improves maintainability and readability.
Classes map closely to real-world concepts, making the system intuitive.
Easier to add new features like overdue fines or reservations by extending classes.
✗ Cons
Initial design requires careful thought to avoid missing important attributes or methods.
Too many small classes can increase complexity if not managed well.
Improper encapsulation can still lead to tight coupling if classes access each other's internals directly.
Use when building systems that model real-world entities with distinct roles and behaviors, especially if the system will grow or change over time.
Avoid for very simple scripts or prototypes where full object-oriented design adds unnecessary complexity.
Real World Examples
Amazon
Models products, customers, and orders as separate classes to manage their online marketplace efficiently.
LinkedIn
Uses clear class designs for users, connections, and messages to handle complex social networking features.
Netflix
Represents movies, users, and viewing sessions as distinct classes to personalize recommendations and track usage.
Code Example
The before code mixes all responsibilities in one class, making it hard to maintain. The after code separates entities into classes with clear roles: Book tracks availability, Member tracks loans, Loan connects them, and Librarian manages loans. This improves clarity and extensibility.
LLD
### Before: Poor class design with mixed responsibilities
class Library:
    def __init__(self):
        self.books = []
        self.members = []
        self.loans = []

    def add_book(self, book):
        self.books.append(book)

    def add_member(self, member):
        self.members.append(member)

    def loan_book(self, book, member):
        if book.available:
            book.available = False
            self.loans.append({'book': book, 'member': member})

### After: Clear class design with responsibilities separated
class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author
        self.available = True

class Member:
    def __init__(self, name):
        self.name = name
        self.loans = []

class Loan:
    def __init__(self, book, member):
        self.book = book
        self.member = member
        self.active = True
        book.available = False
        member.loans.append(self)

class Librarian:
    def __init__(self, name):
        self.name = name

    def loan_book(self, book, member):
        if book.available:
            return Loan(book, member)
        else:
            return None
OutputSuccess
Alternatives
Procedural Programming
Focuses on functions and data separately without bundling them into classes.
Use when: Choose when the system is very simple or performance-critical with minimal state management.
Entity-Component-System (ECS)
Separates data (components) from behavior (systems) rather than using classes with both.
Use when: Choose for game development or highly dynamic systems needing flexible composition.
Summary
Good class design separates system entities into distinct classes with clear responsibilities.
This separation makes the code easier to maintain, extend, and understand.
Avoid mixing unrelated behaviors in one class to prevent complexity and bugs.