Kick off your book project in 3 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Saturday, May 2, 2026. Learn more…

Leanpub Header

Skip to main content

Design Patterns in Modern Java

Java has changed. Have your patterns kept up?

The Gang of Four wrote their landmark patterns in 1994 — when Java didn't exist, generics were a decade away, and "concurrency" meant carefully managing a handful of platform threads. Thirty years later, Java 21 through 25 has transformed the language: records replace boilerplate classes, sealed interfaces model closed type hierarchies with compiler enforcement, pattern matching eliminates entire categories of unsafe casting, and virtual threads make a million concurrent tasks not just possible but routine.

Modern Java Design Patterns bridges that gap. Every classic pattern is shown first in its original form, then systematically rebuilt with the language features that exist today. The result is code that is shorter, safer, more expressive, and immediately recognisable to any team working on a modern Java codebase.

Minimum price

$25.99

$28.96

You pay

$28.96

Author earns

$23.16
$
You can also buy this book with 1 book credit. Get book credits with a Reader Membership or an Organization Membership for your team.
PDF
EPUB
WEB
About

About

About the Book

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.

Share this book

Categories

Author

About the Author

Contents

Table of Contents

Part I: Foundations — What Changed and Why

Chapter 1: The Modern Java Revolution

Chapter 2: Records — Immutability as the Default

Chapter 3: Sealed Classes — Exhaustive Type Hierarchies

Chapter 4: Pattern Matching — From instanceof to Deconstruction

Chapter 5: Virtual Threads — Rethinking Concurrency Patterns

Part II: Creational Patterns — Building Objects the Modern Way

Chapter 6: Singleton Pattern

Chapter 7: Builder Pattern

Chapter 8: Factory Method & Abstract Factory

Chapter 9: Prototype Pattern

Chapter 10: Object Pool Pattern

Part III: Structural Patterns — Composing Objects

Chapter 11: Adapter Pattern

Chapter 12: Decorator Pattern

Chapter 13: Proxy Pattern

Chapter 14: Composite Pattern

Chapter 15: Flyweight Pattern

Chapter 16: Facade Pattern

Chapter 17: Bridge Pattern

Part IV: Behavioral Patterns — How Objects Interact

Chapter 18: Strategy Pattern

Chapter 19: State Pattern

Chapter 20: Observer Pattern

Chapter 21: Command Pattern

Chapter 22: Iterator Pattern

Chapter 23: Mediator Pattern

Chapter 24: Memento Pattern

Chapter 25: Chain of Responsibility Pattern

Chapter 26: Interpreter Pattern

Chapter 27: Command Query Responsibility Segregation (CQRS)

Chapter 28: Event Sourcing

Part V: Concurrency Patterns — Virtual Threads Change Everything

Chapter 29: Thread Pool Pattern (Obsolete for I/O)

Chapter 30: Active Object / Actor Pattern

Chapter 31: Future/Promise Pattern (Async to Sync)

Chapter 32: ThreadLocal Pattern

Chapter 33: Balking Pattern

Part VI: Modern Patterns — New to Java 21-25

Chapter 34: Functional Programming Patterns

Chapter 35: Data-Oriented Programming (DOP) Patterns

Chapter 36: Builder-like Patterns with Records and with methods

Chapter 37: Algebraic Data Types (ADTs) with Sealed Classes and Records

Chapter 38: Functional Error Handling with Either or Result types

Part VII: Anti-Patterns — What to Avoid in Modern Java

Chapter 39: Modern Java Anti-Patterns

Chapter 40: Migrating Legacy Patterns

Part VIII: Appendices

  1. Appendix A: Java Version Feature Matrix
  2. Appendix B: Before/After Cheat Sheet
  3. Appendix C: IDE Setup for Modern Java

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub