The Visitor Pattern is one of the most mis-understood of the classic design patterns. While it has a reputation as a slightly roundabout technique for doing simple processing on simple trees, it is actually an advanced tool for a specific use case: flexible, streaming, zero-overhead processing of complex data structures. This blog post will dive into what makes the Visitor Pattern special, and why it has a unique place in your toolkit regardless of what language or environment you are programming in.
The Visitor Pattern gives you flexible, streaming, zero-overhead processing of complex data structures. While composable tree-transforming functions give you the flexibility but without the efficiency, and manually-fusing operations in one big function gives you the efficiency without the flexibility. The Visitor Pattern gives you the best of both worlds, while allowing your computations to happen in a streaming fashion where there isn't a concrete data-structure at all!
Libraries like the ASM Bytecode Engineering Library and the uPickle JSON serialization library make heavy use of the visitor pattern to implement their complex, performant bytecode and JSON transformations. While this blog post uses JSON-lite processing as an example, the same principles apply to any sort of processing of complex data structures