Modern Java Design Patterns: A Comprehensive Guide for Java 21–25 is a ground-up rewrite of the design patterns canon for the era of records, sealed classes, virtual threads, and pattern matching for switch. Written for professional Java developers who already know the classics and want to write them the way Java now intends, the book works through every major GoF pattern and a curated set of modern concurrency and functional patterns — each presented with a clear Classic versus Modern comparison so you always understand not just the what, but the why of each evolution.
What's inside The book is organised into eight parts that build from language foundations through to production-ready patterns and an appendix suite designed for day-to-day reference.
Part I opens with the modern Java foundations that underpin every pattern in the book: records, sealed interfaces, pattern matching for instanceof and switch, virtual threads, ScopedValue, and StructuredTaskScope. Readers who are already fluent in these features can treat Part I as a quick reference; those newer to the Java 21+ landscape will find a concise, example-driven orientation that pays dividends across every chapter that follows.
Parts II through IV cover the full GoF catalogue — Creational, Structural, and Behavioural patterns — methodically. Each chapter opens with the classic implementation, identifies exactly which friction points modern Java eliminates, and presents a rewritten version using the most appropriate current-language idioms. The Singleton chapter, for instance, moves from double-checked locking to the enum-based approach and then to the injection-framework model. The Strategy chapter replaces the classic interface-and-class hierarchy with a sealed interface and records dispatched by an exhaustive switch expression. The Visitor chapter trades the accept/visit ceremony for a Function over a sealed hierarchy with pattern matching — cutting the code by more than half while gaining compile-time exhaustiveness checking the original pattern never had.
Part V addresses concurrency patterns with the full weight of Project Loom behind them. The chapters on Virtual Thread Executor, Structured Concurrency, and ScopedValue-based context propagation replace patterns that were necessarily complex when platform threads were the only option. The ThreadLocal memory-leak anti-pattern is covered in depth, with a complete migration guide to ScopedValue that goes beyond toy examples to show how context inheritance works inside a StructuredTaskScope.
Part VI introduces patterns that have no direct GoF equivalent because they arise from Java's functional and algebraic capabilities: Algebraic Data Types built from sealed classes and records, Result/Either types for railway-oriented error handling without exceptions, and functional pipeline composition using the Streams API and function combinators.
Part VII is a dedicated anti-patterns chapter — an unusual inclusion that reflects how patterns can be applied incorrectly just as easily as correctly. It covers misuses specific to modern Java: breaking record immutability through mutable components, pooling virtual threads (which defeats their purpose), reaching for ReentrantLock when synchronized is now the right choice post-JEP 491, and over-applying pattern matching in hierarchies that would be better served by polymorphism. Each anti-pattern is shown with a concrete before-and-after so the correction is unambiguous.
Part VIII provides three reference appendices: a Java version feature matrix from Java 16 through 25 showing exactly when each language feature was finalized; a pattern decision tree for navigating to the right pattern from a plain-English problem statement; and a before/after cheat sheet mapping every classic pattern to its modern counterpart at a glance. A fourth appendix covers IDE configuration for preview features in both IntelliJ IDEA and VS Code, including the Maven and Gradle build file changes required to enable preview compilation.