<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
    主站蜘蛛池模板: 在线观看永久免费视频网站| 野花高清在线观看免费完整版中文| 亚洲AV无码日韩AV无码导航| 国产免费小视频在线观看| 亚洲人成免费电影| 午夜无码A级毛片免费视频| 一级毛片完整版免费播放一区| 亚洲人成欧美中文字幕| 亚洲精品国产成人中文| 亚洲成AV人片在WWW色猫咪| 亚洲国产成人久久精品99 | 最近的免费中文字幕视频| **毛片免费观看久久精品| 日韩免费在线视频| 日本免费人成网ww555在线| 久久久久久av无码免费看大片| 男男gay做爽爽的视频免费| 美女18毛片免费视频| 中文字幕永久免费视频| 国产一区二区免费| 嫩草在线视频www免费观看| 久久99青青精品免费观看| 182tv免费视频在线观看| 久久aⅴ免费观看| 好大好硬好爽免费视频| 国产日韩成人亚洲丁香婷婷| 亚洲AV日韩精品久久久久| 亚洲国产精品一区二区久| 亚洲国产精品成人午夜在线观看 | 免费无码又爽又刺激高潮| 国产黄色片在线免费观看| 国产亚洲精品无码拍拍拍色欲| 亚洲五月六月丁香激情| 亚洲国产精品美女久久久久| 久久精品无码免费不卡| 最近免费中文字幕大全视频| 中文字幕人成人乱码亚洲电影| 亚洲日本国产乱码va在线观看| 另类专区另类专区亚洲| 久久精品免费全国观看国产| 亚洲人成无码www久久久|