-
-
Save meza/5729760 to your computer and use it in GitHub Desktop.
private String getCreateContentForm(String contentType) { | |
if ("article".equals(contentType)) { | |
return createArticleForm(); | |
} | |
if ("audio".equals(contentType)) { | |
return createAudioForm(); | |
} | |
if ("cartoon".equals(contentType)) { | |
return createCartoonForm(); | |
} | |
if ("competition".equals(contentType)) { | |
return createCompetitionForm(); | |
} | |
throw new IllegalArgumentException("No such content type"); | |
} |
porting my twitter answer & expanding on it here for more comments
Forgive me for using python for examples, but I haven't coded in Java for years...
- If Java Maps were not as verbose, something like this could work nice, revealing intent - mapping a string to a method
def get_create_content_form(content_type):
return {
'article': create_article_form,
'audio': create_audio_form,
'cartoon': create_cartoon_form,
'competition': create_competition_form
}.get(content_type)() # raises KeyError on invalid content_type
- If it is in test code and could possibly take the reflection hit, then
def get_create_content_form(self, content_type): # assuming it's part of a class
factory_method_name = 'create_%s_form' % content_type
return getattr(self, factory_method_name)() # raises an AttributeError if not found
could work just as well - in the spirit of convention over configuration
Just 2c :)
private String getCreateContentForm(ContentType contentType) {
switch (contentType) {
case ARTICLE:
return createArticleForm();
case AUDIO:
return createAudioForm();
case CARTOON:
return createCartoonForm();
case COMPETITION:
return createCompetitionForm();
default:
throw new IllegalArgumentException("No such content type");
}
}
was the solution. I' still not feeling this to be the right solution, but it does work, passes the tests and the standard, so time should not be wasted on this any more.
Academic discussions however are welcomed :D
first, how about renaming getCreateContentForm() to contentFormFor()? Second, if this is Java, add a createForm() method to the enum that does the right thing.
hahh, never used an enum for such things before. Don't ask why though. Naming convention cultural change is a slow process, but on it! :)
Now put a 'createForm' method in the enum itself and get rid of the switch statement. Ah, Steve already said this. Good.
BTW, my rule for switch statement is this: You are allowed one switch for each set of cases. The cases must create polymorphic objects that eliminate all the other switch statements for those cases.
Thank you guys, I am very happy with this now. I've always used enums as dumb types, nothing more. I have learned a great thing today! What a way to end a week! :)
For anyone stumbling on this thread:
The following examples are modified for the sake of the gist, but the end solution is something like:
public enum ContentType {
ARTICLE("article"),
AUDIO("audio"),
CARTOON("cartoon"),
COMPETITION("competition");
private String label;
ContentTypeForm(String label) {
this.label = label;
}
public String toString() {
return label;
}
public String formPostData() {
switch (this) {
case ARTICLE:
return createArticleFormPostData();
case AUDIO:
return createAudioFormPostData();
case CARTOON:
return createCartoonFormPostData();
case COMPETITION:
return createCompetitionFormPostData();
default:
throw new IllegalArgumentException("No such content type");
}
}
}
As Cucumber can use ENUMS as parameters, the usage follows:
@When("^I have launched a new \"([^\"]*)\" page$")
public void iHaveLaunchedANewPage(ContentType contentType) {
String contentFormData = contentType.formPostData();
}
good point