<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

    主站蜘蛛池模板: 国产亚洲色视频在线| 国产午夜无码片免费| 69精品免费视频| 亚洲激情在线观看| 97视频免费观看2区| 亚洲高清美女一区二区三区| A毛片毛片看免费| 亚洲av女电影网| 日本人的色道免费网站| 亚洲二区在线视频| 精品国产精品久久一区免费式| 亚洲成av人片在www鸭子| 在线视频免费国产成人| 黄色网址大全免费| 中文字幕专区在线亚洲| 久久99热精品免费观看动漫| 久久亚洲国产精品成人AV秋霞| 美女被免费喷白浆视频| 亚洲国产成人AV在线播放 | 精品久久久久久亚洲精品| 一个人免费观看在线视频www| 亚洲人成网站免费播放| 一本色道久久88亚洲综合| 国产在线精品一区免费香蕉| 亚洲嫩模在线观看| 四虎成人免费网站在线| xxxxx做受大片在线观看免费| 亚洲av日韩av不卡在线观看| 中文字幕无码视频手机免费看| 免费精品国产自产拍在线观看 | 9i9精品国产免费久久| 久久久婷婷五月亚洲97号色| 中文字幕人成无码免费视频| 免费人成视频在线观看免费| 亚洲av无码成h人动漫无遮挡| 4虎永免费最新永久免费地址| 美女视频黄频a免费大全视频| 亚洲一区二区中文| 又粗又大又长又爽免费视频| 嫩草在线视频www免费观看 | 日韩激情无码免费毛片|