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.
Librarian (an admin) and Member.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 of User.Catalog: A class dedicated to searching the book collection.BookLending: A record that tracks which member has borrowed which BookItem and when it's due.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
}
Catalog is only for searching. The Library class is for high-level management. The BookLending class is only for tracking loans.Librarian and Member? You could use a simple role check, or for a more complex system, you could introduce a Permission class or even use the Decorator Pattern to add responsibilities to User objects dynamically.Map for the catalog might consume too much memory. You could discuss using a database with proper indexing on the title and author columns to make searching efficient.borrowBook method in the Library class should be synchronized or use a locking mechanism on the specific BookItem to ensure that only one user can successfully borrow it.