2 class hierarchies design question

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
 
I

iamfractal

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

This is polymorphism in action: you want each report to be treated,
not as its subclass, but as its super class; that is, you want ReportA
to be treated as Report, not ReportA.

A consequence of this is that each subclass must know that which makes
it a subclass, and no other class must have this knowledge. In other
words, you can't take the processing of each report out of the report
itself: you have to include the processing of each report in each
report.

The reason for this is that each report must know how to process
itself: this is encapsulation of report-specific knowledge within the
report.

So Report should have a process() method, which each sub-class can
then implement according to its own type.

If you want maintain a Processor encapsulation, then you can, but each
report must be able to access the Processor of its choice
(double-dispatch will allow you, in this case to have runtime-type
identification - if sub-class ReportA calls Processor.processMe(this)
then this will call Processor.processMe(ReportA report) and not
Processor.processMe(ReportA report) - though be advised that
double-dispatch will introduce circular-dependencies, which aren't
generally good; it is still better to have Processor that depends only
on Report and not on any subclasses).

..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top