访客模式 - 添加新的ConcreteElement类很难? [英] Visitor pattern - adding new ConcreteElement classes is hard?

查看:80
本文介绍了访客模式 - 添加新的ConcreteElement类很难?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一本关于访客模式的书。它提供与 oodesign的网站相同的类图。



它说添加新的ConcreteElement类很难。但是我不明白为什么。据我所知,Concretevisitor定义了一组操作,这些操作必须由concreteElement使用。所以当我添加一个新的元素,它具有与之前定义的相同的操作时,我不需要添加任何东西(只有ConcreteElement本身)。如果我添加了一个新的元素,这与之前在访问者中定义的操作不同,我需要添加一个新的访问者。但是我必须在任何设计模式中做。

解决方案

嗯,你必须扩展你的所有访客。

您有一个调用者,一些需要访问的元素,以及一个执行单个元素处理的元素(访问者)。您的目标是保持元素和调用者的实现,并通过新的访问者扩展功能。



通常你有很多具体的访问者。如果您添加一个新类型的要处理的元素,您将需要更改所有具体的访问者以考虑这一点。



为什么?



好吧,假设来电者是工厂,你有元素汽车和自行车。



油漆你必须有方法

  void process(Car c); //绘制汽车
void process(Bike b); //绘制一辆自行车

同样的操作组装,包,洗等



如果您添加元素Scooter,则所有操作都必须使用新方法扩展

  void process(Scooter s); //处理滑板车

这有点工作。另外你可能会碰到你所添加的元素与其他元素有很大的不同,你可以很容易地将它们与操作相匹配。



维基百科(http://en.wikipedia.org/wiki/Visitor_pattern)说


本质上,访问者可以将一个新的虚拟函数添加到
系列的类中,而无需修改类本身;相反,
一个创建一个访问类,它实现虚拟函数的所有相应的
特殊化。访问者以
实例引用为输入,并通过双
调度实现目标。


这是一个相当抽象的方式说出我在上面所说的话。通常你可以将这些方法添加到元素中,但是如果你不能将方法添加到其他的东西,并将它们传递给处理。这是一个额外的工作,但如果情况值得,可能值得。


I read a book about the visitor pattern. It gives the same class diagram as in the oodesign's website.

It says that adding new ConcreteElement classes is hard. But I didn't understand why. As I understood, the Concretevisitor defines the set of operations, which have to be used by the concreteElement. So when I add a new element, which has the same operation I defined earlier, I don't need to add anything (just the ConcreteElement itself). If I add a new element, which doesn't have the same operations I defined earlier in the visitors, I need to add a new visitor. But this I have to do in any design pattern.

解决方案

Well, you have to extend all your visitors.

You have a caller, some elements that need to be visited, and an element - the visitor - that does the processing of the individual elements. Your goal is to keep the implementation of the elements and the caller fixed, and extend functionality via new visitors.

Usually you have a lot of the concrete visitors. If you add a new type of element to be processed, you will need to change all the concrete visitors to take this into account.

Why?

well, imagine that the caller is "Factory", and you have the elements "Car" and "Bike".

For operation "Paint" you have to have the methods

void process(Car c); // Paint a car
void process(Bike b); // Paint a bike

Likewise for operations "Assemble", "Package", "Wash" etc.

If you add an element "Scooter", all the operations have to be extended with a new method

void process(Scooter s); // Handle a Scooter

This is a bit of work. Also you may hit the isse where the element you add is so different from the others that you canøt easily fit them to the operations.

Wikipedia (http://en.wikipedia.org/wiki/Visitor_pattern) says

In essence, the visitor allows one to add new virtual functions to a family of classes without modifying the classes themselves; instead, one creates a visitor class that implements all of the appropriate specializations of the virtual function. The visitor takes the instance reference as input, and implements the goal through double dispatch.

That's a pretty abstract way of saying what I try to say above. Usually you'd add these methods to the elements, but if you can't you have to add the methods to somethign else, and pass that along to do the processing. This is a bit of extra work, but may be worth it, if the situation merits it.

这篇关于访客模式 - 添加新的ConcreteElement类很难?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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