Java设计模式(17) -- 访问者

来源:互联网 发布:复旦大学软件学院学费 编辑:程序博客网 时间:2024/06/08 14:17

Intent

Visitor lets you define a new operation without changing theclasses of the elements on which it operates.
It would bebetter if each new operation could be added separately, and the classes were independent of the operations that apply to them.
Visitor makes adding new operations easy.
A visitor gathers related operations and separates unrelated ones.

 

How to
Visitor
declares a Visit operation for each class of ConcreteElement in the object structure.

ConcreteVisitor
implements each operation declared by Visitor.

Element
defines an Accept operation that takes a visitor as an argument.

ConcreteElement
implements an Accept operation that takes a visitor as an argument.

 

ObjectStructure
can enumerate its elements.

 

UML

Code

Konwn cases
(1)向已有的稳定的类结构添加新操作
(2)聚集一个功能的相关操作

原创粉丝点击