Pattern 10Replacing Visitor

Intent

To encapsulate an action to be performed on a data structure in a way that allows the addition of new operations to the data structure without having to modify it.

Overview

A common sticking point in large, long-lived programs is how to extend a data type. We want to extend along two dimensions. First, we may want to add new operations to existing implementations of the data type. Second, we may want to add new implementations of the data type.

We’d like to be able to do this without recompiling the original source, indeed, possibly without even having access to it. This is a problem that’s as old as programming itself, and it’s now known as the expression problem.

For example, consider Java’s Collection ...

Get Functional Programming Patterns in Scala and Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.