The Visitor design pattern is a classic solution for operating on complex object hierarchies- but in traditional Java, it often comes with a significant drawback: verbose, repetitive, and hard-to-maintain boilerplate code.
In this XtremeJ 2025 session, Haim Michael revisits the Visitor pattern through the lens of modern Java, demonstrating how recent language features fundamentally change how the pattern can be implemented.
By combining sealed classes, record classes, and pattern matching with switch expressions, this talk shows how the Visitor pattern can be expressed in a way that is:
+ More concise
+ Easier to reason about
+ Safer and more explicit
+ Significantly easier to maintain over time
+ The slides walk step by step through:
+ The original problem the Visitor pattern is designed to solve
+ A traditional, textbook Visitor implementation in Java
+ A modern alternative using sealed interfaces and records
+ How pattern matching removes the need for double dispatch
+ Why modern Java enables cleaner, centralized, and more expressive logic
Key takeaways include:
+ How sealed classes enforce complete and controlled hierarchies
+ How records drastically reduce structural boilerplate
+ How pattern matching ensures exhaustiveness and clarity
+ Why modern implementations improve readability, performance, and maintainability
+ When this approach is a better fit than the classic Visitor pattern
This session offers a fresh, practical perspective on a well-known design pattern, showing how Java’s evolution empowers developers to modernize legacy designs without sacrificing architectural intent.
These slides were presented at XtremeJ 2025, the online Java conference for professional developers.
About the Conference:
More information about the XtremeJ 2025 online conference is available at:
https://xtremej.dev