Wednesday 15 June 2011

java - How does a visitor "define a new operation" in a way that an iterator does not? -


I know and use both heaters and visitors frequently, and I also use four gang designs of patterns I have been using both before being heard about it. Although syntax is very different for two patterns, I use both for the same conceptual goal: crossing a group of objects. On broadly speaking, I use the Iterator when I have the same type of unstructured items, and I use the Visitor when I have different types of structured objects. The visitor for me is just an elegant, fancy, and Is a strong type eritorator.

When I read the design pattern, I saw the details of the visitor and in particular how it differs from the iterator details.

Iterator: Provide a way to access the elements of a total object sequentially.

Iterator: Without highlighting its underlying representation.

I have thought about it and I really can not understand how visitors defines a new operation .

If I want to implement an unusual operation for example, like toLocalizedString () as local code toString () . When you pass an element to a visitor, it will cross the entire base of that object. Apart from this you can not return anything to accept / travel methods. Each of these attributes can be sent to me by toLocalizedString () .

prevented from using a visitor to define it and it brings my question: How does a visitor "define a new operation" is not a method that does the itater? If I have to trust the gang of four details, then I think I am losing on the true strength of the visitor pattern.

The traversal of structures is not a definition of visitor pattern. In fact, a person with his ideas Can think in the form of a fancy itater.

Does it differentiate visitors from visitors that visitors send you a message to be called so that the method visited depends on the runtime type of the object and the visitor object the visited object visitor Outside, but the operator's executable code is inside the visitor. In other words, following the Visitor Pattern, you are the object of the object that is visited by the object, which can be considered as a new operation on the object.

No comments:

Post a Comment