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

    主站蜘蛛池模板: 美女视频免费看一区二区| 中文字幕亚洲精品资源网| 亚洲中文字幕精品久久| 免费黄色网址网站| 久久久久亚洲精品日久生情 | 毛片亚洲AV无码精品国产午夜| 亚欧免费视频一区二区三区| 亚洲精品国产免费| 国产精品成人免费福利| 亚洲国产精品免费在线观看| 99久久99久久精品免费看蜜桃| 91亚洲精品麻豆| 成人免费视频77777| 亚洲色无码专区一区| 国产国产人免费视频成69大陆| 亚洲精品永久在线观看| 免费国产真实迷j在线观看| 国产尤物在线视精品在亚洲| 亚洲狠狠爱综合影院婷婷| 久久精品免费大片国产大片 | 欧洲精品码一区二区三区免费看| 内射无码专区久久亚洲| 国产特黄一级一片免费 | 偷自拍亚洲视频在线观看99| 亚洲精品高清在线| 久久伊人免费视频| 亚洲第一男人天堂| 四虎精品亚洲一区二区三区| 一个人看的www免费视频在线观看| 亚洲国产成人久久精品动漫| 曰批视频免费30分钟成人| 色五月五月丁香亚洲综合网| 九月丁香婷婷亚洲综合色| 黄页网站免费在线观看| 国产成人综合亚洲绿色| 亚洲av无码不卡一区二区三区| 97人妻无码一区二区精品免费| 黄色a三级免费看| 亚洲黄网在线观看| 国产精品酒店视频免费看| 国产羞羞的视频在线观看免费|