A design question for experienced OO designers

  • Thread starter Thread starter Zeng
  • Start date Start date
Z

Zeng

From the below situation, it's painful to keep declaring and implement the
similar methods just different return type for each list class;
alternatively, we can just force the client code use RecordBaseList's
methods directly and converting the return object to the correct type on its
own (from RecordBase to RecordTypeA or RecordTypeB)- which is also painful.
Any experienced OO designer out there that can suggest me a better design?
Thank you very much in advance....

abstract class RecordBase
{
abstract public int commonF1();
abstract public int commonF2();
}

public class RecordTypeA : RecordBase
{
override int commonF1() {...}
override int commonF2() {...}

public int FooA() {...}
}

public class RecordTypeB : RecordBase
{
override int commonF1() {...}
override int commonF2() {...}

public int FooB() {}
}


abstract class RecordBaseList
{
public RecordBase FindRecord( int id ) {...}
public RecordBase AddNewRecord() {...}
public RecordBase FirstRecord() {...}
...
...
}

public class RecordTypeAList : RecordBaseList
{
new public RecordTypeA FindRecord( int id ) // we are hiding the method
of base class here
{
return (RecordTypeA) base.FindRecord( id );
}

new public RecordTypeA AddNewRecord() // we are hiding the method of
base class here
{
return (RecordTypeA) base.AddNewRecord( id );
}

new public RecordTypeA FirstRecordRecord() // we are hiding the method
of base class here
{
return (RecordTypeA) base.AddNewRecord( id );
}
}

//// FOR RecordTypeBList, WE WOULD SIMILAR CODE AS RecordTypeAList, JUST
DIFFERENT RETURN TYPE AND TYPE CONVERSION AT RETURN STATEMENT.
 
Zeng,
Have you considered using Eiffel. As it fully supports covariant return
types (overriding a function with a derived return type).

http://www.eiffel.com

I normally avoid designing class hierarchies where I need to change the
return type on derived classes. Although a couple of places I thought it
would have been useful. I figured out I didn't really need it. As you say
the consumer can change the type if they really need to.

Hope this helps
Jay
 
From the below situation, it's painful to keep declaring and implement
the similar methods just different return type for each list class;
alternatively, we can just force the client code use RecordBaseList's
methods directly and converting the return object to the correct type on
its own (from RecordBase to RecordTypeA or RecordTypeB)- which is also
painful. Any experienced OO designer out there that can suggest me a
better design? Thank you very much in advance....
Code:
//// FOR RecordTypeBList, WE WOULD SIMILAR CODE AS RecordTypeAList, JUST
DIFFERENT RETURN TYPE AND TYPE CONVERSION AT RETURN STATEMENT.[/QUOTE]

Basicly you are trying to write a typed collection. This is not
possible in currenct C#, you have to wait for generics OR use a generator to
produce this code, since it is very repetitive.

FB
 
Back
Top