I have a system in which I will perform queries to a database. I want Query1, Query2 and Query3 to use the same methods in their way to the data source layer.
To do that I would like to make something similar to Strategy or Template Patterns, where I have a part of the code which is a default behavior for all queries (getting to the database layer, connection, executing the query and so on) and a variable part (building the query itself).
I think this idea is good and feasible. I would receive a parameter from the client specifying the query to be built, build it and run. The only question is: given this parameter, how can I decide which method to call to build the query? This is my current solution: (is this solution a pattern?)
public Query QueryFactory(string queryIdentifier){
if(queryIdentifier == "query1")
return BuildQuery1();
else if(queryIdentifier == "query2")
return BuildQuery2();
}
It works, but I don’t think this is “pretty enough”, I would like something nicer. It would be perfect if it didn’t require the developer to add a new if statement when he created a new query.
Is there any way to improve this if/else chain? And to make this decision automatic?
Edit:
The problem I want to solve is: I have queries that are going to be called against a database. I want to have the same flow so I don’t have duplicated code for the queries that do not have any business logic applied to it (ex: get all users, get all students, and so on)
5
As Steve Evers states, using an enumeration as a key and either a Class to instantiate (or an instance of Query pre-built) to return. If you don’t like the if/else pattern, I know Java has Map implementations, of which the EnumMap would be a good fit (where it optimizes the utilization of Enumerations as the key values in the map.
Then your code looks something like
public Query QueryFactory(QueryType type){
return localMap.get(type);
}
With the assumption that you define an enumeration named QueryType, and your localMap contains the appropriate Query instances for each type.
If you didn’t want to pre-instantiate the queries, the code would use the map to store the Class, which would allow the object to be instantiated then and there
public Query QueryFactory(QueryType type){
Class queryClass = localMap.get(type);
return (Query) queryClass.newInstance();
}
Again, this is all using Java as an example. Just realized you didn’t mention which language. But it can be cleaned up the way it sounds like you would prefer.
1