C++ Real-Time Strategy Pattern to process Different Data

189 views Asked by At

I a currently researching an efficient way to implement a strategy pattern in c++ that is efficient in terms of runtime (it is supposed to run in real-time) yet modular.

To be more specific, think about a system that will handle data from different sources. The abstract processing steps for the data are always the same (think template pattern). However the specific algorithms to be used may depend upon which data (i.e. the source it stems from) is processed. Thus I want to set the context that determines the strategy via an appropriate interface, but keep the actual processing interface fixed.
To complicate things further I need the modularity, i.e. I need to be able to switch between different setups of sources without rewriting the complete code. (High compile-time modularity.)

To give you more of an idea of what I am attempting, here some stripped-down sample-code that obviously won't work:

class GeneralDataContainer
{
    int some_general_data;
}

template<typename type>
class DataContainer : GeneralDataContainer
{
    type some_specific_data;
}

class SubmoduleInterface
{
    void setContext(SomeContextIdentifier & context){...}
    void processData(GeneralDataContainer & data){...}
}

Now the question is: how can I make it work?

The main directions I was looking into were standard OO polymorphism (which has the problem of table lookups), and CRTP for which I do not know how to access the specific data (some_specific_data) within the submodule and enclosed strategy algorithms.

It might well be that this is a really stupid question, and there is a straight-forward answer. But I was just not able to figure it out, so if someone with more c++ experience (which should basically be everyone) than I could point me in the right direction this would be great. Thanks in advance.

1

There are 1 answers

0
Maryan Pritsak On

From what I understand, main goal is to use different processing algorithms depending on which specific data you have.

It looks like strategy pattern gives is a solution for this problem, and it works in run-time. For each new source you have to just implement additional concrete class.

If I didn't get your question, please, add some more explanation or example of what do you want to do.