C++通过构造函数注入"策略"
C++ injecting 'strategies' through constructor
我正在开发一些复杂的计算,需要确定"步骤"(策略模式类型实现),我不确定将步骤子计算类注入主类的最佳方法。
我研究过基于策略的设计,但读到策略设计是针对"编译时多态性"而不是运行时的。另外,我不确定如何使用模板,因为一些子计算类需要构造函数参数。
我已经开始为每个步骤实现虚拟的"接口"类,并在构造函数中注入每个步骤作为unique_ptr,但不确定这是否正确的"现代c++"方式。
我最初开始在主类中实现所有功能,但发现这使得独立地对每个步骤进行单元测试变得困难(如果不是不可能的话)。
结构类似如下:
class CalculationStepA
{
public:
// default constructor
StepAResult performStep(const input& requiredInput);
};
class CalculationStepBType1
{
public:
// default constructor
StepBResult performStepB(const stepBInput& requiredInput);
};
class CalculationStepBType2
{
public:
CalculationStepBType2(const inputIOnlyNeedForType2& parameters)
{
// initialize class members from input
// need for this calculation type
}
StepBResult performStepB(const stepBInput& requiredInput);
};
class CalculationStepCType1
{
public:
CalculationStepBType2(const inputIOnlyNeedForType1& parameters)
{
// initialize class members from input
// need for this calculation type
}
StepCResult performStepC(const stepCInput& requiredInput);
};
class CalculationStepCType2
{
public:
CalculationStepBType2(const inputIOnlyNeedForType2& parameters)
{
// initialize class members from input
// need for this calculation type
}
StepCResult performStepB(const stepCInput& requiredInput);
};
class ClassThatUsesAllTheCalculations
{
public:
ClassThatUsesAllTheCalculations(/* take required parameters that determine which step types I need */)
{}
// possible constructor?
ClassThatUsesAllTheCalculations(
std::unique_ptr<IStepACalculationStrategy> stepA,
std::unique_ptr<IStepBCalculationStrategy> stepB,
std::unique_ptr<IStepCCalculationStrategy> stepC)
{
}
FinalResult executeCalculation(const finalInputRequiredHere& input)
{
auto stepAresult = stepACalculator(somethingFromInput);
// logic to use stepA and determine if we should continue
auto stepBresult = stepBCalculator(somethingFromStepAResult);
// again, logic to use stepB and determine if we should continue
auto stepCresult = stepCCalculator(somethingFromStepBResult);
// assemble final result
return theFinalResult
}
// other method needed to setup calculation
private:
TypeForStepACalculation stepACalculator;
TypeForStepBCalculation stepBCalculator;
TypeForStepCCalculation stepCCalculator;
};
简单继承怎么样?
struct StepA{
virtual StepAResult perform(StepAParams)=0;
};
struct someStepAImpl : public StepA{
virtual StepAResult perform(StepAParams params) override {
//actual implementation
}
};
计算类是否使用引用(必须在构造时设置),std::reference_wrapper
(不是null,但可以稍后更改)或某种(智能)指针(可能是nullptr
,不要忘记检查,但在管理生命周期时最容易)取决于您计划如何使用它以及您希望如何管理对象的生命周期。使用unique_ptr
的一个例子是:
class Calculator
{
public:
Calculator(
std::unique_ptr<StepA> stepA,
std::unique_ptr<StepB> stepB,
std::unique_ptr<StepC> stepC
)
:m_stepA(std::move(stepA)),m_stepB(std::move(stepB)),m_stepC(std::move(stepC))
{}
FinalResult executeCalculation(const finalInputRequiredHere& input)
{
//logic
auto stepAresult = stepA->perform(StepAParams);
//logic
auto stepBresult = stepB->perform(StepBParams);
//logic
auto stepCresult = stepC->perform(StepAParams);
//logic
return FinalResult();
}
private:
std::unique_ptr<StepA> m_stepA=nullptr;
std::unique_ptr<StepB> m_stepB=nullptr;
std::unique_ptr<StepC> m_stepC=nullptr;
};
void somewhereElse(){
std::unique_ptr<StepA> stepa(new someStepAImpl());
std::unique_ptr<StepB> stepa(new someStepBImpl());
std::unique_ptr<StepC> stepa(new someStepCImpl());
Calculator calc(
std::move(stepa),
std::move(stepb),
std::move(stepc)
);
calc.executeCalculation(...);
}
相关文章:
- C++17中的并行执行策略
- 运行时执行策略不同
- 编译器上的策略数据结构不起作用
- 无法使用加载库卸载注入的 dll (C++)
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 将 dll 文件注入 UWP
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 将具有固定签名的自定义函数名称注入 CRTP
- 没有执行策略的 std::transform_reduce 是可移植的吗?
- 发生注入类名时会发生什么情况?(C++)
- C++ 运算符修改/元编程策略,用于不那么冗长的语法
- 作为指针注入类后重新创建堆栈对象不好吗?
- 如何使用可视化代码和平台IO将环境变量注入CPP文件?
- 限定的依赖名称查找,其中包含基类的注入类名
- 如何在窗口中使用注入的 dll 中的参数调用函数
- PostgresSQL - SQL Ready 语句与字符串转义,防止 SQL 注入攻击
- 使用 Qt5 SQL 进行异步数据库访问的策略
- Windows 将标准的 C 文件结构注入到正在运行的进程中
- C++基于策略的设计:继承与组合
- C++通过构造函数注入"策略"