设计模式 - 访问者模式

定义

Represent an operation to be performed on the elements of an object structure.
Visitor lets you define a new operation without changing the classes of the elements on which it operates.

UML

实例

系统优化,

  1. 需要对一个复杂对象结构(比如对象树)中的所有元素执行某些操作。
  2. 清理辅助行为的业务逻辑。(所有非主要的行为可以抽取到一组访问者)

比较

比较 访问者模式 备注
命令模式 访问者是命令的加强版本,其对象可对不同类的多种对象执行操作
组合模式 访问者对整个组合树执行操作
迭代器模式 同时使用来遍历复杂数据结构
posted @ 2022-04-21 23:56  wesson2019  阅读(19)  评论(0编辑  收藏  举报