I have two classes, one does all the work of getting data from a Database(already implemented) and the other from a File(implementing right now).
The thing here is that I want to be able to switch the streaming(Database or File). The process that check whether is Database or File is needed only once.
So, for each method in a class I don’t wanna check everytime what should be use when this method is called.
for now, I do as follows:
if(IsDataBaseStream())
Database::execQuery("SELECT * from table");
else //is FileStream
File::GetAllFrom("Table");
This is ugly. I’m refusing to do this.
I thought about callbacks, but that doesnt work between different classes.
Is there a way of not wasting processor work like that?
Thanks in advance
You should use the Strategy Design Pattern. Give both classes a common base with an abstract method taking the data, make a pointer to that base class, and then assign the pointer a file-based or a db-based implementation after a single check.