redeveloper
redeveloper

Reputation: 119

Merging Multiple Objects to Multiple Formats

I'm looking for the best design to apply to a situation with multiple DataObjects and multiple output formats (ReportGenerators).

The current setup is something like this: there's a Formattable interface that has various methods used by a ReportGenerator. Each DataObject (DataObject could be any of several different unrelated classes) implements Formattable -- ie, it knows how to organize its particular data for the report, and the ReportGenerator is simply fed Formattable objects from which it accesses String arrays and pretties them up.

1) So here is my first question: is this advisable? Because the result is that the process of organizing data for the report is strewn throughout all the DataObjects. The alternative is an intermediary class with a whole bunch of instanceof checks on what kind of DataObject to format, but at least then it's all in one place.

2) And then my new problem is that I need to introduce a secondary ReportGenerator which will need DataObject data organized slightly differently. In the current setup, I could introduce a parameter on Formattable methods to specify what kind of ReportGenerator the DataObject should format itself for, but again, not sure if this is advisable.

Upvotes: 3

Views: 58

Answers (1)

Chetan Kinger
Chetan Kinger

Reputation: 15212

Let's address your high-level question with high-level pointers :

DataObject could be any of several different unrelated classes) implements Formattable -- ie, it knows how to organize its particular data for the report

You say that DataObject is going to organize the data and then you say :

ReportGenerator is simply fed Formattable objects from which it accesses String arrays and pretties them up

Is there really a difference between formatting and organizing? In a way, organizing data seems to be a preliminary step to formatting data and should ideally be treated as a single responsibility rather than treating it as multiple responsibilities. Translating this to code means that a single class should organize/format (these terms do seem to be interchangable) data. ReportGenerator would be a prime candidate for this.

And then my new problem is that I need to introduce a secondary ReportGenerator which will need DataObject data organized slightly differently

If ReportGenerator takes up the responsibility of organizing/formatting data, this problem should be solved as well.

In conclusion, it would be a good idea to move the organizing of data from the DataObjects to the ReportGenerator.

Upvotes: 2

Related Questions