I have some business requirements which sound something like this:
Find the data in the HR Infotypes that meet the criteria (… listed criteria…),
For every found data (record) send e-mail to given recipients with the desired text.
For now I’ve got few sets of criteria to find specific data, but I know that in the future there will be another sets.
I’ve decided to build the solution where every set of criteria and settings for messages would be a single class – a SearchAgent or something:
public abstract AbstractSearchAgent {
public run(){...}
public get_results() {...}
public send_info() {...}
}
first question: is the concept correct?
I also wanted to design application where the main app will be implemented once. when i get the new criteria of searching the data, I only add another child class and the rest will be the same:
public class myApp {
private AbstractSearchAgent searchagent;
public static void main() {
//for each existing implementation of abstractSearchAgent
//get instance into searchagent
//and execute its methods:
// e.g.
searchagent->run();
searchagent->send_info();
//
// if i wanted to do something else with the result
result = searchagent->get_results();
}
second question: how to do it? 🙂
I thought of using the abstract factory or builder pattern but I don’t know which pattern will be proper for this situation and how to build it?
Could anyone help me to decide which way is better?
Basically this sounds like a Strategy, which is perfectly usable for this kind of job. You are right that you can use a Factory to create the desired search agent.
To improve long term maintainability of your solution, you may consider implementing your search agents as Composites of basic criteria, thus making individual criteria more reusable. This is best when the search criteria can be complex logical expressions, and especially if these include NOT / OR operators, e.g. “name does not start with ‘M’ and (age is less than 30 or age is greater than 50)”. The implementation of this as a Composite hierarchy could look something like this:
AndCriteriaNotCriteriaNameCriteriaOrCriteriaAgeCriteriaAgeCriteriaAn alternative to this would be Decorator. This is adequate if the search criteria can be performed in a linear fashion, such as “name starts with ‘M’ and age is less than 30 and gender is female”. This could be implemented as a sequence of Decorators like this: