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 ...

Get .NET Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.