Sagewire Logo

Return instance of subclass

2 Message(s) by 2 Author(s) originally posted in java language


From: Andrew Brampton Date:   Saturday, March 31, 2007
Hi,
I have 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 range s,
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 extend s 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 do not 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'll
call this method instead of doing "new Ranges()".

But both ways seem ugly to me.

How do people normally solve this problem? Hopefully I have explained it well
enough.

Thanks
Andrew


From: Lew Date:   Saturday, March 31, 2007
wrote in message:
Hi,
I have 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 do not 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'll
call this method instead of doing "new Ranges()".
But both ways seem ugly to me.
How do people normally solve this problem? Hopefully I have explained it well
enough.



Why not use a Map< Ranges, Integer > instead of extending Ranges?

-- Lew



Next Message: function in derby[beginner]


Blogs related to Return instance of subclass

Creating a field comparison validator for Crank (JSF, Spring)
public static ValidationContext getCurrentInstance() { return (ValidationContext) holder.get(); } /**Allows the subclass to register an instance of itself * as the context. The subclass will either be JSF, Struts 2 (WebWork) or * Spring ...

Ruby Class Tutorial
As we just saw, instance methods are invoke using an object instance as the receiver. Ruby also supports class methods that belong to the class and not on an object instance. In Java class methods are also known as static methods. ...

Final? Not yet...
... when a subclass of a semifinal class is used as the type of a reference, parameter or method return). For instance, say you have an interface I that has methods mA() and mB() , and one implementation X that adds a method mC() . ...

[jira] Commented: (TOMAHAWK-952) Provide cleaner, consistent ...
dataModel instanceof BaseSortableModel)) > { > dataModel = new BaseSortableModel(dataModel); > } > ((BaseSortableModel)dataModel).setComparator(comparator); > } > return dataModel; > } > After stripping out the comparator stuff from ...

Cocoa-Java Porting Step 2: Life Support
When the Java class you’re messaging is in fact a subclass of a Cocoa class, the messaging can feel extremely natural. For instance, assuming you’ve got a Java-based Preferences Window controller, this is how you might message it to ...

Spring: Prototype beans with Singletons - 1
CmdMgrServiceFactory.java. Invoking the no-arg getCommand() method or the single-arg method with null, will return an instance of the Command object by matching it's type if there is only one bean of the type beans.Command. ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional