Browse Java Design Patterns & Enterprise Application Architecture

Visitor vs. Iterator Pattern

Compare Visitor and Iterator in Java so traversal and operation-separation concerns are not treated as the same design move.

On this page

Visitor and Iterator often appear around object structures, but they solve different problems.

Iterator

Iterator answers:

  • how do I traverse this structure?

Visitor

Visitor answers:

  • how do I add operations over this structure without putting them all inside the element classes?

Java Shortcut

If traversal is the main problem, think Iterator.

If operation separation across a stable structure is the main problem, think Visitor.

The patterns can coexist, but they should not be confused.

Revised on Thursday, April 23, 2026