System Design
Object-Oriented Design
Library Management

Design Example: Library Management System

A Library Management System tracks book inventory, user memberships, and the lifecycle of book loans (issue, return, renew, fines).


Step 1: Requirements Gathering

Core Use Cases:

  • Search: Find books by title, author, subject, or publication date.
  • Member Management: Register members and librarians.
  • Book Issuance: A member can borrow up to 5 books for 10 days.
  • Rules & Fines: Calculate fines for overdue books ($1 per day).
  • Reservation: A member can reserve a book if it's currently loaned out.

Entity States:

  • BookItem Status: Available, Reserved, Loaned, Lost.
  • Account Status: Active, Closed, Canceled, Blacklisted.

Step 2: Identify Core Objects

  • Book: Metadata like ISBN, Title, Author.
  • BookItem: A specific physical copy of a Book.
  • Account: Base class for Member and Librarian.
  • LibraryCard: Stores member identification.
  • BookLending: Tracks a specific loan (issue date, due date).
  • Search: Interface for various search implementations.

Step 3: Design Class Diagram


Step 4: Implementation in TypeScript

enum BookStatus { AVAILABLE, RESERVED, LOANED, LOST }
 
class Book {
  constructor(
    public isbn: string,
    public title: string,
    public author: string
  ) {}
}
 
class BookItem extends Book {
  private barcode: string;
  private status: BookStatus = BookStatus.AVAILABLE;
 
  public checkout(memberId: string): boolean {
    if (this.status !== BookStatus.AVAILABLE) return false;
    this.status = BookStatus.LOANED;
    return true;
  }
}
 
class Fine {
  public static calculateFine(daysOverdue: number): number {
    const RATE_PER_DAY = 1.0;
    return daysOverdue * RATE_PER_DAY;
  }
}
 
interface Search {
  searchByTitle(title: string): BookItem[];
  searchByAuthor(author: string): BookItem[];
}

Deep Dive: Strategy Pattern for Search

Different search strategies can be plugged into the system without modifying the main library logic.

class BookSearch implements Search {
  public searchByTitle(title: string): BookItem[] {
    // Logic to search in DB or Catalog
    return [];
  }
  
  public searchByAuthor(author: string): BookItem[] {
    // Logic to search by author
    return [];
  }
}

Wrap Up

The Library Management System is a classic example of Composition vs. Inheritance. While a Member and Librarian are Accounts (Inheritance), a Book has multiple BookItems (Composition). This distinction is critical for handling real-world inventory.

[!TIP] Use the Observer Pattern to notify members when a reserved book becomes available.