<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

    主站蜘蛛池模板: 亚洲熟妇AV乱码在线观看| 亚洲一卡一卡二新区无人区| 免费在线观看一区| 天天天欲色欲色WWW免费| 亚洲一区二区三区高清视频| 亚欧人成精品免费观看| 亚洲天天在线日亚洲洲精| 午夜精品射精入后重之免费观看 | 少妇亚洲免费精品| 亚洲一本大道无码av天堂| 亚洲午夜免费视频| 最近免费最新高清中文字幕韩国 | 一个人看的免费视频www在线高清动漫| 狼友av永久网站免费观看| 国产精品亚洲A∨天堂不卡| av永久免费网站在线观看| 天天看免费高清影视| 久久亚洲精品高潮综合色a片| 国产v片免费播放| 亚洲伊人久久大香线焦| 免费看黄的成人APP| 亚洲网站在线观看| 日韩av无码成人无码免费| 亚洲AV日韩AV高潮无码专区| 精品女同一区二区三区免费站| 久久亚洲最大成人网4438| 久久一本岛在免费线观看2020| 久久久久亚洲AV无码专区体验| 国产va免费精品观看精品| 91精品国产亚洲爽啪在线观看| 4399影视免费观看高清直播| 亚洲综合小说另类图片动图| 日韩精品电影一区亚洲| 中文字幕免费不卡二区| 亚洲av无码不卡久久| 亚洲av午夜成人片精品电影 | 最新中文字幕免费视频| 亚洲综合久久一本伊伊区| 国产hs免费高清在线观看| 久久久久久成人毛片免费看| 亚洲精品无码日韩国产不卡av|