使用Visitor模式和单独的类有什么区别? [英] What's the difference between using the Visitor pattern and a separate class?

查看:120
本文介绍了使用Visitor模式和单独的类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Visitor模式与使用静态方法分开执行代码之间的区别是什么.

I would like to know what is the difference between the Visitor pattern and using a static method to execute code in separation.

让我们看一个我可以称为访客"模式的示例:

Let's take a look at an example where I might call the Visitor pattern:

  new AnalyticsVisitor.accept(myClass); 

,例如,当从myClass调用时,它将把工作移到访问者中执行.如果占用大量内存,它甚至可以更快地进行垃圾收集.

and this when called in from myClass for example, would move the work into a visitor to execute. It would even garbage collect faster if it's memory intensive.

现在让我们来看看使用一种简单的方法来实现大致相同的事情:

Now lets take a look at using a simple method to achieve more or less the same thing:

  new AnalyticsManager.execute(myClass);

我完成了同样的事情吗?

Have I achieved the same thing?

  1. 我有代码分隔符.
  2. 我可以将其应用于多个数据结构
  3. 我可以在不更改原有代码的情况下添加信息.

那么,为什么要使用Visitor模式而不是仅使用类(除非进行双重调度)?

So why use the Visitor pattern instead of just a class (unless for double dispatch)?

推荐答案

这个问题还是有点困惑.我怀疑您还不了解访客"模式的目标.

This question is still a little confused. I suspect you haven't understood the goal of the Visitor pattern.

如此处所述当您具有相对稳定的(就开发而言)稳定的复杂数据结构(例如,解析树)时,访问者模式很有用,但是您希望能够继续对其所有元素添加新的操作.这与标准的OO技术笨拙.

As discussed here the visitor pattern is useful when you have complex data structure (such as a parse tree) that is relatively stable (in terms of development), but you want to be able to keep adding new operations on all of its elements. This is clumsy with standard OO techniques.

访问者模式所基于的技术是两次发送,因此当您说为什么不使用访问者模式,除非进行两次发送?"您实际上是在说为什么要使用访客模式?"

The technology the visitor pattern is based on is double-dispatch, so when you say "Why use the Visitor pattern unless for double-dispatch?" you are effectively saying "Why use the visitor pattern?"

您的示例代码仅包含客户端,因此不清楚您的新技术实际上提供了什么.

Your example code only includes the client, so it isn't clear what your new technique actually offers.

对于实际的访客模式,提供的代码似乎是向后的.应该是:

The supplied code appears to be backwards for a real visitor pattern. It should be:

my_datastructure.accept(analytics_visitor);

其中analytics_visitor继承自MyDataStructureVisitor,并为数据结构可以容纳的每种元素类型提供单独的方法.

where analytics_visitor inherits from MyDataStructureVisitor, and supplies individual methods for each of the element types that the data structure can hold.

至于成就:

  1. 代码分隔"是一个模糊的术语.访问者模式允许在不定义所有操作(假定方法)的情况下定义数据结构.相反,它们可以单独定义-封装质量较差.)

  1. "Code separation" is a vague term. The visitor pattern allows the data structure to be defined without all the the operations (putative methods) to be defined. Instead, they can be defined separately - with a cost of poorer encapsulation.)

目前尚不清楚将访问者模式应用于多个数据结构的含义.每个访问者类别都与一个数据结构相关联.

It isn't clear what it means to apply a visitor pattern to several data structures. Each visitor class is associated with one data structure.

目标不是在遗留代码中添加信息".它将操作添加到旧代码中.

The goal isn't to add 'info' to legacy code. It is to add operations to legacy code.

这篇关于使用Visitor模式和单独的类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆