zoukankan      html  css  js  c++  java
  • Visitor Pattern (Visitor设计模式)

    visitor pattern uml

    //java code:
    
    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) {
            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 CarElement[] getElements() {
            return elements.clone(); // Return a copy of the array of references.
        }
     
        public Car() {
            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 element : this.getElements()) {
                element.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());
        }
    }
    

      

  • 相关阅读:
    sqlserver2005存储汉字成问号解决办法:
    .net c# 日期格式和常用处理
    文件夹无法访问拒绝访问,无法删除文件的,快速有效解决方法
    打印出所有的 info.plist 中的 keys、values
    利用时间戳来准确计算某个时间点具现在的时间差
    项目中的技巧经验汇总
    "Base SDK Missing"问题的解决
    UIPopoverController的使用
    给ios自带控件添加圆角边的方法
    实现程序内截屏功能的代码
  • 原文地址:https://www.cnblogs.com/wucg/p/2215373.html
Copyright © 2011-2022 走看看