Quick revision
The short answer
A long-lived codebase should let pattern-heavy code converge on modern constructs gradually: leave stable, working code alone, refactor an area to lambdas, records or sealed switches when we are already changing it for another reason, and write clear guidance so all new code uses the modern form. Mass rewrites carry real risk, because they create huge diffs nobody can review well and regressions that buy users nothing, and automated recipes such as OpenRewrite lower that risk without removing it. Public APIs stay stable while internals change first, and we track each repository's Java language level so upgrades are planned rather than accidental.
What to remember
In this answer, we saw that lambdas, records and sealed switches absorb Strategy, Command and Visitor, but that working, stable pattern code has little reason to change. The safe path is to refactor when we are already changing an area, in separate behaviour-preserving commits; to avoid mass rewrites, whose review fatigue and regressions buy users nothing even when recipes like OpenRewrite automate them; to keep public APIs stable while internals change first; and to drive convergence through guidance for new code and per-repository tracking of language levels.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A long-lived codebase should let pattern-heavy code converge on modern constructs gradually: leave stable, working code alone, refactor an area to lambdas, records or sealed switches when we are already changing it for another reason, and write clear guidance so all new code uses the modern form. Mass rewrites carry real risk, because they create huge diffs nobody can review well and regressions that buy users nothing, and automated recipes such as OpenRewrite lower that risk without removing it. Public APIs stay stable while internals change first, and we track each repository's Java language level so upgrades are planned rather than accidental.
Background to this question
Many Java codebases are ten or fifteen years old. They were written when the Gang of Four patterns were the main way to express behaviour as objects: Strategy interfaces with one method, Command classes, Visitors over class hierarchies, anonymous inner classes everywhere. Java 8 lambdas, Java 16 records and Java 21 sealed types with pattern matching now express much of that more directly, and engineers naturally want to modernise. A senior person is asked this question because the decision is not technical alone: it decides where a team spends months of effort and how much production risk it accepts. In this answer, we will look at how the modern features absorb the old patterns, why working code deserves respect, how opportunistic refactoring works, what mass rewrites really cost, and how to steer a large organisation so the code converges over time.
1How Modern Java Absorbs the Old Patterns
A quick recap of what changed. The Strategy pattern, an interface with one method that lets callers plug in an algorithm, is exactly what a functional interface and a lambda express. The Command pattern, an object that captures a request so it can be queued or logged, often becomes a record holding the data, or a Runnable when only the action matters. The Visitor pattern, used to add operations over a closed hierarchy, can become an exhaustive switch over a sealed interface with record patterns.
Notice that the interface DiscountStrategy did not disappear. It is still the contract that callers depend on. Only the implementation got shorter. That observation is the core of a sensible migration policy: the shape of the code improves at the edges while the contracts that other code relies on stay put.
2Why Stable Code Has Little Reason to Change
Code that has run in production for years has absorbed every edge case its users ever hit. Its tests, its bug fixes and its quirks encode knowledge that nobody wrote down. A TenPercentOff class that is ten lines long, rarely read and never changed costs almost nothing to keep. Rewriting it into a lambda saves little and carries a small but real chance of changing behaviour, for example by losing a toString() that a log parser relied on, or by changing serialisation of a class that was stored in a session.
So the senior judgment is that modernisation is not a goal in itself. The value of a refactor is the reading and changing time it saves in the future, and that time is concentrated in the code people actually touch, which is a small share of files in most repositories.
3Refactor When You Are Already There
The practical rule is opportunistic refactoring, sometimes called the boy scout rule. When an engineer changes a class for a feature or a bug fix, they may also modernise that class and its close neighbours, in a separate commit inside the same pull request. The reason this is safer than a dedicated rewrite is that everything needed to verify the change is already in place: the engineer has just read and understood the area, the tests around it are being run and extended for the feature, and the reviewer is already looking at those files with full attention.
Two habits keep this honest. First, the modernising commit is a pure refactor with no behaviour change, so the reviewer can read it separately from the feature commit. Second, the scope stays local: converting the visitor inside the module we are changing is fine, converting the visitor in five other modules on the way is a separate piece of work with its own review.
- Are we already changing this area?YesConsider a local refactorNoLeave it alone
- Is it part of a public API?YesChange internals only, keep the APINoRefactor freely
- Do tests cover the behaviour?YesRefactor in a separate commitNoAdd tests first, or wait
- Review the refactor apart from the featurePure refactor, no behaviour change
4 steps in order. Play them, or step through them one at a time.
The decision a senior engineer runs through before modernising old pattern code.
4What a Mass Rewrite Really Costs
The tempting alternative is a campaign: one quarter, one team, every Strategy class turned into a lambda across two hundred repositories. The cost shows up in three places. Review fatigue comes first. A pull request that touches 400 files cannot be reviewed line by line, so reviewers approve it on trust, and the one change in four hundred that alters behaviour goes through unnoticed. Regressions come second: subtle differences such as an anonymous class's this meaning something different from a lambda's this, equality of strategy objects that a cache relied on, or serialised command objects stored in a queue or database. Third, there is opportunity cost. The users see no new value from the campaign, while the engineers spend weeks on it and merge conflicts with every team working in parallel.
Automated recipes change the picture but do not remove it. OpenRewrite, a tool that applies refactoring recipes to the syntax tree with type information, can convert anonymous classes to lambdas or migrate a codebase to a newer Java version consistently, and because the changes are mechanical they are far less error-prone than hand edits. But a recipe is only as good as its assumptions. It cannot know that a log parser depends on a class name or that a reflection-based framework looks for a concrete type. So even automated changes need the test suite to be trustworthy, small batches per repository, and a rollout that we can stop. A reasonable approach is to run a recipe on a few low-risk repositories, watch for a release cycle, and only then widen it.
5Keep Public APIs Stable and Change Internals First
Code that other teams or customers compile against deserves extra care. If our library exposes an ExprVisitor interface, replacing it with a sealed hierarchy and switches breaks every caller, and in the case of a sealed interface it also prevents callers from adding their own implementations. So the order is: modernise private and package-private code first, where we control every call site. For public APIs, add the new form alongside the old one, for example a method that accepts a Function next to the one that accepts the legacy interface, mark the old form deprecated with a clear replacement, and remove it only in a planned major version after callers have moved.
6Steering Convergence Across Teams
Opportunistic refactoring only converges if new code is written the modern way. So the second half of the policy is guidance: a short, versioned document with examples that says, for instance, new strategies are functional interfaces with lambdas, new value types are records, closed hierarchies are sealed with exhaustive switches and no default branch, and visitors remain for open extension points. Examples matter more than rules, because engineers copy what they see. Code review checklists and a few static analysis checks reinforce the guidance without anyone policing it by hand.
Finally, language features require a language level. A team cannot use record patterns on a repository compiled for Java 11. So we track, per repository, the JDK version it builds and runs with and the release level it compiles for, for example on a simple dashboard generated from build files. That turns upgrades into planned work: we can see that 30 of 200 repositories are still on Java 17, schedule them, and know which guidance applies where. Over two or three years, the combination of guidance for new code, refactoring on touch and planned upgrades moves the codebase forward without a single risky rewrite.
7Follow-up questions
When is a dedicated rewrite of pattern code justified?
When the old structure is actively costing us: the area changes every sprint and engineers keep making mistakes because of the indirection, or the old code blocks an upgrade, for example a framework migration. Then we treat it as a normal project with a goal, a test safety net built first, small reviewed steps, and a way to measure that change time or defect rate improved.
Are the old patterns now wrong?
No. The ideas behind them are still right; only the syntax got cheaper. A lambda is still a strategy, a record in a queue is still a command, and a sealed switch is still a way to add operations to a closed hierarchy. Engineers who know the patterns understand why the modern constructs exist and when the classic form, such as a visitor for an open hierarchy, is still the better fit.
8Conclusion
In this answer, we saw that lambdas, records and sealed switches absorb Strategy, Command and Visitor, but that working, stable pattern code has little reason to change. The safe path is to refactor when we are already changing an area, in separate behaviour-preserving commits; to avoid mass rewrites, whose review fatigue and regressions buy users nothing even when recipes like OpenRewrite automate them; to keep public APIs stable while internals change first; and to drive convergence through guidance for new code and per-repository tracking of language levels.
Open with
My starting point is that modern Java has absorbed a lot of the GoF patterns: a strategy is now a functional interface and a lambda, a command is often a record or a Runnable, and a visitor over a closed hierarchy can be a sealed interface with an exhaustive switch.
Then, if they want more
But old code that works and is stable has very little reason to change, because the value of a refactor is future reading and change time, and that is concentrated in the code people actually touch.
So my policy is to refactor when we are already in an area for another reason, as a separate behaviour-preserving commit, because then the tests are being run and the reviewer is already looking at those files. I avoid mass rewrites.
A 400-file pull request cannot really be reviewed, so it gets approved on trust, and subtle regressions slip through, like serialised command objects or code that relied on a class name, all with no user value. Tools like OpenRewrite make the changes mechanical and lower the risk, but I would still roll them out in small batches per repository.
Public APIs stay stable: internals first, new forms added alongside the old with deprecation. Then I make it converge: written guidance with examples for new code, review checklists, and tracking of the Java language level per repository so upgrades are planned.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 11, Java 16, Java 17, Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Lambdas, records, sealed types and pattern matching have absorbed several GoF behavioral...": https://scrivio-interview-library.netlify.app/java/behavioral-patterns/268dfac2fe/