2

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?

Btw. User only sees AnimationProperties

3
  • 1
    Pass the structure as an argument to the constructor? Commented Apr 3, 2018 at 16:38
  • Possible duplicate: Create objects by passing different parameter in c++? Commented Apr 3, 2018 at 18:11
  • Can you get together with your classmate, Ahmed Shahin and with your instructor. You instructor needs to be informed of topics that were not communicated well. Commented Apr 3, 2018 at 18:15

1 Answer 1

2

You need Factory Design Pattern - or even Abstract Factory.

Keeping your naming scheme - it should look like following:

class BaseInterpolatorFactory
{
public:
    virtual ~BaseInterpolatorFactory() = default;
    virtual std::unique_ptr<BaseInterpolator> CreateObject() = 0;
};

E.g. implementation for AnticipateOvershootInterpolator:

class AnticipateOvershootInterpolatorFactory : public BaseInterpolatorFactory
{
public:
    AnticipateOvershootInterpolatorFactory (float tension, float extraTension);
    std::unique_ptr<BaseInterpolator> CreateObject() override
    {
         return std::make_unique<>(m_tension, m_extraTension);
    }
private:
    float m_tension;
    float m_extraTension;
};

So - in client code, instead of passing some combined struct/union/discriminator of parameters - just pass BaseInterpolatorFactory&

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.