Timer class, mixin vs. subclass.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ish@unx.ca

    #1

    Timer class, mixin vs. subclass.

    I think this is more of a style question than anything else... I'm
    doing a C++ wrapper around a C event library I have and one of the
    items is a timer class, I'm also using this task to learn C++.

    Is it cleaner to have users subclass my Timer class and implement the
    on_timeout() method? Or should the user use a mixin and provide the
    mixin to my Timer class?

    The subclass method kinda looks like this..

    class Timer
    {
    public:
    ...
    virtual void on_timeout() {};
    };

    or with a mixin..

    class Timer_Mixin
    {
    public:
    virtual ~Timer_Mixin() {};
    virtual void on_timeout() = 0;
    };

    class Timer
    {
    public:
    Timer(Timer_Mix in &mixin);
    };

    I have both implementations working fine, so its more of a matter of
    which style should be preferred.

    Thanks.
  • Bart

    #2
    Re: Timer class, mixin vs. subclass.

    ish@unx.ca wrote:
    I think this is more of a style question than anything else... I'm
    doing a C++ wrapper around a C event library I have and one of the
    items is a timer class, I'm also using this task to learn C++.
    >
    Is it cleaner to have users subclass my Timer class and implement the
    on_timeout() method? Or should the user use a mixin and provide the
    mixin to my Timer class?
    >
    The subclass method kinda looks like this..
    >
    class Timer
    {
    public:
    ...
    virtual void on_timeout() {};
    };
    >
    or with a mixin..
    >
    class Timer_Mixin
    {
    public:
    virtual ~Timer_Mixin() {};
    virtual void on_timeout() = 0;
    };
    >
    class Timer
    {
    public:
    Timer(Timer_Mix in &mixin);
    };
    >
    I have both implementations working fine, so its more of a matter of
    which style should be preferred.
    I tend to look at things like this from a pragmatic point of view. If I
    can't really see the advantage of using the separate class, I would
    follow Occam's razor and use the simpler way. But it depends really.

    It's good to have a few style guidelines when designing, but don't fall
    in the so-called analysis-paralysis. It's better to learn from your
    mistakes than to try to get everything perfect the first time.

    Regards,
    Bart.

    Comment

    • Nate Barney

      #3
      Re: Timer class, mixin vs. subclass.

      ish@unx.ca wrote:
      I think this is more of a style question than anything else... I'm
      doing a C++ wrapper around a C event library I have and one of the
      items is a timer class, I'm also using this task to learn C++.
      >
      Is it cleaner to have users subclass my Timer class and implement the
      on_timeout() method? Or should the user use a mixin and provide the
      mixin to my Timer class?
      Also, look into the way Java AWT/Swing handle events. I, personally,
      find that method particularly elegant.

      Nate

      Comment

      Working...