我想知道如何最好(在效率和可读性方面)实现涉及基类和2个接口类(I1和I2)的设计模式,这些接口类访问执行一些相对耗时的操作的类A
假设我们有一个基类A,它有一个方法:
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
}
现在让我们假设我们有一个接口类(I1),专门用于返回A"结果"值:
// 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_;
}
另一方面,我需要第二个仅对out1
的副产品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_;
}
我可以在单独的方法中拆分computeValue
,问题是值out1
和result
应该来自完全相同的输入(我不想重复执行computeValue
,因为就负载而言,它可能是一种“昂贵的”方法
我想知道是否有任何设计模式可以帮助我实现这一点。任何帮助将不胜感激。
非常感谢