I have been confronted with a C++ design problem to choose between virtual function, template and branch. The three implementations are listed as follows. I eventually chose the the second implementation, which looks tricky but with best performance for a low latency design.
virtual function implementation:
class Channel : public BaseChannel
{
void packet(...) { for (...) message(...); }
virtual void message(...)=0;
};
class ChannelA : public Channel
{
struct Header {...}
void message(...) { ... }
}
class ChannelB : public Channel
{
struct Header {...}
void message(...) { ... }
}
template implementation:
template <typename TImpl>
class Channel : public BaseChannel
{
void packet(...) { for (...) message(...); }
void message(...);
};
class ChannelA : public Channel<ChannelA>
{
struct Header {...}
void message(...) { ... }
}
class ChannelB : public Channel<ChannelB>
{
struct Header {...}
void message(...) { ... }
}
template <typename TImpl>
inline void Channel<TImpl>::message(...) { static_cast<TImpl*>(this)->message(); }
branch implementation:
class Channel : public BaseChannel
{
void packet(...) { for (...) message(...); }
struct HeaderA {...}
struct HeaderB {...}
void message(...)
{
if (isHeaderA(...)) messageA(...);
else if (isHeaderB(...)) messageB(...);
}
void messageA(...) { ... }
void messageB(...) { ... }
};
What dose your expected?
The Polymorphism – Easy to extends the objects, and support varies object, and you will not meet the parabolic-if statement, but you need to do the more coding and design, and it is hard to understand for one who doesn’t strong with the OO concept or the Design Pattern concept.
The Template – This seem likely the branch implementation in (3), because you need to store an object individually.
The Branch – Basic and easy to code, but this will be a pain when you extends those objects, for example, how the code should be look like when you have OjbectA-ObjcetJ. Well, it is the parabolic-id 🙂
Both (1) and (3) need the runtime performance overhead, and (1) have the better performance than(3) ,as I have done the experiments. (2) will make your program bigger and it is like the (3).
In conclusion, you can choose (1) if you have varies of your derived classes or it may increase in the future, or you want your performance, but you can choose (3) if you doesn’t have much object to derived, or you don’t need a good performance ,or you don’t wan’t to do more coding.