For AI agents: the complete documentation index is available at https://nhanbe.id.vn/llms.txt, the full documentation bundle is available at https://nhanbe.id.vn/llms-full.txt, and this page is available as Markdown at https://nhanbe.id.vn/design-patterns/behavioral/visitor.md.

Add operations to object structures without changing their classes. You need to run multiple operations on a stable object structure. Adding new

Visitor

Overview

Add operations to object structures without changing their classes.

When to use

  • You need to run multiple operations on a stable object structure.
  • Adding new operations is more common than adding new node types.

Java example

interface Visitor {
    void visit(TextNode node);
    void visit(ImageNode node);
}

interface Node {
    void accept(Visitor v);
}

class TextNode implements Node {
    public void accept(Visitor v) { v.visit(this); }
}

TypeScript example

interface Visitor {
  visitText(node: TextNode): void;
  visitImage(node: ImageNode): void;
}

interface Node {
  accept(v: Visitor): void;
}

class TextNode implements Node {
  accept(v: Visitor): void { v.visitText(this); }
}

Pros and cons

Pros:

  • New operations can be added without modifying nodes.
  • Separates algorithms from object structure.

Cons:

  • Adding new node types requires updating all visitors.
  • Can be verbose.

Common pitfalls

  • Using Visitor for small object graphs.
  • Forgetting to update all visitors after new nodes.