Composite and visitor pattern - A quick primer
Whenever we deal with hierarchies like W3C DOM, document formats, graphics database or abstract syntax tree (AST) of a compiler, we create part/whole relationship. The GoF composite pattern is natural choice for creating hierarchies. Building hierarchies and its processing can be separated using GoF visitor pattern. The visitor pattern visits each node in the hierarchy and perform some action on the node. In a compiler, the tree representation of a program will be processed for type checking, code generation, trans compilation (source to source transformation) and so on. Sometimes people would like to perform additional activities on the tree. It is not feasible to add methods for processing the nodes within a node. Whenever a programmer wants a new method, we are forced to change every node in the hierarchy. A mechanism is necessary for processing to be decoupled from the nodes in a hierarchy. When we use visitor pattern, the nodes within the...