--- url: "https://refactoring.guru/design-patterns/visitor/java/example" titulek: "Visitor in Java / Design Patterns" h1: "Visitor in Java" popis: "Visitor pattern in Java. Full code example in Java with detailed comments and explanation. Visitor is a behavioral design pattern that allows adding new behaviors to existing class hierarchy without altering any existing code." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/visitor/java/example" drobecky: ["Home", "Design Patterns", "Visitor", "Java"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:23:19" znaku: 8547 slov: 1021 kodovani: "utf-8" sha256: "ba9a93ca5860582453d6e3686318dacf6dad4157df2347bcd2521647f4286ec6" --- # **Visitor** in Java **Visitor** is a behavioral design pattern that allows adding new behaviors to existing class hierarchy without altering any existing code. > Read why Visitors can’t be simply replaced with method overloading in our article [Visitor and Double Dispatch](/design-patterns/visitor-double-dispatch). [Learn more about Visitor](/design-patterns/visitor) **Complexity:** **Popularity:** **Usage examples:** Visitor isn’t a very common pattern because of its complexity and narrow applicability. Here are some examples of pattern in core Java libraries: - [`javax.lang.model.element.AnnotationValue`](http://docs.oracle.com/javase/8/docs/api/javax/lang/model/element/AnnotationValue.html) and [`AnnotationValueVisitor`](http://docs.oracle.com/javase/8/docs/api/javax/lang/model/element/AnnotationValueVisitor.html) - [`javax.lang.model.element.Element`](http://docs.oracle.com/javase/8/docs/api/javax/lang/model/element/Element.html) and [`ElementVisitor`](http://docs.oracle.com/javase/8/docs/api/javax/lang/model/element/ElementVisitor.html) - [`javax.lang.model.type.TypeMirror`](http://docs.oracle.com/javase/8/docs/api/javax/lang/model/type/TypeMirror.html) and [`TypeVisitor`](http://docs.oracle.com/javase/8/docs/api/javax/lang/model/type/TypeVisitor.html) - [`java.nio.file.FileVisitor`](http://docs.oracle.com/javase/8/docs/api/java/nio/file/FileVisitor.html) and [`SimpleFileVisitor`](http://docs.oracle.com/javase/8/docs/api/java/nio/file/SimpleFileVisitor.html) - [`javax.faces.component.visit.VisitContext`](http://docs.oracle.com/javaee/7/api/javax/faces/component/visit/VisitContext.html) and [`VisitCallback`](http://docs.oracle.com/javaee/7/api/javax/faces/component/visit/VisitCallback.html) ## Exporting shapes into XML In this example, we would want to export a set of geometric shapes into XML. The catch is that we don’t want to change the code of shapes directly or at least keep it to the minimum. In the end, the Visitor pattern establishes an infrastructure that allows us to add any behaviors to the shapes hierarchy without changing the existing code of those classes. ### **shapes** #### **shapes/Shape.java:** Common shape interface ``` package refactoring_guru.visitor.example.shapes; import refactoring_guru.visitor.example.visitor.Visitor; public interface Shape { void move(int x, int y); void draw(); String accept(Visitor visitor); } ``` #### **shapes/Dot.java:** A dot ``` package refactoring_guru.visitor.example.shapes; import refactoring_guru.visitor.example.visitor.Visitor; public class Dot implements Shape { private int id; private int x; private int y; public Dot() { } public Dot(int id, int x, int y) { this.id = id; this.x = x; this.y = y; } @Override public void move(int x, int y) { // move shape } @Override public void draw() { // draw shape } @Override public String accept(Visitor visitor) { return visitor.visitDot(this); } public int getX() { return x; } public int getY() { return y; } public int getId() { return id; } } ``` #### **shapes/Circle.java:** A circle ``` package refactoring_guru.visitor.example.shapes; import refactoring_guru.visitor.example.visitor.Visitor; public class Circle extends Dot { private int radius; public Circle(int id, int x, int y, int radius) { super(id, x, y); this.radius = radius; } @Override public String accept(Visitor visitor) { return visitor.visitCircle(this); } public int getRadius() { return radius; } } ``` #### **shapes/Rectangle.java:** A rectangle ``` package refactoring_guru.visitor.example.shapes; import refactoring_guru.visitor.example.visitor.Visitor; public class Rectangle implements Shape { private int id; private int x; private int y; private int width; private int height; public Rectangle(int id, int x, int y, int width, int height) { this.id = id; this.x = x; this.y = y; this.width = width; this.height = height; } @Override public String accept(Visitor visitor) { return visitor.visitRectangle(this); } @Override public void move(int x, int y) { // move shape } @Override public void draw() { // draw shape } public int getId() { return id; } public int getX() { return x; } public int getY() { return y; } public int getWidth() { return width; } public int getHeight() { return height; } } ``` #### **shapes/CompoundShape.java:** A compound shape ``` package refactoring_guru.visitor.example.shapes; import refactoring_guru.visitor.example.visitor.Visitor; import java.util.ArrayList; import java.util.List; public class CompoundShape implements Shape { public int id; public List children = new ArrayList<>(); public CompoundShape(int id) { this.id = id; } @Override public void move(int x, int y) { // move shape } @Override public void draw() { // draw shape } public int getId() { return id; } @Override public String accept(Visitor visitor) { return visitor.visitCompoundGraphic(this); } public void add(Shape shape) { children.add(shape); } } ``` ### **visitor** #### **visitor/Visitor.java:** Common visitor interface ``` package refactoring_guru.visitor.example.visitor; import refactoring_guru.visitor.example.shapes.Circle; import refactoring_guru.visitor.example.shapes.CompoundShape; import refactoring_guru.visitor.example.shapes.Dot; import refactoring_guru.visitor.example.shapes.Rectangle; public interface Visitor { String visitDot(Dot dot); String visitCircle(Circle circle); String visitRectangle(Rectangle rectangle); String visitCompoundGraphic(CompoundShape cg); } ``` #### **visitor/XMLExportVisitor.java:** Concrete visitor, exports all shapes into XML ``` package refactoring_guru.visitor.example.visitor; import refactoring_guru.visitor.example.shapes.*; public class XMLExportVisitor implements Visitor { public String export(Shape... args) { StringBuilder sb = new StringBuilder(); sb.append("" + "\n"); for (Shape shape : args) { sb.append(shape.accept(this)).append("\n"); } return sb.toString(); } public String visitDot(Dot d) { return "" + "\n" + " " + d.getId() + "" + "\n" + " " + d.getX() + "" + "\n" + " " + d.getY() + "" + "\n" + ""; } public String visitCircle(Circle c) { return "" + "\n" + " " + c.getId() + "" + "\n" + " " + c.getX() + "" + "\n" + " " + c.getY() + "" + "\n" + " " + c.getRadius() + "" + "\n" + ""; } public String visitRectangle(Rectangle r) { return "" + "\n" + " " + r.getId() + "" + "\n" + " " + r.getX() + "" + "\n" + " " + r.getY() + "" + "\n" + " " + r.getWidth() + "" + "\n" + " " + r.getHeight() + "" + "\n" + ""; } public String visitCompoundGraphic(CompoundShape cg) { return "" + "\n" + " " + cg.getId() + "" + "\n" + _visitCompoundGraphic(cg) + ""; } private String _visitCompoundGraphic(CompoundShape cg) { StringBuilder sb = new StringBuilder(); for (Shape shape : cg.children) { String obj = shape.accept(this); // Proper indentation for sub-objects. obj = " " + obj.replace("\n", "\n ") + "\n"; sb.append(obj); } return sb.toString(); } } ``` #### **Demo.java:** Client code ``` package refactoring_guru.visitor.example; import refactoring_guru.visitor.example.shapes.*; import refactoring_guru.visitor.example.visitor.XMLExportVisitor; public class Demo { public static void main(String[] args) { Dot dot = new Dot(1, 10, 55); Circle circle = new Circle(2, 23, 15, 10); Rectangle rectangle = new Rectangle(3, 10, 17, 20, 30); CompoundShape compoundShape = new CompoundShape(4); compoundShape.add(dot); compoundShape.add(circle); compoundShape.add(rectangle); CompoundShape c = new CompoundShape(5); c.add(dot); compoundShape.add(c); export(circle, compoundShape); } private static void export(Shape... shapes) { XMLExportVisitor exportVisitor = new XMLExportVisitor(); System.out.println(exportVisitor.export(shapes)); } } ``` #### **OutputDemo.txt:** Execution result ``` 2 23 15 10 4 1 10 55 2 23 15 10 3 10 17 20 30 5 1 10 55 ``` ## **Visitor** in Other Languages