The concept of "visitors", i.e. the Visitor Design Pattern, in object-oriented design is important not just in how it is a useful pattern to use but more fundamentally, in how it illustrates so many core cocepts of good design.
Component-Framework Systems -- Visitors and their hosts create a component-framework system.
Overview: Standard Visitors
- Visitors are designed for type-dependent processing
- Delegation-model programming -- delegate to the object to determine what to do -- objects know unequivocably what to do because they do what they are.
- "Inverted control" -- Let the data (the "host") decide what case of the algorithm (the visitor) to invoke.
- Invariants
- Hosts invariantly call their associated case on the visitor
- Visitors invariantly have a case for every possible host
- Leads to an invariant number of hosts
- Variants:
- Algorithms (the visitors) are variant
- The algorithms are specified in terms of the cases for each type of host
- Declarative programming
- No specification of how the algorithm determines what case to run.
- The inverted control delegation polymorphically determines which case is run.
- Generic List Frameworks
Overview: Extended Visitors
- Extended visitors preserve the fundamental visitor invariant of "HostX calls caseX()"
- Standard visitors: Invariant number of hosts vs. Extended visitors: Variant number of hosts
- Standard visitors --> Extended visitors:
- Fixed case names become parameterized values in a single "
caseAt
" method..
- Default case handles all cases not explicitly defined.
- Extended visitor implementation:
- Can use
if-else
or switch
statements internally.
- Use lambdas to represent the cases.
- The extended visitor truly becomes a "collection of semantically related commands"
- Generic Extended Visitors and Data Packets
Algorithms
Examples and Demos:
© 2020 by Stephen Wong