【问题标题】:Is there an elegant way to handle run-time dependent object composition without using dynamic_cast?有没有一种优雅的方法来处理运行时相关的对象组合而不使用 dynamic_cast?
【发布时间】:2021-07-16 00:22:02
【问题描述】:

上下文

我正在尝试设计一个模块化且易于扩展和测试的建模框架。 (我正在尝试遵循开放/封闭、依赖注入和组合优于继承的原则)。

我有一个模型对象的层次结构,其中包含方法,有些是所有人通用的,有些是特定于某些模型的。我还有一系列估计方法,适用于模型的各种子集。

每个估算器类都需要一个模型对象,该对象使用构造函数传递。

虽然在编译时构造对象没有问题,但我正在努力想出一种在运行时执行此操作的方法,而无需求助于估算器中父模型接口的 dynamic_casting。 虽然它有效,但如果将模型和估计器的无效组合放在一起,则可能是不安全的。我还读到 dynamic_casting 通常是代码设计不佳的标志。

问题

当对象组合细节只在运行时知道时,是否有任何标准设计模式或技术可以避免使用dynamic_cast?

或者这是否表明对象层次结构需要完全重新设计以避免对象之间的这种条件依赖?

代码示例

#include <iostream>
#include <unordered_map>

class i_model {
public:
    virtual double getparameter_a() = 0;
 };

class i_model_A : public i_model {
public:
    virtual double getparameter_b() = 0;
};

class model_A : public i_model_A {
public:
    double getparameter_a() { return 4.0; };
    double getparameter_b() { return 4.0; };
};

class i_model_B : public i_model {
public:
    virtual double getparameter_c() = 0;
};

class model_B : public i_model_B {
public:
    double getparameter_a() { return 5.0; };
    double getparameter_c() { return 2.0; };
};

class i_estimator {
public:
    virtual double getestimate() = 0;
};

class estimator_1 : public i_estimator {
public:
    estimator_1(i_model* in) : model(dynamic_cast<i_model_A*>(in)) {};
    double getestimate() { return model->getparameter_a() + model->getparameter_b(); };
private:
    i_model_A* model;
};

class estimator_2 : public i_estimator {
public:
    estimator_2(i_model* in) : model(dynamic_cast<i_model_B*>(in)) {};
    double getestimate() { return model->getparameter_a() + model->getparameter_c(); };
private:
    i_model_B* model;
};

int main()
{
    int a;
    int b;
    i_model* model = nullptr;
    i_estimator* estimator = nullptr;

    std::cout << "Select model: ";
    std::cin >> a;

    std::cout << "\nSelect estimator: ";
    std::cin >> b;
    
    switch(a) {
    case 1: model = new model_A(); break;
    case 2: model = new model_B(); break;
    }

    switch (b) {
    case 1: estimator = new estimator_1(model); break;
    case 2: estimator = new estimator_2(model); break;
    }
    std::cout << "\nEstimator value = " << estimator->getestimate();
} 

【问题讨论】:

  • estimator_1(i_model* in) : model(dynamic_cast&lt;i_model_A*&gt;(in)) soooo 如果你需要i_model_A,为什么还要选择i_model? Aaaand getparameter_b 和 _c 应该转到 i_model。
  • 模型类型只在运行时知道;
  • 如果a == 1 和b == 2,您发布的代码将会中断,反之亦然。
  • 有一个i_model 接口,具有3 个功能。不适用时从派生类返回 0。然后每个估算器调用它需要的方法。
  • @Paul Sanders 是的,这就是我想看看是否有更好的方法来做到这一点的原因之一。我可以检查用户输入以查看组合是否有效(也许将有效组合存储在寄存器中),但不确定这是最好的方法。

标签: c++ composition dynamic-cast


【解决方案1】:

据我了解,某些估算器仅适用于某些类型的模型,但您允许用户在运行时选择他们想要的任何估算器和模型,然后您尝试将它们粉碎在一起,无论它们是否适合。

如果这是您想要的行为,那么无论用户如何选择不兼容的模型和估算器,因此无论您编写哪种代码,都可能在运行时失败。在这种情况下,运行时失败是正确的行为,因此无法使用类型系统(或任何其他方式)消除这种可能性。

编辑:上面的内容有点不正确,因为dynamic_cast 在转换失败时在指针上返回nullptr,然后你会得到一个空指针取消引用,即 UB。如果您使用引用而不是指针,那将是正确的,在这种情况下,失败的dynamic_cast 会引发异常。或者,如果您想使用指针,请对结果进行显式 null 检查并抛出。

【讨论】:

  • 是的,我走这条路的原因是它在估计算法和模型数据之间提供了很好的分离,使得重用代码或扩展两者变得容易。回复:运行时失败,我可以限制用户输入以防止不兼容的组合(即某种结构将输入组合映射到对象创建)。我只是想了解这种方法是否会给我带来更多麻烦。 (共识似乎是这不是一个很好的方法,很高兴知道)。我只需要弄清楚如何重组它......
  • 如果这是您想要的方法,我认为它没有任何问题。例如,如果您正在编写一个编译器,那么任何文本文件都被接受为输入,然后如果您发现用户编写了类似std::exp("Hello"); 的内容,您只需给出一条消息,指示用户出现语法错误并退出。这不是编译器以这种方式工作的缺陷,并且没有设计模式可用于编写不会遇到语法错误的编译器。问题与代码的抽象级别不同。
  • 换句话说,如果程序应该是这样工作的,那么按照你的方式编写代码并没有错。
【解决方案2】:

虽然它有效,但如果将模型和估算器的无效组合放在一起,则可能是不安全的。

您编写的代码非常不安全,如果用户选择了无效的组合,它将具有未定义的行为。

您应该确保在这种情况下有 一些 指定的行为。例如

class estimator_1 : public i_estimator {
public:
    estimator_1(i_model_A& in) : model(in) {};
    double getestimate() { return model.getparameter_a() + model.getparameter_b(); };
private:
    i_model_A& model;
};

class estimator_2 : public i_estimator {
public:
    estimator_2(i_model_B& in) : model(in) {};
    double getestimate() { return model.getparameter_a() + model.getparameter_c(); };
private:
    i_model_B& model;
};

i_model* make_model(int choice) {
    switch(choice) {
    case 1: return new model_A();
    case 2: return new model_B();
    }
    return nullptr;
}

i_estimator* make_estimator(int choice, i_model * model) {
    switch (choice) {
    case 1: if (i_model_A* model_a = dynamic_cast<i_model_A*>(model)) return new estimator_1(*model_a); break;
    case 2: if (i_model_B* model_b = dynamic_cast<i_model_B*>(model)) return new estimator_2(*model_b); break;
    }
    return nullptr;
}

int main()
{
    int a;
    int b;

    std::cout << "Select model: ";
    std::cin >> a;

    std::cout << "\nSelect estimator: ";
    std::cin >> b;
    
    auto model = make_model(a);

    if (auto estimator = make_estimator(b, model)) {
        std::cout << "\nEstimator value = " << estimator->getestimate();
    } /* else? */
} 

注意您还应该使用 std::unique_ptr 而不是 * 来拥有指针。

【讨论】:

  • 谢谢。我喜欢它自动捕获未定义案例的事实。
猜你喜欢
  • 2010-10-24
  • 1970-01-01
  • 1970-01-01
  • 2019-01-12
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
相关资源
最近更新 更多