WorkspaceLow-Level Design
← Course overview

Course navigation

On this page

Low-Level Design / Behavioral Patterns

pattern · 12 MIN READ

Visitor

Visitor moves an operation across a stable set of element types into a separate object. Each element directs the visitor to its type-specific operation.

Reading completion is independent of quizzes and challenges.

What you’ll understand

  • Visitor moves an operation across a stable set of element types into a separate object. Each element directs the visitor to its type-specific operation.
  • Elements expose accept(visitor), which dispatches to visitText or visitImage on the operation object.
  • Evaluate the tradeoff: Adding operations becomes easier, but adding a new element type requires changing visitors. Use Visitor when element kinds are relatively stable and operations vary.

Useful first: Classes and Objects

The idea and when to recognize it

Visitor moves an operation across a stable set of element types into a separate object. Each element directs the visitor to its type-specific operation.

  • Requirement: A document contains text and image elements, and an export operation should treat them differently.
  • Change to anticipate: Add an export operation without embedding its formatting rules into every document class.
  • Design goal: Elements expose accept(visitor), which dispatches to visitText or visitImage on the operation object.

Start with the concrete problem

A document contains text and image elements, and an export operation should treat them differently.

Add an export operation without embedding its formatting rules into every document class.

Direct implementationRevised design
Handling every element as text loses image-specific information.Elements expose accept(visitor), which dispatches to visitText or visitImage on the operation object.
The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design.Adding operations becomes easier, but adding a new element type requires changing visitors. Use Visitor when element kinds are relatively stable and operations vary.

Responsibilities and contracts

Elements expose accept(visitor), which dispatches to visitText or visitImage on the operation object.

  • Text: Dispatches text-specific operations. Public operations: accept(visitor)
  • Image: Dispatches image-specific operations. Public operations: accept(visitor)
  • ExportVisitor: Owns the export operation. Public operations: visitText(value), visitImage(value)

Read the design

Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.

Preparing the design walkthrough…

Follow the interaction

Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.

Preparing the design walkthrough…

Implementation and expected output

Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.

Preparing the design walkthrough…

Try the idea in another setting

A syntax tree can accept separate type-checking, formatting, and evaluation visitors.

  • Name the new client contract before adapting the example.
  • Identify which state belongs to each object and who controls its lifetime.
  • Write one normal case and one boundary case before extending the design.

Tradeoffs and interview takeaways

Adding operations becomes easier, but adding a new element type requires changing visitors. Use Visitor when element kinds are relatively stable and operations vary.

Common mistake: Handling every element as text loses image-specific information.

  • Explain the choice: Elements expose accept(visitor), which dispatches to visitText or visitImage on the operation object.
  • Challenge your choice: Adding operations becomes easier, but adding a new element type requires changing visitors. Use Visitor when element kinds are relatively stable and operations vary.
  • Describe a requirement that would make the simpler implementation sufficient.

Design challenge and knowledge check

Preparing the design walkthrough…

Make it your own

Saved on this device · 0/20,000

Reading completion is independent of quizzes and challenges.