Lesson 6 of 6
Mini Project: Library System - Part 11 (Design Patterns Applied)
Apply all patterns to the Library System: Singleton for AppConfig, Builder for BookOrder, Decorator for repository with logging and caching, Observer for audit and email, Strategy for flexible search, Command pattern for undo-able checkouts, and a fully functional CLI front-end.
Integrated Architecture
Integrated Architecture
// Full architecture with all patterns applied:
// 1. SINGLETON - one AppConfig
AppConfig config = AppConfig.getInstance();
// 2. REPOSITORY with DECORATORS stacked
BookRepository base = new JdbcBookRepository(config.getDbUrl());
BookRepository logged = new LoggingBookRepository(base);
BookRepository cached = new CachingBookRepository(logged);
// All service code uses 'BookRepository' interface - swappable!
// 3. OBSERVER - notification bus
LibrarySubject bus = new LibrarySubject();
bus.subscribe(LibraryEvent.BOOK_BORROWED, new AuditLogger());
bus.subscribe(LibraryEvent.BOOK_BORROWED, new EmailService());
bus.subscribe(LibraryEvent.BOOK_RETURNED, new AuditLogger());
// 4. STRATEGY - search strategy selection
SearchService search = new SearchService();
List<Book> results = search.search("Bloch", "author", catalog);
// 5. COMMAND + HISTORY - undo-able operations
CommandHistory history = new CommandHistory();
Book book = cached.findByIsbn("001").orElseThrow();
history.execute(new BorrowCommand(book, "MBR-001"));
// Whoops - member changed their mind:
history.undo(); // book is returned automatically
// 6. BUILDER - readable order construction
BookOrder order = new BookOrder.Builder("001", 1)
.memberId("MBR-001")
.unitPrice(39.99)
.priority()
.build();
// 7. CLI FRONT-END
LibraryService service = new LibraryService(cached, bus);
new LibraryCli(service).run();
// Pattern summary table:
// Pattern | Problem it solves
// ────────────┼──────────────────────────────────────────────
// Singleton | One AppConfig instance across JVM
// Builder | Multi-field BookOrder without telescoping ctors
// Decorator | Add logging/caching to repo without modifying it
// Adapter | Legacy file-based system → BookRepository
// Strategy | Swappable search: title, author, ISBN, genre
// Observer | Decouple audit/email from core checkout logic
// Command | Undo-able borrow/return operations
// Factory | Notification type creation (email/SMS/push)
// Facade | LibraryCli hides all subsystem complexityDiagram
Loading diagram…
Visual architecture and concepts for Mini Project: Library System - Part 11 (Design Patterns Applied)
Quick Quiz
Module 11 Quiz: Design Patterns & SOLID
Module 11 Quiz: Design Patterns & SOLID
Question 1 of 5What does the Singleton pattern ensure?
Finished this lesson?
0 of 6 in Module 11