Timeline for How to implement visitor if not all functions are supported for each Element?
Current License: CC BY-SA 4.0
5 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Dec 16, 2021 at 16:14 | answer | added | Doc Brown | timeline score: 3 | |
| Dec 16, 2021 at 16:10 | comment | added | Filip Milovanović |
Your second approach, although somewhat confusing and complicated, might work, but you still have the problem of traversal - say you pass Visitor1 to the root: in order for this to work, you either have to keep Visitable1 and Visitable2 child nodes separate, so that you can traverse only the children that implement Visitable1, or you have to check the type. But, have you considered implementing visit(ElementB element) as a no-op (similar to null object pattern)? Perhaps that makes sense, and it could make things simpler? 2/2
|
|
| Dec 16, 2021 at 16:09 | comment | added | Filip Milovanović | Typically, you'd apply visitors to a polymorphic element structure (like a tree), where you don't know the concrete type of each element in advance (or, more precisely, the visiting operation doesn't know the type in advance). As you traverse the tree, what do you want to happen when you come across an unsupported element? Your first option will throw an exception that you can't really do anything about; it requires the caller to know that the element structure has no ElementB nodes. 1/2 | |
| S Dec 16, 2021 at 15:17 | review | First questions | |||
| Dec 16, 2021 at 15:37 | |||||
| S Dec 16, 2021 at 15:17 | history | asked | sininen | CC BY-SA 4.0 |