2 class hierarchies design question

Discussion in 'Java' started by S. S. Tsay, Jun 24, 2004.

  1. S. S. Tsay

    S. S. Tsay Guest

    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
     
    S. S. Tsay, Jun 24, 2004
    #1
    1. Advertising

  2. S. S. Tsay

    Guest

    (S. S. Tsay) wrote in message news:<>...
    > 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
     
    , Jun 25, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad Smallridge
    Replies:
    1
    Views:
    451
    Mike Treseler
    May 21, 2005
  2. Tomer Ben-David
    Replies:
    4
    Views:
    1,722
    Alan Gutierrez
    Nov 28, 2004
  3. Matthias Kaeppler

    Parametric covariance in class hierarchies

    Matthias Kaeppler, Jan 22, 2006, in forum: Java
    Replies:
    6
    Views:
    403
    Roedy Green
    Jan 23, 2006
  4. Matt
    Replies:
    9
    Views:
    490
    Francesco S. Carta
    Oct 9, 2009
  5. Ulrich Eckhardt

    unit testing class hierarchies

    Ulrich Eckhardt, Oct 2, 2012, in forum: Python
    Replies:
    13
    Views:
    293
    Terry Reedy
    Oct 3, 2012
Loading...

Share This Page