Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > 2 class hierarchies design question

Reply
Thread Tools

2 class hierarchies design question

 
 
S. S. Tsay
Guest
Posts: n/a
 
      06-24-2004
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
 
Reply With Quote
 
 
 
 
iamfractal@hotmail.com
Guest
Posts: n/a
 
      06-25-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (S. S. Tsay) wrote in message news:<(E-Mail Removed). com>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unit testing class hierarchies Ulrich Eckhardt Python 13 10-03-2012 08:14 PM
How to create an alias - overlaying two class hierarchies Matt C++ 9 10-09-2009 11:31 AM
Parametric covariance in class hierarchies Matthias Kaeppler Java 6 01-23-2006 09:01 AM
Hierarchies not the best for video pipelines Brad Smallridge VHDL 1 05-21-2005 02:10 AM
Design question : Parallel Inheritance Hierarchies Tomer Ben-David Java 4 11-28-2004 07:39 PM



Advertisments