Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. //1. Create visitor class
  2. public interface Visitor
  3. {
  4. public void visit(Body b);
  5. }
  6.  
  7. public class DooVisitor implements Visitor
  8. {
  9. //2. Make instance of visitor in new class
  10. public static DooVisitor dv = new DooVisitor();
  11.  
  12. //4. Move Methods leaving delegate behind
  13. //5. Rename methods in visitor class to visit
  14. public void visit(Body b) {
  15. System.out.println("Body does " + b.role);
  16. }
  17. }
  18.  
  19.  
  20.  
  21. public interface CarElement
  22. {
  23. public void accept(DooVisitor v);
  24. }
  25.  
  26. //3. Add visitor-type parameter to OG classes
  27. public class Body implements CarElement
  28. {
  29. public String role = "protect passengers";
  30.  
  31. //4. Move Methods leaving behind delegate method
  32. //5. Rename methods in OG class to accept
  33. public void accept(DooVisitor v) {
  34. v.visit(this);
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement