mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-29 15:13:44 +00:00
Update 设计模式 - 访问者.md
小节标题改为中文,重新绘制类图,补充参考资料。
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 270 KiB |
+11
-10
@@ -1,19 +1,16 @@
|
||||
## 访问者(Visitor)
|
||||
|
||||
### Intent
|
||||
|
||||
### 意图
|
||||
为一个对象结构(比如组合结构)增加新能力。
|
||||
|
||||
### Class Diagram
|
||||
|
||||
### 类图
|
||||
- Visitor:访问者,为每一个 ConcreteElement 声明一个 visit 操作
|
||||
- ConcreteVisitor:具体访问者,存储遍历过程中的累计结果
|
||||
- ObjectStructure:对象结构,可以是组合结构,或者是一个集合。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/79c6f036-bde6-4393-85a3-ef36a0327bd2.png"/> </div><br>
|
||||
|
||||
### Implementation
|
||||
<div align="center"> <img src="pics/dp-visitor.png" width="780px"/> </div><br>
|
||||
|
||||
### 实现
|
||||
```java
|
||||
public interface Element {
|
||||
void accept(Visitor visitor);
|
||||
@@ -182,7 +179,7 @@ public class Client {
|
||||
}
|
||||
```
|
||||
|
||||
```html
|
||||
```text
|
||||
customer1
|
||||
order1
|
||||
item1
|
||||
@@ -200,7 +197,11 @@ Number of orders: 4
|
||||
Number of items: 6
|
||||
```
|
||||
|
||||
### JDK
|
||||
|
||||
### JDK 中的应用
|
||||
- javax.lang.model.element.Element and javax.lang.model.element.ElementVisitor
|
||||
- javax.lang.model.type.TypeMirror and javax.lang.model.type.TypeVisitor
|
||||
|
||||
## 参考资料
|
||||
|
||||
- 弗里曼. Head First 设计模式 [M]. 中国电力出版社, 2007.
|
||||
- Gamma E. 设计模式: 可复用面向对象软件的基础 [M]. 机械工业出版社, 2007.
|
||||
|
||||
Reference in New Issue
Block a user