Design Architecture/Design Pattern 예제

Visitor 패턴 예제

lipnus 2022. 3. 25. 09:37
반응형

 

 

 

package Visitor.interfaceVisitor;

interface ICarElementVisitor {
    void visit(Wheel wheel);
    void visit(Engine engine);
    void visit(Body body);
    void visit(Car car);
    void visit(Trunk trunk);
}

interface ICarElement {
    abstract public void accept(ICarElementVisitor visitor);
}

public class VisitorDemo {
    public static void main(String[] args) {
        ICarElement car = new Car();
//        car.accept(new CarElementPrintVisitor());
//        car.accept(new CarElementDoVisitor());
        car.accept(new CarElementDistoryVisitor());
    }
}

class Wheel implements ICarElement {
    private String name;
    public Wheel(String name) { this.name = name; } 
    public String getName() { return this.name; }
    public void accept(ICarElementVisitor visitor) {
        visitor.visit(this);
    }
}
class Engine implements ICarElement {
    public void accept(ICarElementVisitor visitor) {
        visitor.visit(this);
    }
}
class Body implements ICarElement {
    public void accept(ICarElementVisitor visitor) {
        visitor.visit(this);
    }
}

class Trunk implements ICarElement {
    public void accept(ICarElementVisitor visitor) {
        visitor.visit(this);
    }
}

class Car implements ICarElement {
    ICarElement[] elements;
    public Car() {
        this.elements = new ICarElement[] {
                new Wheel("front left"),
                new Wheel("front right"),
                new Wheel("back left") ,
                new Wheel("back right"),
                new Body(), new Engine(), new Trunk() };
    }
    public void accept(ICarElementVisitor visitor) {    
        for(ICarElement elem : elements) 
            elem.accept(visitor);
        visitor.visit(this);    
    }
}

class CarElementPrintVisitor implements ICarElementVisitor {	
    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");     
    }
    public void visit(Trunk trunk) {
        System.out.println("Visiting trunk.");
    }
}

class CarElementDoVisitor implements ICarElementVisitor {	
    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 void visit(Trunk trunk) {
        System.out.println("Starting trunk.");
    }
}

class CarElementDistoryVisitor implements ICarElementVisitor {
    public void visit(Wheel wheel) {
        System.out.println("Destroy " + wheel.getName() + " wheel");
    }
    public void visit(Engine engine) {
        System.out.println("Destroy engine");
    }
    public void visit(Body body) {
        System.out.println("Destroy body");
    }
    public void visit(Car car) {
        System.out.println("Destroy car");
    }
    public void visit(Trunk trunk) {
        System.out.println("Destroy trunk");
    }
}
반응형

'Design Architecture > Design Pattern 예제' 카테고리의 다른 글

Commnad패턴 예제  (0) 2022.04.26
Composite 패턴 예제  (0) 2021.09.28
Decorator 예제  (0) 2021.09.28
Template Method 예제  (0) 2021.09.28
빌더(Builder) 패턴 예제  (0) 2021.09.26