lundi 12 décembre 2016

"Interface" classes to access ancillary computations of a base class

I am wondering how to best (in terms of efficiency and readability) implement a design pattern involving a base class and 2 interface classes (I1 and I2) that access a class A that performs some relatively time consuming operations

Let's assume we have a base class A that has a method:

class A {

  // methods ...

  double computeValue(double in1, double in2, double& out1) {


      double result;

      /* Perform some very time consuming computations with in1 and in2, 
         that leads to some intermediate values and the result */

      // Compute side product from those intermediate values that took so long
      // to obtain
      out1 = intermediate_value1 * intermediate_value2;

      return result;
  }

  // more methods and members

}

Now let's assume we have an interface class (I1) that is exclusively interested in returning A's "result" value:

// Interface class to A
class I1 {

  public:

    // ctor, dtor, ...

    double getResult(){double in1, in2, out1; return pA_->computeValue(in1, in2, out1);}

  private:

    std::shared_ptr<A> pA_;
}

On the other hand, I need a second Interface class that is only interested in out1, the side product of computeValue.

// Interface class to A
class I2 {

  public:

    double getResult(){double in1, in2, out1; pA_->computeValue(in1, in2, out1); return out1;}

  private:

    std::shared_ptr<A> pA_;
}

I could split computeValue in separate methods, the problem is that the values out1 and result should be derived from exactly the same inputs (and I do not want to repeat the execution of computeValue as it can be an "expensive" method in terms of load)

I was wondering if there is any design pattern that could help me implement this. Any help would be greatly appreciated.

Many thanks

Aucun commentaire:

Enregistrer un commentaire