mardi 5 mai 2020

architecture pattern: function to handle 2 identical implimentation

I have the following problem which I guess I am solving incorrectly given the problem I am facing:

I have an interface I and implementations A, B, C... I want to somehow express that I can get some results from couples (f(A, A), f(B, B), f(C, C)) and so on. In other words, I want to interface I to express that 2 identical implementations can be combined to produce some results, while others can be not (you can't get any valid result from f(A, B)).

Right now I have the following:

class I{
  virtual result f (I &other) = 0;
  virtual result fSpecific (A &other) { throw runtime_error(""); };
  virtual result fSpecific (B &other) { throw runtime_error(""); };
  virtual result fSpecific (C &other) { throw runtime_error(""); };
}

class A{ 
 result f (I &other) override { other->fSpecific(*this); }
 result fSpecific (A &other) override { /*logic here*/ }
}

class B{ 
 result f (I &other) override { other->fSpecific(*this); }
 result fSpecific (B &other) override { /*logic here*/ }
}
/*and so on*/

But I guess I use a wrong architecture. as adding classes results in changing I. Are there any better solution to express this such a relation?

Aucun commentaire:

Enregistrer un commentaire