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()
}
}
1 Answer 1
If I understood well:
- It looks like
Base
abstract class is at the top of the hierarchy. - Every other class in your example is a subclass of
Base
.
Then
- Add the common logic inside abstract class
Base
.
Am I missing something?
You can use the Template-ish Pattern by putting inside Base
an skeleton of the algorithm for subclasses to implement specific parts. That in the case the logic should have specificities, but I believe the logic will be the same for all subclasses. I guess the algorithm is generic enough since the Base
class uses a generic, right? Each subclass should return the relevant type.
-
Thanks for the answer @tulains. Each one of the sub classes except for A7 and B7 have a different logic so I'm not sure how to put a skeleton of the algorithms in the base would work here. I forgot to mention it in the original post but each one of the A1... B1... implements an interface for its specific logicAce– Ace2020年08月22日 16:41:56 +00:00Commented Aug 22, 2020 at 16:41
-
@Ace Then it looks the Strategy Pattern is what you need. But you don't have to implement it all around. A7 and B7 have to implement an interface that accepts a Strategy and then override the doLogic() method to call the strategy's doLogic() method instead. It's exactly the solution that you thought of but using a method to inject the logic instead of a constructor. So you were on the right track to begin with.Tulains Córdova– Tulains Córdova2020年08月22日 17:06:27 +00:00Commented Aug 22, 2020 at 17:06
-
Thanks for you feedback - makes senseAce– Ace2020年08月22日 17:32:04 +00:00Commented Aug 22, 2020 at 17:32
Explore related questions
See similar questions with these tags.
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?