๋ฐฉ๋ฌธ์ž

๋น„์ง€ํ„ฐ ํŒจํ„ด

  • ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋Œ์•„๋‹ค๋‹ˆ๋ฉฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ตฌ์กฐ
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ํ•œ ๊ฐ์ฒด ๊ตฌ์กฐ์—์„œ ๋ถ„๋ฆฌ ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•
  • ๊ตฌ์กฐ์˜ ์ˆ˜์ • ์—†์ด ์ƒˆ๋กœ์šด ๋™์ž‘์„ ๊ธฐ์กด ๊ฐ์ฒด ๊ตฌ์กฐ์— ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
interface CarElementVisitor {
    void visit(Wheel wheel);
    void visit(Engine engine);
    void visit(Body body);
    void visit(Car car);
}
interface CarElement {
  void accept(CarElementVisitor visitor);
}
class Wheel implements CarElement {
  private String name;

  public Wheel(String name) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }

  public void accept(CarElementVisitor visitor) {
    visitor.visit(this);
  }
}

class Engine implements CarElement {
  public void accept(CarElementVisitor visitor) {
    visitor.visit(this);
  }
}

class Body implements CarElement {
  public void accept(CarElementVisitor visitor) {
    visitor.visit(this);
  }
}

class Car implements CarElement {
  CarElement[] elements;
  
  public Car() {
    this.elements = new CarElements[] {
      new Wheel("front left"),
      new Wheel("front right"),
      new Wheel("back left"),
      new Wheel("back right"),
      new Body(),
      new Engine()
    };
  }

  public void accept(CarElementVisitor visitor) {
    for(CarElement elem : elements) {
      elem.accept(visitor);
    }
    visitor.visit(this);
  }
}
class CarElementPrintVisitor implements CarElementVisitor {
  public void visit(Wheel wheel) {
    System.out.println("Visiting " + wheel.getName() + " wheel");
  }

  public void visit(Engine engine) {
      System.out.println("Visiting engine");
  }

  public void visit(Body body) {
    System.out.println("Visiting body");
  }

  public void visit(Car car) {
    System.out.println("Visiting car");
  }
}

class CarElementDoVisitor implements CarElementVisitor {
  public void visit(Wheel wheel) {
    System.out.println("Kicking my " + wheel.getName() + " wheel");
  }

  public void visit(Engine engine) {
      System.out.println("Starting my engine");
  }

  public void visit(Body body) {
      System.out.println("Moving my body");
  }

  public void visit(Car car) {
      System.out.println("Starting my car");
  }
}
public class VisitorDemo {
  static public void main(String[] args) {
    CarElement car = new Car();
    car.accept(new CarElementPrintVisitor());
    car.accept(new CarElementDoVisitor());
  }
}