Browser docs

Acyclic Visitor


Allow new functions to be added to existing class hierarchies without affecting those hierarchies, and without creating the troublesome dependency cycles that are inherent to the GoF Visitor Pattern.


Real world example

We have a hierarchy of modem classes. The modems in this hierarchy need to be visited by an external algorithm based on filtering criteria (is it Unix or DOS compatible modem).

In plain words

Acyclic Visitor allows functions to be added to existing class hierarchies without modifying the hierarchies.

WikiWikiWeb says

The Acyclic Visitor pattern allows new functions to be added to existing class hierarchies without affecting those hierarchies, and without creating the dependency cycles that are inherent to the GangOfFour VisitorPattern.

Programmatic Example

Here’s the Modem hierarchy.

 1public abstract class Modem {
 2  public abstract void accept(ModemVisitor modemVisitor);
 5public class Zoom extends Modem {
 6  ...
 7  @Override
 8  public void accept(ModemVisitor modemVisitor) {
 9    if (modemVisitor instanceof ZoomVisitor) {
10      ((ZoomVisitor) modemVisitor).visit(this);
11    } else {
12"Only ZoomVisitor is allowed to visit Zoom modem");
13    }
14  }
17public class Hayes extends Modem {
18  ...
19  @Override
20  public void accept(ModemVisitor modemVisitor) {
21    if (modemVisitor instanceof HayesVisitor) {
22      ((HayesVisitor) modemVisitor).visit(this);
23    } else {
24"Only HayesVisitor is allowed to visit Hayes modem");
25    }
26  }

Next we introduce the ModemVisitor hierarchy.

 1public interface ModemVisitor {
 4public interface HayesVisitor extends ModemVisitor {
 5  void visit(Hayes hayes);
 8public interface ZoomVisitor extends ModemVisitor {
 9  void visit(Zoom zoom);
12public interface AllModemVisitor extends ZoomVisitor, HayesVisitor {
15public class ConfigureForDosVisitor implements AllModemVisitor {
16  ...
17  @Override
18  public void visit(Hayes hayes) {
19 + " used with Dos configurator.");
20  }
21  @Override
22  public void visit(Zoom zoom) {
23 + " used with Dos configurator.");
24  }
27public class ConfigureForUnixVisitor implements ZoomVisitor {
28  ...
29  @Override
30  public void visit(Zoom zoom) {
31 + " used with Unix configurator.");
32  }

Finally, here are the visitors in action.

1    var conUnix = new ConfigureForUnixVisitor();
2    var conDos = new ConfigureForDosVisitor();
3    var zoom = new Zoom();
4    var hayes = new Hayes();
5    hayes.accept(conDos);
6    zoom.accept(conDos);
7    hayes.accept(conUnix);
8    zoom.accept(conUnix);   

Program output:

    // Hayes modem used with Dos configurator.
    // Zoom modem used with Dos configurator.
    // Only HayesVisitor is allowed to visit Hayes modem
    // Zoom modem used with Unix configurator.

Class diagram

alt text


This pattern can be used:

  • When you need to add a new function to an existing hierarchy without the need to alter or affect that hierarchy.
  • When there are functions that operate upon a hierarchy, but which do not belong in the hierarchy itself. e.g. the ConfigureForDOS / ConfigureForUnix / ConfigureForX issue.
  • When you need to perform very different operations on an object depending upon its type.
  • When the visited class hierarchy will be frequently extended with new derivatives of the Element class.
  • When the recompilation, relinking, retesting or redistribution of the derivatives of Element is very expensive.



The good:

  • No dependency cycles between class hierarchies.
  • No need to recompile all the visitors if a new one is added.
  • Does not cause compilation failure in existing visitors if class hierarchy has a new member.

The bad:

  • Violates Liskov’s Substitution Principle by showing that it can accept all visitors but actually only being interested in particular visitors.
  • Parallel hierarchy of visitors has to be created for all members in visitable class hierarchy.