Question
How can I adhere to the principle of "Report, do not ask" when executing a function with multiple objects.
Example - Reporting
I have the following objects (for illustration only):
Car, horse, rabbit
There is no connection between these objects, but I want to create a report based on these objects:
createHtmlReport(Car car, Horse horse, Rabbit rabbit){ Report report = new Report() report.setSomeField(car.getSerialNumber()) report.setAnotherField(horse.getNumberOfLegs())
The problem with this method is that it must "pull" data from each object, which violates the "Report, Do Not Ask" rule. I would rather keep the insides of each object hidden and make them generate a report for me:
car.createHtmlReport() horse.createHtmlReport() rabbit.createHtmlReport()
... but then I get 3 partial reports. Also, I don’t think that Rabbit should know how to generate every single report that I need (HTML, JMS, XML, JSON ....).
Finally, when creating a report, I can include several elements:
if (car.getWheels() == 4 || horse.getLegs() == 4) // do something
djcredo
source share