Visitor Controversy

VISITOR is a controversial pattern. Some developers consistently avoid applying it; others defend its use and suggest ways to strengthen it, although these suggestions usually add complexity. The fact is that many design problems tend to accompany the VISITOR pattern.

The fragility of VISITOR shows up in the examples in this chapter. For instance, in the MachineComponent hierarchy, the hierarchy developers decided to differentiate between Machine nodes and MachineComposite nodes but not to differentiate among Machine subclasses. If you need to distinguish among types of machines in your visitor, you will have to resort to using instanceof or another technique to tell which type of machine a visit() method has received. You ...

Get Design Patterns Java™ Workbook 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.