next up previous
Next: First Attempt: Instanceof and Up: No Title Previous: No Title

Why Visitors?

The Visitor pattern is one among many design patterns aimed at making object-oriented systems more flexible [1]. The issue addressed by the Visitor pattern is the manipulation of composite objects. Without visitors, such manipulation runs into several problems as illustrated by the following example. Consider the following implementation of integer lists, written in Java.

interface List {}

class Nil implements List {}

class Cons implements List {
  int head;
  List tail;

Let us now write a program which computes the sum of all components of a given List-object.


Jens Palsberg