CarTest.java (4148B)
1 // Copyright (c) 2009, 2015 Tomas Hlavaty All rights reserved. 2 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions 5 // are met: 6 7 // Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following 9 // disclaimer. Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following 11 // disclaimer in the documentation and/or other materials provided 12 // with the distribution. 13 14 // Neither the name of jmultimethod nor the names of its contributors 15 // may be used to endorse or promote products derived from this 16 // software without specific prior written permission. 17 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 // OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 // http://en.wikipedia.org/wiki/Visitor_pattern 32 33 package jmultimethod; 34 35 public class CarTest { 36 37 interface CarElement {} 38 39 class Wheel implements CarElement { 40 private String name; 41 Wheel(String name) { 42 this.name = name; 43 } 44 String getName() { 45 return this.name; 46 } 47 } 48 49 class Engine implements CarElement {} 50 51 class Body implements CarElement {} 52 53 class Car { 54 CarElement[] elements; 55 public CarElement [] getElements() { 56 return elements.clone(); 57 } 58 public Car() { 59 this.elements = new CarElement[] 60 { new Wheel("front left"), new Wheel("front right"), 61 new Wheel("back left") , new Wheel("back right"), 62 new Body(), new Engine()}; 63 } 64 } 65 66 @Multi("visit") 67 public void visitP(Wheel wheel, @V("print") String mode) { 68 System.out.println("Visiting "+ wheel.getName() + " wheel"); 69 } 70 71 @Multi("visit") 72 public void visitP(Engine engine, @V("print") String mode) { 73 System.out.println("Visiting engine"); 74 } 75 76 @Multi("visit") 77 public void visitP(Body body, @V("print") String mode) { 78 System.out.println("Visiting body"); 79 } 80 81 @Multi("visit") 82 public void visitP(Car car, @V("print") String mode) { 83 System.out.println("\nVisiting car"); 84 for(CarElement element : car.getElements()) { 85 visit(element, mode); 86 } 87 System.out.println("Visited car"); 88 } 89 90 @Multi("visit") 91 public void visitD(Wheel wheel, @V("do") String mode) { 92 System.out.println("Kicking my "+ wheel.getName()); 93 } 94 95 @Multi("visit") 96 public void visitD(Engine engine, @V("do") String mode) { 97 System.out.println("Starting my engine"); 98 } 99 100 @Multi("visit") 101 public void visitD(Body body, @V("do") String mode) { 102 System.out.println("Moving my body"); 103 } 104 105 @Multi("visit") 106 public void visitD(Car car, @V("do") String mode) { 107 System.out.println("\nStarting my car"); 108 for(CarElement element : car.getElements()) { 109 visit(element, mode); 110 } 111 System.out.println("Started car"); 112 } 113 114 protected Multimethod mm = new Multimethod("visit", getClass()); 115 116 public void visit(Object any, String mode) { 117 mm.invoke(this, any, mode); 118 } 119 120 public void run() { 121 Car car = new Car(); 122 visit(car, "print"); 123 visit(car, "do"); 124 } 125 126 static public void main(String[] args){ 127 CarTest t = new CarTest(); 128 t.run(); 129 } 130 }