Created
June 7, 2013 14:43
-
-
Save meza/5729760 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
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();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.