On Apr 24, 1:36*am, "Peter" <xdz...@hotmail.com> wrote:
> 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);
}
|