<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    人在江湖

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

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

    class=thumbimage

    Example
    The following example is in the Java programming language, and shows how the contents of a tree of nodes (in this case describing the components of a car) can be printed. Instead of creating "print" methods for each subclass (Wheel, Engine, Body, and Car), a single class (CarElementPrintVisitor) performs the required printing action. Because different subclasses require slightly different actions to print properly, CarElementDoVisitor dispatches actions based on the class of the argument passed to it.

    alt=A Diagram of the Java Code Example.        I, the copyright holder of this work, hereby release it into the public domain.  This applies worldwide. In case this is not legally possible, I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.

    interface CarElementVisitor {
        void visit(Wheel wheel);
        void visit(Engine engine);
        void visit(Body body);
        void visit(Car car);
    }
    interface CarElement {
        void accept(CarElementVisitor visitor); // CarElements have to provide accept().
    }
    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 CarElement[] getElements() {
            return elements.clone(); // Return a copy of the array of references.
        }
        public Car() {
            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 element : this.getElements()) {
                element.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){
            Car car = new Car();
            car.accept(new CarElementPrintVisitor());
            car.accept(new CarElementDoVisitor());
        }

    posted on 2011-02-13 14:19 人在江湖 閱讀(882) 評論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 亚洲色偷偷色噜噜狠狠99网| 亚洲熟妇无码AV在线播放| 亚洲综合久久成人69| 日韩免费人妻AV无码专区蜜桃| 久久久久亚洲AV无码专区网站| 成年免费a级毛片| 国产hs免费高清在线观看| 美女黄频免费网站| 亚洲成AV人网址| 亚洲免费在线观看| 亚洲国产精品一区第二页 | 一区二区三区在线观看免费| 亚洲av成人一区二区三区在线观看| 羞羞的视频在线免费观看| 亚洲国产日韩成人综合天堂| 一级毛片视频免费观看| 亚洲男人的天堂www| 免费无码VA一区二区三区| 亚洲人成网站日本片| 日韩免费毛片视频| 在线看片免费人成视久网| 亚洲欧洲精品久久| 美女视频黄的全免费视频 | 国产产在线精品亚洲AAVV| 精品亚洲视频在线观看| 国产精品免费观看调教网| 亚洲乱码卡三乱码新区| 国产在线19禁免费观看国产| 久久久久久久国产免费看| 精品亚洲成a人片在线观看| 搡女人免费视频大全| 日本中文字幕免费看| 日木av无码专区亚洲av毛片| 成年人免费视频观看| 不卡视频免费在线观看| 亚洲日本在线免费观看| 亚洲成a人片在线观看日本麻豆| 久久免费观看国产精品| 亚洲AⅤ男人的天堂在线观看| 国产亚洲av片在线观看18女人 | 伊人久久大香线蕉免费视频|