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

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

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

    Chan Chen Coding...

    Seven: Visitor Design Pattern

    In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. It is one way to easily follow the open/closed principle.

    In essence, the visitor allows one to add new virtual functions to a family of classes without modifying the classes themselves; instead, one creates a visitor class that implements all of the appropriate specializations of the virtual function. The visitor takes the instance reference as input, and implements the goal through double dispatch.

    File:VisitorClassDiagram.svg



    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) {
            /*
             * 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 Body 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) {
            Car car = new Car();
            car.accept(new CarElementPrintVisitor());
            car.accept(new CarElementDoVisitor());
        }
    }



    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-11-02 14:16 Chan Chen 閱讀(278) 評論(0)  編輯  收藏 所屬分類: Design Pattern

    主站蜘蛛池模板: 国产AV无码专区亚洲AV男同| 亚洲成AV人片在线观看无| 99久久婷婷国产综合亚洲| 无码人妻一区二区三区免费n鬼沢| 国产成人99久久亚洲综合精品| 日本一区二区三区在线视频观看免费 | 暖暖在线视频免费视频| 亚洲日韩v无码中文字幕| 国产精品偷伦视频免费观看了| 在线亚洲午夜理论AV大片| 免费国产成人午夜在线观看| 亚洲av日韩综合一区在线观看| 久久99精品视免费看| 亚洲国产综合在线| 亚洲男人在线无码视频| 亚洲免费视频一区二区三区| 国产亚洲精品美女久久久 | 成人免费网站久久久| 亚洲午夜福利精品久久| 女人隐私秘视频黄www免费| 亚洲一区二区影院| 最新欧洲大片免费在线| 免费一级毛片在线播放视频免费观看永久| 亚洲第一福利网站在线观看| 成人自慰女黄网站免费大全| 少妇中文字幕乱码亚洲影视| 好男人视频社区精品免费| 人人公开免费超级碰碰碰视频| 情人伊人久久综合亚洲| 免费无码又黄又爽又刺激| 免费一级毛suv好看的国产网站| 亚洲AV乱码一区二区三区林ゆな| 1000部拍拍拍18免费网站| 亚洲av纯肉无码精品动漫| 国产亚洲大尺度无码无码专线 | 亚洲精品视频免费看| 午夜爱爱免费视频| 青青操视频在线免费观看| 亚洲精品中文字幕| 亚洲国产精品无码久久SM| 女人被男人桶得好爽免费视频|