Let's say I have an Object Structure like this that I import data into from a source:
Reporting:
Body:
ReportingEntity:
DocSpec
Reports[]:
ConstEntities[]
DocSpec
AdditionalInfo[]
DocSpec
MessageSpec
Whenever I import the data I have to check for changes in any of the objects and update the DocSpec objects within dependent of the type of change (no change
, correction
, added data
, removed data
). Then I have to split the object and filter the correction
and removed
data into one object and filter added data
into a second object. Each of them will be written to its own XML file and sent to an authority.
On first glance this looks like a great opportunity to use the Visitor Pattern for UpdateReportingVisitor
and FilterReportingVisitor
but I also see a major difference here: A visitor only traverses one object structure and does its operations on it. It doesn't work on a second object simultaneously. An accept()
method for UpdateReportingVisitor
would look like this:
class Body(object):
def accept(self, visitor, other):
"""rough draft to communicate the concept. This is not the actual implementation!"""
visitor.visit_body(self, other)
for self_report, other_report in zip(self.reports, other.reports):
self_report.accept(visitor, other_report)
for self_info, other_info in zip(self.additional_infos, other.additional_infos):
self_info.accept(visitor, other_info)
while FilterReportingVisitor
wouldn't need that extra argument.
I'm a bit uncomfortable with this solution because of that other
argument in each accept()
method that clearly doesn't belong to the pattern. Would this approach still be considered a visitor pattern? (If no: Does it have a different name?) is there a different (better?) pattern to solve the problem i have?
accept()
for all visitors in each class i will need at least 2 of those methods in each class. Maybe there will be a third case that will need a third implementation of the visitor pattern one day so I was wondering if this pattern might be wrong abstraction. Your reaction helped me a lot though. Thanks! :-) – Tekay37 Aug 2 '18 at 15:49