Java Magazine, Sept/Oct 2018
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2018 69 design patterns pete with the well known word processor thats out there Therefore Node is now an interface with just one method public interface Node abstract void accept Visitor v I was tempted to call this interface Visitable instead of Node On one hand Visitable is a more descriptive name for this version On the other hand most formal definitions of Visitor use the term Node I know some of you will go to Wikipedia to get a second opinion after reading this and I dont want to confuse anyone Node could alternatively be an abstract class but that would force all the implementation classes to be related by inheritance which may be an unnecessary restriction Node uses Visitor as a type so the next step is to define Visitor public abstract class Visitor public abstract void visitTextNode TextNode textNode public abstract void visitImageNode ImageNode imageNode And so on for TableNode SectionNode VideoNode and so forth Note that you could make all the methods be overloads of a single method called visit because the argument types are unique but I think this way is clearer Its a stylistic choice so pick one way and try to be consistent At any rate here you meet the one complication of the Visitor pattern Visitors need to know how to visit every main kind of node The revised node classes themselves are not that interesting so I didnt show their code the Text node has a Text property the Image node has a FileName a width a height and an
You must have JavaScript enabled to view digital editions.