0

Thanks for taking the time too look at my question. It's pretty general but I can't seem to find very much information on it. I've looked at similar questions but none of the examples seem to be this convoluted.

In C++ I am looking for the proper way that sub-classes should interact given that each pair has a different set of operations.

Lets say I have the following classes with sub-classes.

      Vehicle                                          Tires
         |                                               |
      Motorcycle                                       / | \
       /  |  \                                   Snow Seasonal Summer 
  Honda Suzuki Yamaha

Let's say I have a function for each type (Honda, Suzuki, Yamaha) called Performance(Tires tires) that does certain operations based on the type of tires. I was wondering what would be the "best practices" way to implement this in a general way. The issue that I am running into is that each pair has different operations that need to be done, e.g. Honda.Performance(snowTires) is different from Honda.Performance(seasonalTires) and it is also different from Suzuki.Performance(snowTires). My issue is that implementing something like the following seems like a poor solution because it would also have to be implemented for the other subclasses of Motorcycle.

void Honda::Performance(Tires tires)
{
if(tires are Snow)
{
  //snow ops
}
elseif(tires are seasonal)
{
   //seasonal ops
}
else
{
   //summer ops
}
}

Any constructive suggestions or advice are appreciated.

Thanks!

sokato
  • 354
  • 4
  • 14
  • 1
    [_Decorator_ or _Strategy_ design patterns](https://sourcemaking.com/design_patterns)? Or both. – πάντα ῥεῖ Jan 19 '21 at 20:40
  • @πάνταῥεῖ I will take a look at that thanks. – sokato Jan 19 '21 at 20:42
  • fwiw, not everything must be a member function. `Suzuki.Performance(snowTires)` can be `performance(Suzuki,SnowTires)` – 463035818_is_not_an_ai Jan 19 '21 at 21:05
  • @largest_prime_is_463035818 in that case wouldn't I still need to do the if/case method for each pair? – sokato Jan 19 '21 at 21:09
  • 1
    if every combination is different you need to implement every combination, the question is just how to organise them. Instead of long if/else chains you would have one overload for each combination – 463035818_is_not_an_ai Jan 19 '21 at 21:11
  • 4
    Useful related reading: [What is an example of the Liskov Substitution Principle?](https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle). TL;DR version: if you have subclasses that cannot or should not follow the same interface, you're in for some pain. – user4581301 Jan 19 '21 at 21:12
  • @user4581301 I will check that out thanks. – sokato Jan 19 '21 at 21:15
  • @πάνταῥεῖ I think the Strategy design pattern may be a good option for my application. I think I will give it a go and see where that takes me. Thanks! – sokato Jan 19 '21 at 22:25
  • 1
    Are you really passing in your Tires by value? Because, they will get sliced. – jxh Jan 19 '21 at 23:57
  • @jxh This was just a simple example based on my situation to understand the best organization. – sokato Jan 20 '21 at 00:28
  • @sokato Trying to use the given example as a basis for testing doesn't work. A reference or a pointer is needed for the dynamic binding to be valid. – jxh Jan 20 '21 at 00:30
  • @jxh Using a reference or pointer is fine. The example was just to present the idea. – sokato Jan 20 '21 at 06:18
  • What is the root of the difference in calculating performance of `snowTires` vs `seasonalTires`? – Robert Andrzejuk Jan 20 '21 at 12:23
  • @RobertAndrzejuk The actual code is a thermodynamic/chemical kinetics code that calculates properties of different types of chemical reactors e.g., constant volume, constant pressure, etc. So, there are differences in the equations of each. I just put this example together to get the idea across. So, the two classes are not defined the only constraint is that they require different equations lets say. – sokato Jan 20 '21 at 19:41

0 Answers0