我想减少开关情况的圈复杂度,我的代码是:
public String getCalenderName() {
switch (type) {
case COUNTRY:
return country == null ? name : country.getName() + HOLIDAY_CALENDAR;
case CCP:
return ccp == null ? name : ccp.getName() + " CCP" + HOLIDAY_CALENDAR;
case EXCHANGE:
return exchange == null ? name : exchange.getName() + HOLIDAY_CALENDAR;
case TENANT:
return tenant == null ? name : tenant.getName() + HOLIDAY_CALENDAR;
default:
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
此代码块的复杂度为16,并希望将其降低到10。country,ccp,exchange和tenant是我的不同对象。基于类型I将调用它们各自的方法。