NH-2,BYPASS,ROAD BHADWAR,VARANASI, PIN CODE 2213311
This article was adapted from a Google Tech on the Toilet (TotT) episode. You can download a printer-friendly version of this TotT episode and post it in your office.
By Shahar Roth
Classes require various objects and parameters to function. The “Construct with Collaborators, Call with Work” guideline can help you construct effective inputs:
Consider a ReportGenerator that needs a database, a formatter, and a date range to generate a report. The database and formatter, as collaborators, are injected via the constructor, while dateRange, which varies per report generation, is passed as a method parameter to the generate method:
| class ReportGenerator {
private final Database database; private final Formatter formatter; // database and formatter are passed as collaborators. ReportGenerator(Database database, Formatter formatter) { this.database = database; this.formatter = formatter; } // dateRange is passed as a parameter. Report generate(Range<Instant> dateRange) { return formatter.format(database.getRecords(dateRange)); } } |
A single ReportGenerator object can generate multiple reports with different date ranges:
| ReportGenerator generator = new ReportGenerator(database, formatter);
Report report1 = generator.generate(dateRange1); Report report2 = generator.generate(dateRange2); |
Following the “Construct with Collaborators, Call with Work” guideline promotes:
Note that the definition of “collaborator” versus “work” depends on the object’s identity. For example, a RequestMessage could be a collaborator for a RequestHandler if the handler operates on a single request, or work if the handler processes different requests with each method call.
Copyright by Heritage Nursing College 2023. Design & Developed By Sarthak Tech