Skip to content

Instantly share code, notes, and snippets.

@serhii-lypko
Last active September 27, 2025 12:19
Show Gist options
  • Select an option

  • Save serhii-lypko/96981adf19312d4e42351a5e12ae24c5 to your computer and use it in GitHub Desktop.

Select an option

Save serhii-lypko/96981adf19312d4e42351a5e12ae24c5 to your computer and use it in GitHub Desktop.

Revisions

  1. serhii-lypko revised this gist Sep 27, 2025. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions visitor_pattern.java
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,7 @@ int getArea() {
    return (int) (Math.PI * radius * radius);
    }

    // set visitor only once
    @Override
    void accept(Visitor visitor) {
    visitor.visitCircle(this);
    @@ -45,6 +46,7 @@ int getArea() {
    return width * height;
    }

    // set visitor only once
    @Override
    void accept(Visitor visitor) {
    visitor.visitRect(this);
  2. serhii-lypko revised this gist Sep 27, 2025. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions visitor_pattern.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    package com.lox;

    /*
    * We want to be able to define new operations without having to add a new method
    * to each class every time.
  3. serhii-lypko revised this gist Sep 27, 2025. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions visitor_pattern.java
    Original file line number Diff line number Diff line change
    @@ -85,8 +85,10 @@ public static void playground() {
    final Figure rect = new Rect(10, 20);

    final RenderingVisitor renderingVisitor = new RenderingVisitor();
    final TransformVisitor transformingVisitor = new TransformVisitor();

    rect.accept(renderingVisitor);
    circle.accept(transformingVisitor);

    printArea(circle);
    }
  4. serhii-lypko created this gist Sep 27, 2025.
    98 changes: 98 additions & 0 deletions visitor_pattern.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    package com.lox;

    /*
    * We want to be able to define new operations without having to add a new method
    * to each class every time.
    *
    * The Visitor pattern lets you execute an operation over a set of objects with
    * different classes by having a visitor object implement several variants of
    * the same operation, which correspond to all target classes.
    */

    abstract class Figure {
    abstract int getArea();

    abstract void accept(Visitor visitor);
    }

    class Circle extends Figure {
    Circle(int radius) {
    this.radius = radius;
    }

    final int radius;

    @Override
    int getArea() {
    return (int) (Math.PI * radius * radius);
    }

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

    class Rect extends Figure {
    Rect(int width, int height) {
    this.width = width;
    this.height = height;
    }

    final int width;
    final int height;

    @Override
    int getArea() {
    return width * height;
    }

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

    interface Visitor {
    void visitCircle(Circle circle);

    void visitRect(Rect rect);
    }

    class RenderingVisitor implements Visitor {
    public void visitCircle(Circle circle) {
    // do rendering stuff for circle
    }

    public void visitRect(Rect rect) {
    // do rendering stuff for rect
    }
    }

    class TransformVisitor implements Visitor {
    public void visitCircle(Circle circle) {
    // do transforming stuff for circle
    }

    public void visitRect(Rect rect) {
    // do transforming stuff for rect
    }
    }

    public class Playground {
    public static void playground() {
    final Figure circle = new Circle(10);
    final Figure rect = new Rect(10, 20);

    final RenderingVisitor renderingVisitor = new RenderingVisitor();

    rect.accept(renderingVisitor);

    printArea(circle);
    }

    static void printArea(Figure figure) {
    final int area = figure.getArea();
    System.out.println(area);
    }
    }