Re: Interface Delegation or ??
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Dec 15, 9:12 pm, Rob McDonald <rob.a.mcdon...@gmail.com> wrote:
Then generics are likely your answer, as REE9o...@gmail.com suggested.
How can I use generics to do this? I want to extend from an arbitrary
base class (known to implement some interface).
My experience with generics is very much in the spirit of
Collections<T>. Which really doesn't seem to apply here.
I guess I'll go hit the books some more, but I thought this was the
question I asked in the first place.
Rob
Below is sample code. This code works for one dimensional space
(points are BigDecimals). You can extend it to other dimensions by
(1) implementing some class to represent the higher dimension points
and (2) implementing some DistanceMetric class that works on the new
Point class. If you do 1 and 2 correctly, you should be able to use
the ListSegmentFactory without any further modifications.
I hope this helps.
Emory Merryman
External Concepts Guild
/**
* Useful for making things.
**/
interface Factory < R , P , E extends java . lang . Exception >
{
public abstract R make ( final P p ) throws E ;
}
/**
* Can be used to calculate the distance between two Points
**/
interface DistanceMetric < P , R extends java . lang . Comparable < R
{
public abstract R distance ( final P p1 , final P p2 ) ;
}
/**
* Calculates the euclidean distance between two one dimension points
* represented by java.math.BigDecimal.
**/
final class Distance1D implements DistanceMetric < java . math .
BigDecimal , java . math . BigDecimal >
{
public final java . math . BigDecimal distance ( final java .
math . BigDecimal p1 , final java . math . BigDecimal p2 )
{
final java . math . BigDecimal difference = p1 . subtract
( p2 ) ;
final java . math . BigDecimal distance = difference . abs ( ) ;
return ( distance ) ;
}
}
/**
* Segments have length.
**/
interface Segment < P , R extends java . lang . Comparable < R > >
{
public abstract R length ( ) ;
}
/**
* Makes a Segment based on the specified ordered list of points.
* Uses the specified distance metric to calculate the length.
**/
final class ListSegmentFactory < P > implements Factory < Segment <
P , java . math . BigDecimal > , java . util . List < P > , java .
lang . RuntimeException >
{
ListSegmentFactory ( final DistanceMetric < P , java . math .
BigDecimal > dm )
{
super ( ) ;
this . dm = dm ;
}
private DistanceMetric < P , java . math . BigDecimal > dm ;
public final Segment < P , java . math . BigDecimal > make ( final
java . util . List < P > points )
{
final Segment < P , java . math . BigDecimal > segment = new Segment
< P , java . math . BigDecimal > ( )
{
public final java . math . BigDecimal length ( )
{
java . math . BigDecimal length = java . math . BigDecimal .
ZERO ;
for ( int i = 1 ; i < points . size ( ) ; i ++ )
{
final P p1 = points . get ( i - 1 ) ;
final P p2 = points . get ( i ) ;
final java . math . BigDecimal distance = dm . distance ( p1 ,
p2 ) ;
length = length . add ( distance ) ;
}
return ( length ) ;
}
} ;
return ( segment ) ;
}
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
iD8DBQFHaBYMVQdj5Q2e9q0RAsZlAKCEcgrENuBMLAx5cvwt7p+cRc/H2QCghumj
BCDb1SMnc9Ka/jgE4xKClRw=
=MzTY
-----END PGP SIGNATURE-----