Java Magazine, Sept/Oct 2018
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2018 67 design patterns Code from other articles in this series on design patterns can be found further up the trunk of that repository The Node objects must know how to accept the Visitor and they will usually call a method on the Visitor that is appropriate to the type of the node for example class TextNode implements Node void accept Visitor v v visitTextNode this other state and methods Therefore one consequence of this pattern is that the Visitor needs to know about all the node types it might encounter Double Dispatch Many explanations of the Visitor pattern refer to it as double dispatch This term sometimes makes readers think of a two step dispatching process as with a pointer to another pointer used in some languages Thats not what is meant The term refers to the fact that both the type of the visitor and the type of the node or receiver are used in sorting out which method winds up doing the work You can see this in the accept method above theres the call to accept and the call back to visitTextNode Visiting the Text Suppose I need to maintain a word processor that was written in Java There are a few data types text node image node and so on Common operations such as editing text setting fonts and setting colors are taken care of But there are many supplemental operations that need to be performed on the text and new ones come along often as customers provide feedback Heres what the text nodes class started as
You must have JavaScript enabled to view digital editions.