vendredi 18 septembre 2020

Create a class based on combination of other classes

I have the same problem as this I have some problems about this project?, but due to the title was poorly written, the topic has been closed.
Basically, I have n classes, derived from a base class. Build a new class with its behaviors and attributes are based on matched combination (predefined) of those derived classes.
In his code he used a lot of if cases to find out the combination, which is cumbersome and the expandability is almost impossible. Can a design pattern replace those if cases?
E.g:

A+B+A = B+A+A = ... = A+B (combination 1)
A+A+A = no combination  

Pseudocode:

class base

class A : public base
class B : public base
class C : public base

class combine : public base{
  void combine(base&, base&, base&){
    if(isA && isB && isC)
        //implement combination1 here
    else((isA && isB)) || (isB && isA))
        //implement combination2 here
    else(...)//and a lot more
  }  
};

Aucun commentaire:

Enregistrer un commentaire