 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Andrew Brampton Guest
|
Posted: Sun Apr 01, 2007 12:19 am Post subject: Return instance of subclass |
|
|
Hi,
I've encoutered a design problem that I'm unsure how to solve in a neat way.
I have a class called Ranges, which represents a set of numerical ranges,
such as [0-10] [30-50], etc. And I can OR different Range instance together
to work out the overlaps.
class Ranges {
// Returns the overlapping regions of the two ranges
public Ranges overlap ( Ranges r ) {
Ranges ret = new Ranges();
//do something
return ret;
}
}
Now I have a RangeCounts class, which does a similar thing, but assigns a
value to these ranges., such that [0-10 val 1] [30-40 val 2] [40-50 val 1].
Now this works in a very similar way to Ranges, and infact extends Ranges.
class RangeCounts extend Ranges {
// Does exactly the same as the parent, thus not defined
//public Ranges overlap ( Ranges r ) {}
}
Now the problem is, the overlap method in RangeCounts is the exact same code
as Ranges, thus I don't overload it. BUT I want the overlap method to return
a new RangeCounts object, not a Ranges object. Now I can make this happen by
overloading overlap, and duplicating the code. OR I can create another
method that returns a new instance of that class, and make sure both Ranges
and RangesCount implement this method. That way in Ranges.overlap, it will
call this method instead of doing "new Ranges()".
But both ways seem ugly to me.
How do people normally solve this problem? Hopefully I've explained it well
enough.
Thanks
Andrew |
|
| Back to top |
|
 |
Lew Guest
|
Posted: Sun Apr 01, 2007 6:32 am Post subject: Re: Return instance of subclass |
|
|
Andrew Brampton wrote:
| Quote: | Hi,
I've encoutered a design problem that I'm unsure how to solve in a neat way.
I have a class called Ranges, which represents a set of numerical ranges,
such as [0-10] [30-50], etc. And I can OR different Range instance together
to work out the overlaps.
class Ranges {
// Returns the overlapping regions of the two ranges
public Ranges overlap ( Ranges r ) {
Ranges ret = new Ranges();
//do something
return ret;
}
}
Now I have a RangeCounts class, which does a similar thing, but assigns a
value to these ranges., such that [0-10 val 1] [30-40 val 2] [40-50 val 1].
Now this works in a very similar way to Ranges, and infact extends Ranges.
class RangeCounts extend Ranges {
// Does exactly the same as the parent, thus not defined
//public Ranges overlap ( Ranges r ) {}
}
Now the problem is, the overlap method in RangeCounts is the exact same code
as Ranges, thus I don't overload it. BUT I want the overlap method to return
a new RangeCounts object, not a Ranges object. Now I can make this happen by
overloading overlap, and duplicating the code. OR I can create another
method that returns a new instance of that class, and make sure both Ranges
and RangesCount implement this method. That way in Ranges.overlap, it will
call this method instead of doing "new Ranges()".
But both ways seem ugly to me.
How do people normally solve this problem? Hopefully I've explained it well
enough.
|
Why not use a Map< Ranges, Integer > instead of extending Ranges?
-- Lew |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|