Revision Notes/Design Patterns

Field Entry  -  Design Patterns

 Coreadvanced

Visitor Pattern

Lets you add new operations to a class hierarchy without modifying any of the existing classes — by moving the operation logic into a separate "visitor" object.

#behavioral#visitor#double dispatch#open closed#gof

Field Note - Real-world Analogy

Think of it like this

Like a tax assessor visiting different property types. The assessor (visitor) visits a house, an apartment, and a commercial building (elements). The assessment logic is completely different for each type. The properties don't know how they're assessed — they just let the assessor in (accept). To add a "property insurance calculator", you create a new assessor type without touching the house or apartment classes at all.

Field Observations - Key Topics Covered

01

The core problem Visitor solves: you have a stable class hierarchy (like Shape → Circle, Square, Triangle) and you want to perform many different operations on it (calculate area, export to SVG, export to PDF, generate a bounding box, etc.). Without Visitor, every time you add a new operation, you have to open and modify every class in the hierarchy — violating the Open-Closed Principle.

4 more observations inside

Sign in to read every key point in full

Unlock Full Note

Ready to go deeper?

Open the complete revision note.

Sign in to read every field observation in full, including a runnable code example and side-by-side comparisons with related patterns.

Read Full Notes