Project 1 - Phase 2
Visitor Pattern
- Visitor Pattern
- There is an interface "Visitor" which contains methods for each
class in the hierarchy.
- Each class in a hierarchy implements an interface "Visitable" that
contains a single method "accept", which takes a single Visitor as
a parameter. This method simply calls the proper method in the
Visitor.
- The visitor hierarchy sacrifices the ability to add new classes to a
hierarchy for the ability to add new methods.
- Diagram
- Why is it useful here?
- Because of our inheritance scheme, there is a need for virtual
methods. While this could be done by modifying the generated code,
this is a bad idea.
- The sacrifice of a dynamic hierarchy for new methods is not a big
deal because the hierarchy can be re-generated to add new
functionality.