S
S. S. Tsay
I have 2 class hierarchies: Processors and Reports.
abstract Processor
Processor1
Processor2
...
abstract Report
ReportA
ReportB
...
Processor has some data that will be changed whenever it gets a
report. Each Processor/Report method could have a different
implementation. I had planned on doing something like this:
class Processor {
dataType someData; // varies for different processors
process(ReportA);
process(ReportB);
process(Report...);
// not actually using 1.5, just for brevity
// ProcessReports called by some other object
ProcessReports(ArrayList<Report> reports) {
for (Report r : reports) {
p.process(r);
}
}
}
So each Processor subclass would override the process methods that it
needed to.
The problem is that java doesn't decide which overloaded function to
call at runtime, so it will always look for Processor(Report), no
matter what the runtime type of r is in ProcessReports.
I could move the process function to report and pass it a reference to
a Processor, but I would them have the same problem, just mirrored. I
could also use instanceof, but there has got to be a better design.
This must be a fairly typical problem, but I can't come up with a
design that allows me not to use a bunch of ugly if instanceof else if
inst... Any suggestions?
Thanks,
TS
abstract Processor
Processor1
Processor2
...
abstract Report
ReportA
ReportB
...
Processor has some data that will be changed whenever it gets a
report. Each Processor/Report method could have a different
implementation. I had planned on doing something like this:
class Processor {
dataType someData; // varies for different processors
process(ReportA);
process(ReportB);
process(Report...);
// not actually using 1.5, just for brevity
// ProcessReports called by some other object
ProcessReports(ArrayList<Report> reports) {
for (Report r : reports) {
p.process(r);
}
}
}
So each Processor subclass would override the process methods that it
needed to.
The problem is that java doesn't decide which overloaded function to
call at runtime, so it will always look for Processor(Report), no
matter what the runtime type of r is in ProcessReports.
I could move the process function to report and pass it a reference to
a Processor, but I would them have the same problem, just mirrored. I
could also use instanceof, but there has got to be a better design.
This must be a fairly typical problem, but I can't come up with a
design that allows me not to use a bunch of ugly if instanceof else if
inst... Any suggestions?
Thanks,
TS