美文网首页
设计模式之访问者模式(Visitor Pattern)

设计模式之访问者模式(Visitor Pattern)

作者: 杨斌_1024 | 来源:发表于2020-05-30 11:59 被阅读0次

概述

The Visitor pattern allows to apply one or more operation to a set of objects at run-time without having the operations tightly coupled with the object structure.

Visitor模式是指通过封装一个对象实现聚合列表中每个元素的操作(访问)。

访问者(Visitor)模式的定义:将作用于某种数据结构中的各元素的操作分离出来封装成独立的类,使其在不改变数据结构的前提下可以添加作用于这些元素的新的操作,为数据结构中的每个元素提供多种访问方式。它将对数据的操作与数据结构进行分离,是行为类模式中最复杂的一种模式。

本文类图

Visitor模式

主要代码

Visitor主要用于访问元素 元素是被访问的对象 聚合类中是是通过这种方式对每个元素的访问

测试结果

测试结果

参考资料

访问者模式参考代码本文GitHub代码地址

相关文章

网友评论

      本文标题:设计模式之访问者模式(Visitor Pattern)

      本文链接:https://www.haomeiwen.com/subject/cfdczhtx.html