mardi 3 avril 2018

Create objects by passing different parameters

I have a class hierarchy of interpolators like this:

class BaseInterpolator
{
public:
    virtual ~BaseInterpolator();

    virtual float GetInterpolation(const float input) const = 0;
};

class AccelerateInterpolator : public BaseInterpolator
{
public:
    explicit AccelerateInterpolator(const float factor = 1.0f);
    virtual float GetInterpolation(const float input) const override;
private:
    float m_factor;
};

class AnticipateOvershootInterpolator : public BaseInterpolator
{
public:
    explicit AnticipateOvershootInterpolator(const float tension = 2.0f, const float extraTension = 1.5f);
    virtual float GetInterpolation(const float input) const override;
private:
    float m_tension;
    float m_extraTension;
};

I would like to be able to construct these objects with different parameters. However, there is different number of parameters for each possible derived type from BaseInterpolator, i.e AccelerateInterpolator has 1, AnticipateOvershootInterpolator has 2. I have also class abstracting properties which client passes to my library which creates Interpolators based of those properties:

struct AnimationProperties
{
    enum class Curve
    {
        Accelerate,
        AnticipateOvershoot,
    };

    Curve curve;
    uint32_t duration{ 0 };
};

I would like to add possibility to user to pass different params via AnimationProperties, so I can create parametrized Interpolator using those params. Until now I've used unparametrized interpolators. I'm not sure how I should approach this. Should I use visitor somehow, or what is the correct approach?

Aucun commentaire:

Enregistrer un commentaire