advancedBridge, Visitor & Chain of Responsibility

What trade-off does the Visitor pattern make, and when does that trade-off actually work in your favor?

Visitor makes adding a new operation trivial: a brand-new class implementing the visitor interface, with zero changes required to any existing element type. But it makes adding a new element type expensive: every existing visitor implementation must add a new overload to handle it, or the code fails to compile. This is the inverse of the trade-off ordinary polymorphism usually makes, where new types are cheap to add and new operations mean touching every existing type. Visitor is the right choice specifically when the set of concrete types is stable and unlikely to grow, while new operations on those types are expected to keep arriving over time -- exactly the situation with a fixed set of document formats that need an ever-growing list of operations performed on them.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to In the Chain of Responsibility pattern, does the object sending a request need to know in advance which handler will actually process it?← Back to all Low-Level Design & Design Patterns questions