I have the following class hierarchies
abstract class Base<T>
{
abstract T getRelevantType();
}
class A : Base<AType>
{
AType getRelevantType()
}
class B : Base<BType>
{
BType getRelevantType()
}
class A1 : A {
getRelevantType()
// logic
}
class A2 : A {
getRelevantType()
/// logic
}
<...>
class B1 : B {
getRelevantType()
// logic
}
class B2 : B {
getRelevantType()
// logic
}
Now I have a situation where I have a logic that needs to be run once with AType
and once with BType
.
class A7 : A
{
// same logic
}
class B7 : B
{
// same logic
}
Each one of the classes A1 - B1 implements an interface for its specific logic.
Now, I'm just thinking without changing the current design of (A1 : A, B1 : B ...) if there's a way I can achieve it or maybe a design pattern I'm not aware of (since I can't inherit from multiple classes).
The only way I thought of is to create a generic class which would contain the same logic and which may be passed as a parameter to each one of the different classes.
E.g.
class CommonLogic<T>()
{
public doLogic();
}
class A7 : A
{
Ctr (commonLogic logic)
getRelevantType()
{
logic.doLogic()
}
}
class B7 : B
{
Ctr (commonLogic logic)
getRelevantType()
{
logic.doLogic()
}
}
A7
andB7
intrinsically the same? IfA7
does not work as it was intended to work (it was specified incorrectly), does that mean thatB7
also has to change?