Paul J. Lucas <pauljlucas <at> mac.com> writes:
> On Nov 26, 2007, at 2:33 AM, Jerome Louvel wrote:
>
> > Your suggestion regarding the map of ConverterService is
> > interesting. However, how would you handle the reverse conversion
> > (from Representation to Object) ?
>
> Also using a map. For both conversion directions, you allow for the
> possibility of subclasses, e.g.:
>
> public class ConverterServiceMap extends ConverterService {
> // ...
>
> public Object toObject( Representation rep ) {
> for ( Class c = rep.getClass(); c != Object.class;
> c = c.getSuperclass() ) {
> final ConverterService cs = m_toObjMap.get( c );
> if ( cs != null )
> return cs.toObject( rep );
> }
> return super.toObject( rep );
> }
> }
I haven't actually tried this, but couldn't you just overload the toObject()
and toRepresentation() methods? Doesn't Java do the work of choosing the
method with the most specific argument?
For example:
public class ConverterService extends ConverterService {
public Foo toObject(FooRepresentation fr);
public FooRepresentation toRepresentation(Foo f);
}