gpt4 book ai didi

java - 可以在下面的代码中使用多态性代替 if/else

转载 作者:行者123 更新时间:2023-12-04 20:41:34 24 4
gpt4 key购买 nike

我应该在条件语句上使用多态性,但我可以在下面的情况下使用它吗?

下面的代码根据 jsonObject 的内容返回一个 Screen 对象。

public static Screen getNextScreen(){

JSONObject jsonObject = RatingsUtils.getCurrentJsonObjectFromServer();

if(isNextProgramScreen(jsonObject)) {
ParentRatingsObject parentRatingsObject = JsonBusinessObjectFactory.createParentRatingsObject(jsonObject);
return new NextProgramScreen(parentRatingsObject);
}
else if(isTimerScreen(jsonObject)) {
ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
return new TimerScreen(childWithParentRatingsObject);
}
else if(isNextContestantPreJudgeScreen(jsonObject)) {
ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
return new NextContestantPreJudgingScreen(childWithParentRatingsObject);
}
else if(isNextContestantJudgeScreen(jsonObject)) {
ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
return new TimerScreen(childWithParentRatingsObject);
}
else {
return null;
}
}

最佳答案

当然。我自己更经常地采用多态方法,我真的很喜欢它。由于 Java 语言,这看起来有点臃肿。我们真的需要 lambda 表达式来正确地做到这一点!

private static List<ScreenProvider> screenProviders = screenProviders();

public static Screen getNextScreen(JSONObject jsonObject) {
for (ScreenProvider screenProvider : screenProviders) {
if (screenProvider.supports(jsonObject)) {
return screenProvider.getScreen(jsonObject);
}
}
return null;
}

interface ScreenProvider {
boolean supports(JSONObject jsonObject);
Screen getScreen(JSONObject jsonObject);
}

private static List<ScreenProvider> screenProviders() {
return Arrays.asList(
new ScreenProvider() {
public boolean supports(JSONObject jsonObject) {
return isNextProgramScreen(jsonObject);
}

public Screen getScreen(JSONObject jsonObject) {
ParentRatingsObject parentRatingsObject = JsonBusinessObjectFactory.createParentRatingsObject(jsonObject);
return new NextProgramScreen(parentRatingsObject);
}
},
new ScreenProvider() {
public boolean supports(JSONObject jsonObject) {
return isTimerScreen(jsonObject);
}

public Screen getScreen(JSONObject jsonObject) {
ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
return new TimerScreen(childWithParentRatingsObject);
}
},
new ScreenProvider() {
public boolean supports(JSONObject jsonObject) {
return isNextContestantPreJudgeScreen(jsonObject);
}

public Screen getScreen(JSONObject jsonObject) {
ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
return new NextContestantPreJudgingScreen(childWithParentRatingsObject);
}
},
new ScreenProvider() {
public boolean supports(JSONObject jsonObject) {
return isNextContestantJudgeScreen(jsonObject);
}

public Screen getScreen(JSONObject jsonObject) {
ChildWithParentRatingsObject childWithParentRatingsObject = JsonBusinessObjectFactory.createChildWithParentRatingsObject(jsonObject);
return new TimerScreen(childWithParentRatingsObject);
}
}
);
}

这种方法的一个巨大好处是 getNextScreen() 方法的简单性。考虑一下,可能可以使 screenProviders() 方法更紧凑——也许可以通过添加一个实现 ScreenProvider 的抽象类来完成一些工作。

关于java - 可以在下面的代码中使用多态性代替 if/else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com