目的

为一个对象结构(比如组合结构)增加新能力。

类图

  • Visitor: 访问者,为每一个 ConcreteElement 声明一个 visit 操作。
  • ConcreteVisitor: 具体访问者,存储遍历过程中的累计结果。
  • ObjectStructure: 对象结构,可以是组合结构,或者是一个集合。

访问者类图.png

实现

  1. public interface Element {
  2. void accept(Visitor visitor);
  3. }
  4. class CustomerGroup {
  5. private List<Customer> customers = new ArrayList<>();
  6. void accept(Visitor visitor) {
  7. for (Customer customer : customers) {
  8. customer.accept(visitor);
  9. }
  10. }
  11. void addCustomer(Customer customer) {
  12. customers.add(customer);
  13. }
  14. }
  15. public class Customer implements Element {
  16. private String name;
  17. private List<Order> orders = new ArrayList<>();
  18. Customer(String name) {
  19. this.name = name;
  20. }
  21. String getName() {
  22. return name;
  23. }
  24. void addOrder(Order order) {
  25. orders.add(order);
  26. }
  27. public void accept(Visitor visitor) {
  28. visitor.visit(this);
  29. for (Order order : orders) {
  30. order.accept(visitor);
  31. }
  32. }
  33. }
  34. public class Order implements Element {
  35. private String name;
  36. private List<Item> items = new ArrayList();
  37. Order(String name) {
  38. this.name = name;
  39. }
  40. Order(String name, String itemName) {
  41. this.name = name;
  42. this.addItem(new Item(itemName));
  43. }
  44. String getName() {
  45. return name;
  46. }
  47. void addItem(Item item) {
  48. items.add(item);
  49. }
  50. public void accept(Visitor visitor) {
  51. visitor.visit(this);
  52. for (Item item : items) {
  53. item.accept(visitor);
  54. }
  55. }
  56. }
  57. public class Item implements Element {
  58. private String name;
  59. Item(String name) {
  60. this.name = name;
  61. }
  62. String getName() {
  63. return name;
  64. }
  65. public void accept(Visitor visitor) {
  66. visitor.visit(this);
  67. }
  68. }
  69. public interface Visitor {
  70. void visit(Customer customer);
  71. void visit(Order order);
  72. void visit(Item item);
  73. }
  74. public class GeneralReport implements Visitor {
  75. private int customersNo;
  76. private int ordersNo;
  77. private int itemsNo;
  78. public void visit(Customer customer) {
  79. System.out.println(customer.getName());
  80. customersNo++;
  81. }
  82. public void visit(Order order) {
  83. System.out.println(order.getName());
  84. ordersNo++;
  85. }
  86. public void visit(Item item) {
  87. System.out.println(item.getName());
  88. itemsNo++;
  89. }
  90. public void displayResults() {
  91. System.out.println("Number of customers: " + customersNo);
  92. System.out.println("Number of orders: " + ordersNo);
  93. System.out.println("Number of items: " + itemsNo);
  94. }
  95. }
  96. public class Client {
  97. public static void main(String[] args) {
  98. Customer customer1 = new Customer("customer1");
  99. customer1.addOrder(new Order("order1", "item1"));
  100. customer1.addOrder(new Order("order2", "item1"));
  101. customer1.addOrder(new Order("order3", "item1"));
  102. Order order = new Order("order_a");
  103. order.addItem(new Item("item_a1"));
  104. order.addItem(new Item("item_a2"));
  105. order.addItem(new Item("item_a3"));
  106. Customer customer2 = new Customer("customer2");
  107. customer2.addOrder(order);
  108. CustomerGroup customers = new CustomerGroup();
  109. customers.addCustomer(customer1);
  110. customers.addCustomer(customer2);
  111. GeneralReport visitor = new GeneralReport();
  112. customers.accept(visitor);
  113. visitor.displayResults();
  114. }
  115. }
  116. 运行结果
  117. customer1
  118. order1
  119. item1
  120. order2
  121. item1
  122. order3
  123. item1
  124. customer2
  125. order_a
  126. item_a1
  127. item_a2
  128. item_a3
  129. Number of customers: 2
  130. Number of orders: 4
  131. Number of items: 6

JDK中的体现

  • javax.lang.model.element.Element and javax.lang.model.element.ElementVisitor
  • javax.lang.model.type.TypeMirror and javax.lang.model.type.TypeVisitor