Design a Library Management System
LLD Case Studies
This question tests your ability to model a system with different types of users and complex rules. The goal is to design a system to manage the catalog of a library and the borrowing of books by its members.
1. Clarify Requirements
- What are the primary functions? The system should be able to add new books, search for books, and manage user accounts.
- Who are the users? There are two types of users:
Librarian
(an admin) andMember
. - What can a Librarian do? Add/remove books, add/remove members, and view all borrowed books.
- What can a Member do? Search for books, borrow books, and return books.
- What are the rules for borrowing? A member can borrow up to a certain limit (e.g., 5 books). A book cannot be borrowed if it's already checked out.
- How is the book catalog organized? Books can be searched by title, author, or subject.
- Is there a system for fines? Yes, if a book is returned after its due date, a fine is calculated.
2. Identify the Core Classes and Objects
Library
: The main class that manages the catalog and user accounts.Book
: Represents a single book.BookItem
: Represents a specific copy of a book (since a library can have multiple copies). This is a key distinction.User
: An abstract class for users.Librarian
,Member
: Subclasses ofUser
.Catalog
: A class dedicated to searching the book collection.BookLending
: A record that tracks which member has borrowed whichBookItem
and when it's due.
3. Design the System - Class Diagram
4. Key Design Decisions & Implementation Details
Book
vs. BookItem
:
This is a critical design choice. A Book
is a conceptual entity (e.g., "The Lord of the Rings"), while a BookItem
is a physical copy with a unique barcode.
public class Book {
private String title;
private String author;
private String ISBN;
// ...
}
public class BookItem extends Book {
private String barcode;
private Date issueDate;
private BookStatus status; // e.g., AVAILABLE, RESERVED, LOANED
}
The Catalog
Class:
This class encapsulates the searching logic, adhering to the Single Responsibility Principle.
public class Catalog implements Searchable {
private Map<String, List<Book>> bookTitles;
private Map<String, List<Book>> bookAuthors;
@Override
public List<Book> searchByTitle(String title) {
return bookTitles.get(title);
}
// ... other search methods
}
Interview Focus: Key Discussion Points
- Single Responsibility Principle: Point out how you've separated concerns. The
Catalog
is only for searching. TheLibrary
class is for high-level management. TheBookLending
class is only for tracking loans. - User Roles and Permissions: How would you handle the different permissions for
Librarian
andMember
? You could use a simple role check, or for a more complex system, you could introduce aPermission
class or even use the Decorator Pattern to add responsibilities toUser
objects dynamically. - Scalability: If the library has millions of books, a simple
Map
for the catalog might consume too much memory. You could discuss using a database with proper indexing on thetitle
andauthor
columns to make searching efficient. - Concurrency: What if two members try to borrow the last copy of a book at the same time? The
borrowBook
method in theLibrary
class should besynchronized
or use a locking mechanism on the specificBookItem
to ensure that only one user can successfully borrow it.