Skip to content

Latest commit

 

History

History
39 lines (25 loc) · 984 Bytes

设计模式.md

File metadata and controls

39 lines (25 loc) · 984 Bytes

设计模式

「行为设计模式」

1. 访问者模式

访问者模式建议将新行为放入一个名为访问者的独立类中, 而不是试图将其整合到已有类中。

  • 访问者如何接受不同类型的参数?

    声明了一系列以对象结构的具体元素为参数的访问者方法。 如果编程语言支持重载, 这些方法的名称可以是相同的, 但是其参数一定是不同的。

  • 调用时如何使用?

​ 让调用者 accept 一个访问者,主动告诉其应执行的访问者方法

// 客户端代码
foreach (Node node in graph)
    node.accept(exportVisitor)

// 城市
class City is
    method accept(Visitor v) is
        v.doForCity(this)
    // ……

// 工业区
class Industry is
    method accept(Visitor v) is
        v.doForIndustry(this)
    // ……

参考链接

[1] 访问者设计模式 (refactoringguru.cn)