Timer class, mixin vs. subclass.

I

ish

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_Mixin &mixin);
};

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

Thanks.
 
B

Bart

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_Mixin &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.
 
N

Nate Barney

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,007
Messages
2,570,266
Members
46,863
Latest member
montyonthebonty

Latest Threads

Top