java 重构 设计模式_java-设计模式以嵌套if或switch语句重构代码

我必须重构庞大的现有代码.在SO和其他网站上解决了许多类似的问题,但仍然感到困惑.如果有人可以提出一些建议或任何想法,将大有帮助.

有5个下拉菜单,我需要根据下拉菜单中的选定值更新视图.

首先下拉菜单有以下选项:

"TOP DOWN BUDGET"

"TEMPLATE BUDGET"

"ORIGINAL BUDGET"

"REVISED BUDGET"

第二个下拉菜单具有以下选项:

"Day"

"Week"

"Month"

"Quarter"

"Year"

第三个下拉列表具有以下选项:

"Details"

"Summary"

第四个下拉列表具有以下选项:

"Hours"

"Dollars"

第五下拉菜单具有以下选项:

"StartDate"

"EndDate"

现在,代码具有以下情形:

public List XYZ(...){//opening of some method XYZ....

List workPlanReportList=null;

switch(first_Drop_Down_Value){

case "TOP DOWN BUDGET":

List timeLine=getTimeLine("firstDropDownA", second_drop_down_val, third_drop_down_val, fourth_drop_down_val);

workPlanReportList=setWorkPlanByTimeLine(timeLine, "firstDropDownA", second_drop_down_val, third_drop_down_val, fourth_drop_down_val);

break;

case "TEMPLATE BUDGET":

List timeLine=getTimeLine("firstDropDownB", second_drop_down_val, third_drop_down_val, fourth_drop_down_val, fifth_dd_val);

workPlanReportList=setWorkPlanByTimeLine(timeLine, "firstDropDownA", second_drop_down_val, third_drop_down_val, fourth_drop_down_val);

break;

case "ORIGINAL BUDGET":

List timeLine=getTimeLine("firstDropDownC", second_drop_down_val, third_drop_down_val, fourth_drop_down_val, fifth_dd_val);

workPlanReportList=setWorkPlanByTimeLine(timeLine, "firstDropDownA", second_drop_down_val, third_drop_down_val, fourth_drop_down_val);

break;

case "REVISED BUDGET":

List timeLine=getTimeLine("firstDropDownD", second_drop_down_val, third_drop_down_val, fourth_drop_down_val, fifth_dd_val);

workPlanReportList=setWorkPlanByTimeLine(timeLine, "firstDropDownA", second_drop_down_val, third_drop_down_val, fourth_drop_down_val);

break;

}

return workPlanReportList;

}// Closing of some method XYZ....

private List getTimeLine(String first_Drop_Down_Value, String second_dd_val, third_dd_val, fourth_dd_val, String fifth_dd_val){

switch(second_Drop_Down_Value){

case "Day":

if(third_dd_val.equals("Details")){

if(fourth_dd_val.equals("Hours")){

if(fifth_dd_val.equals("startDate"){

//prepare query & call DB for fetching days timeline for hours details of TOP DOWN BUDGET filtered by start date...

}

else// means endDate{

//prepare query & call DB for fetching days timeline for hours details of TOP DOWN BUDGET filtered by end date...

}

}

else{//means Dollars

if(fifth_dd_val.equals("startDate"){

//prepare query & call DB for fetching days timeline for dollars details of TOP DOWN BUDGET filtered by start date...

}

else// means endDate{

//prepare query & call DB for fetching days timeline for dollars details of TOP DOWN BUDGET filtered by end date...

}

}

}

else// means summary...

{

if(fourth_dd_val.equals("Hours")){

if(fifth_dd_val.equals("startDate"){

//prepare query & call DB for fetching days timeline for 'hours' "summary" of TOP DOWN BUDGET filtered by start date...

}

else// means endDate{

//prepare query & call DB for fetching days timeline for hours summary of TOP DOWN BUDGET filtered by end date...

}

}

else{//means Dollars

if(fifth_dd_val.equals("startDate"){

//prepare query & call DB for fetching days timeline for dollars details of TOP DOWN BUDGET filtered by start date...

}

else// means endDate{

//prepare query & call DB for fetching days timeline for dollars details of TOP DOWN BUDGET filtered by end date...

}

}

}

break;

case "Week":

//....similar code as in case "Day" just here we need to fetch data week-wise

break;

case "Month":

//....similar code as in case "Day" just here we need to fetch data month-wise

break;

case "Quarter":

//....similar code as in case "Day" just here we need to fetch data quarter-wise

break;

case "Year":

//....similar code as in case "Day" just here we need to fetch data year-wise

break;

}

}

private List setWorkPlanByTimeLine(List timeLine, String "firstDropDownA", String second_drop_down_val, String third_drop_down_val, String fourth_drop_down_val){

WorkPlanReport wpr=new WorkPlanReport();

// Here I have real mess..., Iterating the timeLine list and have switch case and inside switch case multilevel nesting of if else to decide which setter we need to use to set the value.

for example:

If it is "TOP DOWN BUDGET" in first drop-down, "Day" in second drop-down, "Details" in third drop down, "Hours" in fourth drop-down & "StartDate" in fifth drop-down, I have to call follwing code:

wpr.setTDBDetailsHoursByStartDate(taskDetails.getTDBDetailsByHourStartDayView());

If it is "TOP DOWN BUDGET" in first drop-down, "Day" in second drop-down, "Details" in third drop down, "Hours" in fourth drop-down & "EndDate" in fifth drop-down, I have to call follwing code:

wpr.setTDBDetailsHoursByEndDate(taskDetails.getTDBDetailsByHourEndDayView());

}

这段代码超过1000行,我不愿使用一些合适的设计模式来对其进行重构.

工作计划报告和TaskDetails DTO共有一些相似类型的属性,并且还有许多其他不同属性.

我不允许更改那些DTO,因为它们在某些相关的通用代码库中使用.

编辑:这是此代码中使用的一种方法.我尽力使级别变得简单,但无法提出任何可行的想法.

private void setSummaryColumns(String DolOrHr, String columnFilter,

Map filterBy, Object[] obj,

WorkplanReport workplanReport, TemplateDump td) {

switch(filterBy.get(columnFilter)){

case "TOP DOWN":

td.setTopDownBudgetStartDate(obj[1] != null ? (Date)obj[1]:null);

td.setTopDownBudgetEndDate(obj[2] != null ? (Date)obj[2]:null);

workplanReport.setStartDatebyMajorMeasure(obj[1] != null ? obj[1].toString():"-");

workplanReport.setEndDatebyMajorMeasure(obj[2] != null ? obj[2].toString():"-");

workplanReport.setDolorHrsbyMajorMeasure(obj[3] != null ? obj[3].toString():"-");

if(DolOrHr.equals("BudgetDollars"))

td.setTopDownBudgetDollars(obj[3] != null ? (BigDecimal)obj[3]:null);

else

td.setTopDownBudgetHours(obj[3] != null ? (BigDecimal)obj[3]:null);

break;

case "TEMPLATE":

td.setTemplateBudgetStartDate(obj[1] != null ? (Date)obj[1]:null);

td.setTemplateBudgetEndDate(obj[2] != null ? (Date)obj[2]:null);

workplanReport.setStartDatebyTemplateBudget(obj[1] != null ? obj[1].toString():"-");

workplanReport.setEndDatebyTemplateBudget(obj[2] != null ? obj[2].toString():"-");

workplanReport.setDolorHrsbyTemplateBudget(obj[3] != null ? obj[3].toString():"-");

if(DolOrHr.equals("BudgetDollars"))

td.setTemplateBudgetDollars(obj[3] != null ? (BigDecimal)obj[3]:null);

else

td.setTemplateBudgetHours(obj[3] != null ? (BigDecimal)obj[3]:null);

break;

case "ORIGINAL":

td.setOriginalBudgetStartDate(obj[1] != null ? (Date)obj[1]:null);

td.setOriginalBudgetEndDate(obj[2] != null ? (Date)obj[2]:null);

workplanReport.setOrgStartDate(obj[1] != null ? obj[1].toString():"-");

workplanReport.setOrgEndDate(obj[2] != null ? obj[2].toString():"-");

workplanReport.setOrgBudgetedDollarsHours(obj[3] != null ? obj[3].toString():"-");

if(DolOrHr.equals("BudgetDollars"))

td.setOriginalBudgetDollars(obj[3] != null ? (BigDecimal)obj[3]:null);

else

td.setOriginalBudgetHours(obj[3] != null ? (BigDecimal)obj[3]:null);

break;

case "REVISED":

td.setRevisedBudgetStartDate(obj[1] != null ? (Date)obj[1]:null);

td.setRevisedBudgetEndDate(obj[2] != null ? (Date)obj[2]:null);

workplanReport.setRevStartDate(obj[1] != null ? obj[1].toString():"-");

workplanReport.setRevEndDate(obj[2] != null ? obj[2].toString():"-");

workplanReport.setRevBudgetedDollarsHours(obj[3] != null ? obj[3].toString():"-");

if(DolOrHr.equals("BudgetDollars"))

td.setRevisedBudgetDollars(obj[3] != null ? (BigDecimal)obj[3]:null);

else

td.setRevisedBudgetHours(obj[3] != null ? (BigDecimal)obj[3]:null);

break;

case "MANAGER":

td.setManagerBudgetStartDate(obj[1] != null ? (Date)obj[1]:null);

td.setManagerBudgetEndDate(obj[2] != null ? (Date)obj[2]:null);

workplanReport.setStartDatebyManagersRevised(obj[1] != null ? obj[1].toString():"-");

workplanReport.setEndDatebyManagersRevised(obj[2] != null ? obj[2].toString():"-");

workplanReport.setDolorHrsbyManagersRevised(obj[3] != null ? obj[3].toString():"-");

if(DolOrHr.equals("BudgetDollars"))

td.setManagerBudgetDollars(obj[3] != null ? (BigDecimal)obj[3]:null);

else

td.setManagerBudgetHours(obj[3] != null ? (BigDecimal)obj[3]:null);

break;

case "ACTUAL":

td.setActualBudgetStartDate(obj[1] != null ? (Date)obj[1]:null);

td.setActualBudgetEndDate(obj[2] != null ? (Date)obj[2]:null);

workplanReport.setStartDatebyActual(obj[1] != null ? obj[1].toString():"-");

workplanReport.setEndDatebyActual(obj[2] != null ? obj[2].toString():"-");

workplanReport.setDolorHrsbyActual(obj[3] != null ? obj[3].toString():"-");

if(DolOrHr.equals("BudgetDollars"))

td.setActualBudgetDollars(obj[3] != null ? (BigDecimal)obj[3]:null);

else

td.setActualBudgetHours(obj[3] != null ? (BigDecimal)obj[3]:null);

break;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值