Pattern Comparison

Though seemingly dissimilar, there are several ways in which we can compare these three patterns.

Reusability

All three patterns aim to relieve the developer of repeatedly implementing common code. Once Visitors have been set up, they can be used with many different kinds of object structures without change. The success rate for reusability will depend on how much of the actual state the Visitor needs to be aware of. In our examples, the Visitors were dependent only on the type of the elements, and not on their contents, so a high degree of visitor reusability was obtained between the theory example and the course example. The Interpreter pattern was specifically designed to make inputting structures data driven, by means of a Parse method. In the Memento pattern, the Caretaker and Memento classes are data-independent, and only the Originator needs to change for different circumstances.

Working with structures

All the patterns are intended to work with structures. The Memento moves the structure around as a whole, relying on serialization to do the traversing, whereas Visitor and Interpreter require the developer to write the traversing code. These patterns complement each other in that the traversing code is part of the Visitor pattern, whereas getting the data into an object structure in the first place is part of the Interpreter pattern.

Objects as arguments

The Visitor and Memento pattern pass objects around as part of their infrastructure. In the case of the Visitor, ...

Get C# 3.0 Design Patterns 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.