<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 閱讀(283) 評論(0)  編輯  收藏 所屬分類: Design Pattern

    主站蜘蛛池模板: 国国内清清草原免费视频99| 一进一出60分钟免费视频| 久久精品一区二区免费看| 亚洲国产精品成人久久蜜臀| 亚洲熟妇无码AV不卡在线播放| 18禁免费无码无遮挡不卡网站| 亚洲国产精品专区| 动漫黄网站免费永久在线观看| 亚洲一区二区三区免费观看| 99热在线精品免费全部my| 亚洲精品无码久久久久YW| 国产成人免费a在线视频色戒| 九九精品国产亚洲AV日韩| 免费**毛片在线播放直播| 国产区图片区小说区亚洲区| 亚洲高清成人一区二区三区 | 久久久亚洲欧洲日产国码农村| 成人免费一区二区三区| 久久精品国产亚洲| 亚洲Av综合色区无码专区桃色| 在线成人精品国产区免费| 亚洲AV色香蕉一区二区| 久久免费看黄a级毛片| 亚洲看片无码在线视频| 全黄a免费一级毛片人人爱| 成人免费ā片在线观看| 亚洲欧洲在线播放| 日本一道综合久久aⅴ免费| 一级一看免费完整版毛片| 亚洲人成在线影院| 免费国产污网站在线观看不要卡| 亚洲一区二区三区在线视频| 国产午夜无码精品免费看动漫| 亚洲成a人片在线观看中文!!!| 免费无码又爽又刺激高潮| 未满十八私人高清免费影院| 亚洲好看的理论片电影| 精品国产免费一区二区| 中国好声音第二季免费播放| 67194在线午夜亚洲| 亚洲尤码不卡AV麻豆|