Guest User

Untitled

a guest
Jul 9th, 2021
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. public class VisitorDemoExtended {
  2.  
  3.     public static void main(final String[] args) {
  4.         Car car = new Car(List.of(new Body(), new Engine(), new Tire()));
  5.         car.accept(new CarElementPrintVisitorExtended());
  6.         System.out.println("-------------------");
  7.         car.accept(new CarElementPrintVisitor());
  8.     }
  9.  
  10. }
  11.  
  12. interface CarElementExtended extends CarElement {
  13.  
  14.     void accept(CarElementVisitorExtended visitor);
  15.  
  16.     @Override
  17.     default void accept(CarElementVisitor visitor) {
  18.         if (visitor instanceof CarElementVisitorExtended) {
  19.             accept((CarElementVisitorExtended) visitor);
  20.         }
  21.     }
  22. }
  23.  
  24. class Tire implements CarElementExtended {
  25.  
  26.     public void accept(CarElementVisitorExtended visitor) {
  27.         visitor.visit(this);
  28.     }
  29.  
  30. }
  31.  
  32. interface CarElementVisitorExtended extends CarElementVisitor {
  33.  
  34.     @Override
  35.     void visit(Body body);
  36.     @Override
  37.     void visit(Car car);
  38.     @Override
  39.     void visit(Engine engine);
  40.  
  41.     void visit(Tire tire);
  42.  
  43. }
  44.  
  45. class CarElementPrintVisitorExtended implements CarElementVisitorExtended {
  46.  
  47.     @Override
  48.     public void visit(Body body) {
  49.         System.out.println("Visiting body");
  50.     }
  51.  
  52.     @Override
  53.     public void visit(Car car) {
  54.         System.out.println("Visiting car");
  55.     }
  56.  
  57.     @Override
  58.     public void visit(Engine engine) {
  59.         System.out.println("Visiting engine");
  60.     }
  61.  
  62.     @Override
  63.     public void visit(Tire tire) {
  64.         System.out.println("Visiting tire");
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment