dimanche 25 janvier 2015

Design pattern for creating multiple objects of the same type

I am writing a search parser.There is an input search query(String) that needs to be converted to individual condition object.


For example, /perons/?q=age>50&name:mike


This query would translate into two condition objects. 1.GreaterThanCondition(String field, int value) 2.LikeCondition(String field, String value)


I have 14 such condition objects that implement the Condition Interface.


At runtime, I parse the string condition out of query (like "age>50") and I need to design an algorithm that would efficiently convert this condition into appropriate object (like GreaterThanCondition) efficiently.


Here is one algo I am thinking : Create a ConditionFactory. This ConditionFactory will take this string("age>50") and iterate over the existing 14 factories in a particular order (One factory for each condition) to match if the regex defined in the factory(For example, regex for GreaterThanConditionFactory can be [a-zA-Z0-9]+>+[0-9]+ . Based on regex match, GreaterThanConditionFactory can identify field(age) and value(50) from the condition and create GreaterThanConditionObject.


With this approach, I need to create 15 factories and 14 condition object. I am not sure if creating that many factories is a good solution. Please suggest.


Aucun commentaire:

Enregistrer un commentaire