Thanks in advance for reading. This is a picky design question in that I have an adequate solution, but question if there isn’t a better way via templates, which I’m not very experienced with.
I have a series of types of data tables (derived from abstract class DataTable) which I need to store. I wrote a “DataTableIndex” abstract class which stores a vector DataTable*’s and which handles the general work common to all DataTableIndexes — doing lookups, implementing a Proxy pattern so tables are only loaded if needed, error checking, etc.
I then subclass it for each table type, the only reason I need to do so is because each table type has a specific function to be called to load it.
I’d like to avoid this subclassing of DataTableIndex somehow via templates if possible, because there are lots of subclasses of DataTable.
class DataTableIndex
{
// various functions to implement lookup, Proxy and error checking
// functionality common to all DataTableIndexes.
// This code needs access to _lookupTable and _theTables
DataTable* getTable( int tableNum );
private:
// These functions call the appropriate user interface function for loading
// a table of the subclass' type.
// They are called by more general non-virtual public functions
virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable ) = 0;
virtual DataTable* loadTable( int tableNum ) = 0;
vector<LookupEntry*> _lookupTable;
vector<DataTable*> _theTables;
UserInterface* UI;
};
This class has then very simple subclasses, which basically point to functions in the User Interface class that actually open and parse the file of the data table.
class TableTypeA_Index : public DataTableIndex
{
virtual bool loadIndex( vector<LookupEntry*>& returnLookupTable )
{
return UI->loadTableAIndex( _lookupTable );
}
virtual DataTable* loadTable( int tableNum )
{
return UI->loadTableTypeA( _lookupTable[ tableNum ] );
}
};
This works adequately. But I feel like I should be able to pass “loadTableTypeA” for example to DataTableIndex via a template parameter, so I don’t have to subclass it.
Another reason why I would like to use templates is that I don’t want to have to cast DataTable* all of the time to the actual table type. Even though I know at compile time what type of table it should be, I feel like I should be using dynamic_cast<> to error check, but I don’t want the caller of getTable() to have to do this every time (it is called often).
My ideal solution would:
1) generalize the DataTableIndex class to a template, with template parameters replacing LookupEntry* and DataTable* in _lookupTable and _theTables. This would eliminate casting.
2) map the appropriate UI functions to load the right table type, without subclassing.
So basically I’d like to the use of this class to look like this (somehow)
DataTableIndex< LookupEntryTypeAMatlab,
TableTypeA,
loadTableAIndex(),
loadTableTypeA() > theTypeAIndex;
I gave some thought to policy classes, but my impression of that approach was that, in this case, I’d just be moving the subclassing to something else.
In general, this could be done by using the strategy pattern. This can be implemented using simple composition, so there are no templates needed. However, you will still have to define a special strategy for each of the different table types. This will require subclassing:
This class will have to be subclasses for each table-type.
DataTableIndexwould then receive an instance ofLoadTableStrategyin the constructor, and use that to load the data, instead of the private virtual functions.You can of course pass the strategy-type as template parameter, but be aware, that this as drawbacks. Two
DataTableIndexinstantiated with different template parameters will be different types for the compiler. You will not be able to define a function that can handle both, unless you create specific overloads, or make the function a function template itself.