Visitor pattern for document traversal
The tree-structured DOM created by us needs to be traversed to produce the content in an output format like HTML, PDF, or SVG.
Note
The composite tree created by us can be traversed using the GoF visitor pattern. Wherever composite pattern is used for composing an hierarchy of objects, the visitor pattern is a natural choice for the traversal of the tree.
In a visitor pattern implementation, every node in the composite tree will support a method called accept
, which takes a visitor concrete class as a parameter. The job of the accept
routine is to reflect the call to the appropriate visit method in the visitor concrete class. We declare an interface named IDocumentVisitor
with methods for visiting each of the elements in our hierarchy as follows:
public interface IDocumentVisitor { void visit(TDocument doc); void visit(TDocumentTable table); void visit(TDocumentTableRow row); void visit(TDocumentTableCell...