0

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()
    }
}
5
  • 1
    Is the logic in A7 and B7 intrinsically the same? If A7 does not work as it was intended to work (it was specified incorrectly), does that mean that B7 also has to change? Aug 22 '20 at 9:15
  • Yes. For simplicity, you can think of A7 and B7 as having only one parameter in their class (the one passed in their Ctr) so the only change that could be done is inside the commonLogic. The only issue here is the different type that they are operating on.
    – Ace
    Aug 22 '20 at 9:32
  • Your solution looks fine-ish. I’d question why you need an extra class and an instance of it. A generic free function might do the trick as well. Of course if that is possible depends on the restrictions your programming language places on you.
    – besc
    Aug 22 '20 at 9:34
  • @besc - that's also true. But where would that generic function be? I'm writing in c#
    – Ace
    Aug 22 '20 at 10:13
  • @Ace It would be in some kind of utility/helper package/file that both relevant classes can access. I dabbled too little in C# too long ago to be more concrete about how an elegant solution would look there.
    – besc
    Aug 22 '20 at 16:54
3

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.

enter image description here

3
  • 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 logic
    – Ace
    Aug 22 '20 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. Aug 22 '20 at 17:06
  • Thanks for you feedback - makes sense
    – Ace
    Aug 22 '20 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.