重构包含无参数方法的 switch 语句 [英] Refactoring switch statement containing methods with no parameters
问题描述
我目前正在重构我的代码并被阻止.我有一个 switch
语句,我只知道可以重构,我只是缺乏这样做的经验:( 处理开关的大多数模式和方法侧重于管理不同方法的参数,但是情况并非如此.有什么想法我可以做些什么吗?非常感谢任何提示.
I'm currently refactoring my code and got blocked. I have a switch
statement that I just know that can be refactored, I just have lack of experience to do so :( Most of patterns and ways of dealing with switches focus of managing parameters which differs methods, but it's not the case here. Any ideas what can I do about it? Would really appreciate any hints.
public void manageMenuAction(MenuItem item, GiftListPresenterViewConnector activityConnector) {
switch (item.getItemId()) {
case R.id.nav_your_account:
activityConnector.goToEditAccount();
break;
case R.id.help:
activityConnector.goToHelpScreen();
break;
case R.id.nav_about:
createInfoDialog();
break;
case R.id.nav_exit:
activityConnector.exitApp();
break;
case R.id.nav_logout:
clearLocalData();
activityConnector.logout();
break;
}
}
我正在为 Android 编写代码,因此使用 java 1.7.GiftListPresenterViewConnector
是一个接口.
I'm coding for Android, so working with java 1.7. GiftListPresenterViewConnector
is an interface.
推荐答案
考虑使用命令"设计模式 https://en.wikipedia.org/wiki/Command_pattern
Consider using 'command' design pattern https://en.wikipedia.org/wiki/Command_pattern
这篇关于重构包含无参数方法的 switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!