ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 행위패턴 : Visitor 패턴
    디자인패턴 2013. 3. 27. 09:00
    반응형

    분류 : 행위패턴(Behavioral Patterns)


    정의 :

    객체 구조에서 기능을 분리하는 패턴. 기존 객체의 구조를 수정하지 않고 새로운 기능을 추가할 수 있음.

    용도 :





    소스

    public interface CarElementVisitor {
    	void visit(Wheel wheel);
        void visit(Engine engine);
        void visit(Body body);
        void visit(Car car);
    	
    }
    
    
    public interface CarElement {
    	void accept(CarElementVisitor visitor); // CarElements have to provide accept().
    }
    
    
    class Body implements CarElement {
        public void accept(CarElementVisitor visitor) {
            visitor.visit(this);
        }
    }
    
    
    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) {
            /*
             * accept(CarElementVisitor) in Wheel implements
             * accept(CarElementVisitor) in CarElement, so the call
             * to accept is bound at run time. This can be considered
             * the first dispatch. However, the decision to call
             * visit(Wheel) (as opposed to visit(Engine) etc.) can be
             * made during compile time since 'this' is known at compile
             * time to be a Wheel. Moreover, each implementation of
             * CarElementVisitor implements the visit(Wheel), which is
             * another decision that is made at run time. This can be
             * considered the second dispatch.
             */ 
            visitor.visit(this);
        }
    }
    
    
    class Engine implements CarElement {
        public void accept(CarElementVisitor visitor) {
            visitor.visit(this);
        }
    }
    
    
    class Car implements CarElement {
        CarElement[] elements;
     
        public Car() {
            //create new Array of elements
            this.elements = new CarElement[] { 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());
        }
    }
    
    



    참고자료

    http://en.wikipedia.org/wiki/Visitor_pattern

    반응형

    '디자인패턴' 카테고리의 다른 글

    Reactor 패턴  (1) 2014.09.03
    행위패턴 : Template Method 패턴  (0) 2013.03.25
    행위패턴 : Strategy 패턴  (0) 2013.03.18
    행위패턴 : State 패턴  (0) 2013.03.11
    행위패턴 : Observer 패턴  (0) 2013.03.04

    댓글

Designed by Tistory.