Creational Design Pattern for this Problem?
I have the following problem illustrated by the pseudo code below (might not make a whole lot of sense):
class Form {
boolean loggedOn = false;
String id = null;
...get/set shared methods
}
class SearchFormA extends Form{
String name = null;
String email = null;
...get/set methods
}
class SearchFormB extends Form{
String age = null;
String gender = null;
...get/set methods
}
class Search {
public Search(HttpServletRequest request){
String searchMode = (String) request.getSearchMode();
if("0".equals(searchMode)){
SearchFormA formA = new SearchFormA();
formA.setName((String)request.getParameter("name"));
formA.setId((String)request.getParameter("id"));
...populate form
request.getSession().setAttribute("formA",formA);
}
if("1".equals(searchMode)){
SearchFormB formB = new SearchFormB();
formB.setAge((String)request.getParameter("age"));
formB.setId((String)request.getParameter("id"));
...populate form
request.getSession().setAttribute("formB",formB);
}
...rest of code
}
}
What i've done is using reflection, but is there any other way to do it at compile time? I've also tried Factory method, but the classes SearchFormA and SearchFormB does not have much in common.
EDIT: ok basically, in this case i have 4-5 searchModes, each searchMode has a different form. Between these forms they share certain similar fields. In fu开发者_如何转开发ture i might have to add 10 searchModes and it's going to be repeating alot of populating of the same fields.
Unfortunately your code is too much "pseudo." For example it is not clear where do you take searchMode
from. Also initialization of your specific forms is hard coded. I believe that in real life all these data is taken from class Data
.
So, I assume that class Data
contains name, age, id. Now the question is how does Data class contain this data. Special fields? Generic hash table?
In any case I think that the fact that you have some kind of "general" code (Search class) and have to create special forms is a bad pattern. You should use some MVC framework that populates your specific form automatically. Then run your search logic using the form data. if you have shared logic for both modes implement it either in abstract class or in utility class.
I don't see much wrong with your current approach. You could use an enum to avoid the string comparisons as in the following example:
public enum SearchMode {
A {
@Override
public Form createForm(Data data) {
SearchFormA form = new SearchFormA();
form.setName(...);
populateCommon(form, data);
return form;
}
},
B {
@Override
public Form createForm(Data data) {
SearchFormB form = new SearchFormB();
form.setAge(...);
populateCommon(form, data);
return form;
}
};
public abstract Form createForm(Data data);
public void populateCommon(Form form, Data data) {
// Set common properties
form.setId(...);
}
public static Form createForm(String searchMode, Data data) {
return SearchMode.valueOf(searchMode).createForm(data);
}
}
What you're obviously trying to avoid is that in the Search method you'd have to keep appending to the if structure (spaghetti code).
I feel like the situation you're describing isn't all that different from the one I wrote this answer for. That would mean that Strategy and Template (and others) could come in handy here.
Cheers, Wim
精彩评论