微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在装饰器设计模式C ++中返回Decorator对象

如何解决在装饰器设计模式C ++中返回Decorator对象

我正在为我的大学做练习,我必须使用Decorator设计模式创建一个Vector3D类。

enter image description here

[编辑1:这里要注意的一件事,我的教授告诉我们,如果我理解正确,则将Decorator和ConcreteDecorator合并为一个Vector3D

这就是我随附的。

class Vector3D : public IVector
{
    std::shared_ptr<IVector> vector2D;
    double z;
public:
    Vector3D(IVector* vector2D,double z);
    double abs() const override;
    std::vector<double> getComponents() const override;
    std::vector<double> getAngles() const override;
    double cdot(const IVector& vector) const override;
};

然后,我必须实现一个函数,该函数返回两个向量的叉积。我知道如何计算坐标,但是我不知道如何构造对象,因为我的构造函数接受实现IVector的类的任何对象,而IVector本身没有构造函数

Vector3D Vector3D::crossproduct(const IVector &vector) const
{
    const auto v = vector2D->getComponents();
    const auto coords = vector.getComponents();
    if(coords.size() != 3)
        throw std::runtime_error("Given vector has wrong number of components");

    return ???
}

我应该只使用Vector2D的构造函数创建该对象,还是有点破坏设计模式?我有什么混淆的地方吗?

[edit 2:我想知道是否有可能获得std::shared_ptr<IVector> vector2D字段所指向的对象的类型并以某种方式使用其构造函数?]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。