-
-
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"); | |
} |
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();
}
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