The VISITOR Pattern as a Reusable, Generic, Type-Safe Component - Oliveira, B; Wang, M; Gibbons, J; ACM (2008)
The visitor design pattern shows how to separate the structure of an object hierarchy from the behaviour of traversals over that hierarchy. the pattern is very flexible; this very flexibility makes it difficult to capture the pattern as anything more formal than prose, pictures and prototypes. we show how to capture the essence of the visitor pattern as a reusable software library, by using advanced type system features appearing in modern object-oriented languages such as scala. we preserve type-safety statically and modularly: no reflection or similar mechanisms are used and modules can be independently compiled. the library is generic, in two senses: not only is it parametrised by both the return type and the shape of the object hierarchy, but also it allows a number of implementation choices (internal versus external control, imperative versus functional behaviour, orthogonal aspects such as tracing and memoisation) to be specified by parameters rather than fixed in early design decisions. finally, we propose a generalised datatype-like notation, on top of our visitor library: this provides a convenient functional decomposition style in object-oriented languages. Copyright © 2008 ACM.