Visitor pattern.
I read about this pattern, and i can understand it. Again i know the "rules and 
conventions" but i'm a novice player.

I won't detail my thought pattern again… it's based on trial and error… I found 
a match, an example that i can relate to from experience.

"Consider the design of a 2D CAD system. At its core there are several types to 
represent basic geometric shapes like circles, lines and arcs. The entities are 
ordered into layers, and at the top of the type hierarchy is the drawing, which 
is simply a list of layers, plus some additional properties.
A fundamental operation on this type hierarchy is saving the drawing to the 
system's native file format. At first glance it may seem acceptable to add 
local save methods to all types in the hierarchy. But then we also want to be 
able to save drawings to other file formats, and adding more and more methods 
for saving into lots of different file formats soon clutters the relatively 
pure geometric data structure we started out with.
A naive way to solve this would be to maintain separate functions for each file 
format. Such a save function would take a drawing as input, traverse it and 
encode into that specific file format. But if you do this for several different 
formats, you soon begin to see lots of duplication between the functions, e.g. 
lots of type-of if statements and traversal loops. Another problem with this 
approach is how easy it is to miss a certain shape in some saver.
Instead, you could apply the Visitor pattern. The Visitor pattern encodes a 
logical operation on the whole hierarchy into a single class containing one 
method per type. In our CAD example, each save function would be implemented as 
a separate Visitor subclass. This would remove all duplication of type checks 
and traversal steps. It would also make the compiler complain if you leave out 
a shape.
Another motivation is to reuse iteration code."

Based on the above text i can extract the basic pattern of interaction and 
apply it to our example.

Essentially "passing a visitor" as you say would involve me creating a class 
that has a method or methods for each type of object i want to "draw a picture" 
for. Per from your text, i would pass this visitor to the AST to produce these 
images from the input that AST gets from some example code or something.

Please be so kind as to point out anything that i might have misunderstood.


Pe 26.01.2013, la 13:41, Stéphane Ducasse [via Smalltalk] 
<[email protected]> a scris:

> > Print all the methods on small pieces of paper and try to understand the 
> > thing as a whole first. Then focus on understanding the patterns and reason 
> > behind their use. 
> > 
> 
> Do you know what is a visitor? 
> If not you should read the pattern because on a AST you can pass a visitor 
> and it can produce many different shapes 





--
View this message in context: 
http://forum.world.st/How-do-i-list-the-execution-order-of-statements-inside-a-method-tp4665303p4665576.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.

Reply via email to