Visitor desing pattern implemented in Java.

This pattern is used to add new operation(s) to existing class or set of classes, without the need of major code re-factorisation.

Main.java

package visitor;

// ? 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Main {
    public static void main(String[] args) {
        Shape circle = new Circle(2, 5, 4);
        Shape triangle = new Triangle(1, 3, 7, 10);
        Shape square = new Square(-1, 2, 8);

        Visitor basicInfoVisitor = new BasicInfoVisitor();
        Visitor areaVisitor = new AreaVisitor();

        System.out.println("BasicInfoVisitor");
        circle.accept(basicInfoVisitor);
        triangle.accept(basicInfoVisitor);
        square.accept(basicInfoVisitor);

        System.out.println();
        System.out.println("AreaVisitor");
        circle.accept(areaVisitor);
        triangle.accept(areaVisitor);
        square.accept(areaVisitor);
    }
}

After running the output should be:

BasicInfoVisitor
Circle at 2.0, 5.0 radius 4.0
Triangle at 1.0, 3.0 base 7.0 height 10.0
Square at -1.0, 2.0 side 8.0

AreaVisitor
Circle area 50.26548245743669
Triangle area 35.0
Square area 64.0

Shape.java

package visitor;

// ? 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public abstract class Shape {
    private double posX;
    private double posY;

    public Shape(double posX, double posY) {
        this.posX = posX;
        this.posY = posY;
    }

    public double getPosX() {
        return posX;
    }

    public double getPosY() {
        return posY;
    }

    public abstract void accept(Visitor visitor);
}

Circle.java

package visitor;

// ? 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Circle extends Shape {
    private double radius;

    public Circle(double posX, double posY, double radius) {
        super(posX, posY);
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visitCircle(this);
    }
}

Triangle.java

package visitor;

// ? 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Triangle extends Shape {
    private double base;
    private double height;

    public Triangle(double posX, double posY, double base, double height) {
        super(posX, posY);
        this.base = base;
        this.height = height;
    }

    public double getBase() {
        return base;
    }

    public double getHeight() {
        return height;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visitTriangle(this);
    }
}

Square.java

package visitor;

// ? 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class Square extends Shape{
    private double side;

    public Square(double posX, double posY, double side) {
        super(posX, posY);
        this.side = side;
    }

    public double getSide() {
        return side;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visitSquare(this);
    }
}

Visitor.java

package visitor;

// ? 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public interface Visitor {
    public void visitCircle(Circle circle);

    public void visitTriangle(Triangle triangle);

    public void visitSquare(Square square);
}

BasicInfoVisitor.java

package visitor;

// ? 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class BasicInfoVisitor implements Visitor {
    @Override
    public void visitCircle(Circle circle) {
        System.out.println("Circle at " + circle.getPosX() + ", " + circle.getPosY() + " radius " + circle.getRadius());
    }

    @Override
    public void visitTriangle(Triangle triangle) {
        System.out.println("Triangle at " + triangle.getPosX() + ", " + triangle.getPosY() + " base " + triangle.getBase() + " height " + triangle.getHeight());
    }

    @Override
    public void visitSquare(Square square) {
        System.out.println("Square at " + square.getPosX() + ", " + square.getPosY() + " side " + square.getSide());
    }
}

AreaVisitor.java

package visitor;

// ? 2019 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net
public class AreaVisitor implements Visitor {
    @Override
    public void visitCircle(Circle circle) {
        System.out.println("Circle area " + circle.getRadius() * circle.getRadius() * Math.PI);
    }

    @Override
    public void visitTriangle(Triangle triangle) {
        System.out.println("Triangle area " + 0.5f * triangle.getHeight() * triangle.getBase());
    }

    @Override
    public void visitSquare(Square square) {
        System.out.println("Square area " + square.getSide() * square.getSide());
    }
}

 



Java Visitor
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close