advice on class structure

P

Peter

Hi

I would like some advice on a class hierarchy.

I have "converter" classes and "specific result" classes. The
"converter" classes each accept one "specific result" which they
convert to a "GeneralResult".

For example, I have

class TankerConverter
{
GeneralResult Convert(TankerResult tanker){}
}

class SpeedboatConverter
{
GeneralResult Convert(SpeedboatResult speedboat){}
}

class DestroyerConverter
{
GeneralResult Convert(DestroyerResult destroyer){}
}

Now I need to make this more general, as I have a controller program
which receives a "converter" and a "result" which it should combine to
produce a "GeneralResult".

For example, I want something like:

GeneralResult Perform(IConverter converter, IResult result)
{
return converter(result);
}

But the problem I have is that the specific result classes don't really
have anything in common (it is the specific converter classes which
know the details of the associated result class) - so I can't really
define an IResult with known methods...

Does anyone have any ideas to a "generic" structure for this?

Thanks,
Peter
 
P

Pavel Minaev

I would like some advice on a class hierarchy.

I have "converter" classes and "specific result" classes. The
"converter" classes each accept one "specific result" which they
convert to a "GeneralResult".

For example, I have

class TankerConverter
{
  GeneralResult Convert(TankerResult tanker){}

}

class SpeedboatConverter
{
  GeneralResult Convert(SpeedboatResult speedboat){}

}

class DestroyerConverter
{
  GeneralResult Convert(DestroyerResult destroyer){}

}

Now I need to make this more general, as I have a controller program
which receives a "converter" and a "result" which it should combine to
produce a "GeneralResult".

For example, I want something like:

GeneralResult Perform(IConverter converter, IResult result)
{
  return converter(result);

}

But the problem I have is that the specific result classes don't really
have anything in common (it is the specific converter classes which
know the details of the associated result class) - so I can't really
define an IResult with known methods...

Does anyone have any ideas to a "generic" structure for this?

The answer is right in your question already - use generics!

interface IConverter<TResult> {
GeneralResult Convert(TResult result);
}

class TankerConverter : IConverter<TankerResult> { ... }
...

GeneralResult Perform<TResult>(IConverter<TResult> converter,
TResult result)
{
  return converter.Convert(result);
}
 
P

Peter

Pavel said:
The answer is right in your question already - use generics!

interface IConverter<TResult> {
GeneralResult Convert(TResult result);
}

class TankerConverter : IConverter<TankerResult> { ... }
...

GeneralResult Perform<TResult>(IConverter<TResult> converter,
TResult result)
{
  return converter.Convert(result);
}

Thanks very much. It was just like that I was aiming for - but I
couldn't quite work out what the signature for the general "Perform"
method should be.

Thanks,
Peter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top