4 ms·
That's actually not what the visitor pattern is about, though the way it's typically presented and the poor name tends to confuse the fact. Visitor isn't about
by munificent 11y ago
That's actually not what the visitor pattern is about, though the way it's typically presented and the poor name tends to confuse the fact. Visitor isn't about traversing trees. It's about letting you define class-specific behavior outside of the definition of the class itself.
It gives you a way to "add a method" to a set of classes without actually stuffing them in the class definitions themselves.
In practice, most visitor patterns are used with AST classes or other types that are stored in a tree-like fashion, but that's coincidence. The visitor pattern itself is really about using virtual dispatch to avoid an ugly and non-type-safe type switch.
- Munksgaard 11y agoSo pattern matching?
- munificent 10y agoSort of. It's a way to emulate a simple subset of pattern matching in languages that lack it.
- emodendroket 11y agoI'm not sure I'm following what you describe. Like an extension method or mix-in?