现在的位置: 首页 > 综合 > 正文

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

2013年10月26日 ⁄ 综合 ⁄ 共 2824字 ⁄ 字号 评论关闭

Intent

Visitor lets you define a new operation without changing the
classes of the elements on which it operates.
It would be
better 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)聚集一个功能的相关操作

抱歉!评论已关闭.