Java Design Pattern: Visitor

Visitor pattern is a design pattern commonly used in the parser of a compiler, such as Eclipse JDT AST Parser.

Basically, there are two interfaces - Visitor and Element - in Visitor pattern.

Visitor pattern story

Suppose a first time visitor comes to New York City. He want to visit the city and the city accepts his visit. Once the visitor starts visit, it automatically visit everything, and he doesn't need to call a method when he wants to go to a museum. The travel is a package!

Visitor pattern class diagram


Steps of Visitor pattern

This diagram shows the steps of visiting.

The working process is like the following:

1. A visitor FirstTimeVisitor and an element City are created.
2. Program starts with "City accept a visitor".
3. The accept method in City defines that let this visitor to visit.
4. Accepted visitor call it's overloaded method "visit" to visit this City.

Visitor pattern Java code

import java.util.ArrayList;
interface Visitor {
	public void visit(City city);
	public void visit(Museum museum);
	public void visit(Park park);
class FirstTimeVisitor implements Visitor {
	public void visit(City city) {
		System.out.println("I'm visiting the city!");
	public void visit(Museum museum) {
		System.out.println("I'm visiting the Museum!");
	public void visit(Park park) {
		System.out.println("I'm visiting the Park!");
interface Element {
	public void accept(Visitor visitor);
class City implements Element {
	ArrayList<Element> places = new ArrayList<Element>();
	public City() {
		places.add(new Museum());
		places.add(new Park());
	public void accept(Visitor visitor) {
		System.out.println("City is accepting visitor.");
		for (Element e : places) {
class Museum implements Element {
	public void accept(Visitor visitor) {
		System.out.println("Museum is accepting visitor.");
class Park implements Element {
	public void accept(Visitor visitor) {
		System.out.println("Park is accepting visitor.");
public class TestVisitor {
	public static void main(String[] args) {
		FirstTimeVisitor visitor = new FirstTimeVisitor();
		City city = new City();


City is accepting visitor.
I'm visiting the city!
Museum is accepting visitor.
I'm visiting the Museum!
Park is accepting visitor.
I'm visiting the Park!

Visitor pattern in JDK

javax.lang.model.element.AnnotationValue obviously use Visitor pattern, but it is not commonly used in regular projects.

Category >> Design Patterns Stories  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Hi there,
    Very useful post about visitor design pattern.
    Thanks for sharing this.
    Keep up the good work!

  • Classworks

    ~*~ Software Design and Architecture ~*~

    Visitor Design Pattern



  • Johnny Park

    Very useful, thank you very much.
    I’m trying to integrate this pattern with Java cup to build an Abstract Syntax Tree, But it has been very difficult for me.

  • amit

    Nice simple example…….thanks…!!!