PC Review


Reply
Thread Tools Rate Thread

advice on class structure

 
 
Peter
Guest
Posts: n/a
 
      24th Apr 2009
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
 
Reply With Quote
 
 
 
 
Pavel Minaev
Guest
Posts: n/a
 
      24th Apr 2009
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);
}

 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      27th Apr 2009
Pavel Minaev wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Advice for field structure or query structure - not getting theresults I thought I should TPK Microsoft Access Reports 2 8th Jul 2010 09:20 PM
Is there a structure/class to store meta-info of a class so that I can generate code to a file? A.Neves Microsoft C# .NET 1 12th Apr 2006 06:50 PM
Using a structure as a property in a class gives error when accessing properties of structure D Witherspoon Microsoft VB .NET 6 5th Mar 2005 06:43 AM
Using a structure as a property in a class gives error when accessing properties of structure D Witherspoon Microsoft Dot NET 6 5th Mar 2005 06:43 AM
Using a structure as a property in a class gives error when accessing properties of structure D Witherspoon Microsoft Dot NET Framework Forms 6 5th Mar 2005 06:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 AM.