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());
        }
    }
    

      

  • 相关阅读:
    [Maven实战-许晓斌]-[第二章]-2.2基于UNIX系统安装maven
    [Maven实战-许晓斌]-[第二章]-2.1在Windows上安装maven
    【sonar-block】Use try-with-resources or close this "BufferedInputStream" in a "finally" clause.
    sonar阻断级别错误(block)简单汇总
    让子类使用父类的Logger
    集合的addAll方法--list.addAll(null)会报错--java.lang.NullPointerException
    nice
    ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded
    Ubuntu 系统修改root密码后,无需密码亦可登录
    MySQL 查找今年的数据
  • 原文地址:https://www.cnblogs.com/wucg/p/2215373.html
Copyright © 2011-2022 走看看