访问者(Visitor)
Intent
为一个对象结构(比如组合结构)增加新能力。
Class Diagram
- Visitor:访问者,为每一个 ConcreteElement 声明一个 visit 操作
- ConcreteVisitor:具体访问者,存储遍历过程中的累计结果
- ObjectStructure:对象结构,可以是组合结构,或者是一个集合。
Implementation
1 | public interface Element { |
1 | class CustomerGroup { |
1 | public class Customer implements Element { |
1 | public class Order implements Element { |
1 | public class Item implements Element { |
1 | public interface Visitor { |
1 | public class GeneralReport implements Visitor { |
1 | public class Client { |
1 | customer1 |
JDK
- javax.lang.model.element.Element and javax.lang.model.element.ElementVisitor
- javax.lang.model.type.TypeMirror and javax.lang.model.type.TypeVisitor