我有一个问题,我需要考虑4种布尔值的可能选项的每种组合。 e.g。
我有4个布尔值,useFeatured,groupBy,orderBy&上升。可能的组合是..
featured.
featured & orderby.
featured & orderby & groupby.
featured & orderby & groupby & ascending.
featured & groupby.
featured & groupby & ascending.
featured & ascending
featured & ascedning & orderby
orderby
orderby & groupby
orderby & groupby & ascending
groupby
groupby & ascending
ascending
ascending & orderby
目前,我有一个巨大的if else else树,这些选项我觉得很难相信它是唯一的解决方案,但我无法想到其他任何事情。
这是if else树的链接。
if(featured){
if(orderby){
if(groupby){
if(ascedning){
//featured orderby groupby ascending
}else{
//featured orderby groupby
}
}else{
//featured orderby
}
}else if(groupby){
if(ascedning){
//featured groupby ascending
}else{
//featured groupby
}
}else if(ascending){
if(orderby){
//featured ascending orderby
}else{
//featured ascending
}
}else{
//featured
}
}else if(orderby){
if(groupby){
if(ascending){
//orderby groupby ascending
}else{
//orderby groupby
}
}
}else if(groupby){
if(ascending){
//groupby ascending
}else{
//groupvy
}
}else if(ascending){
if(orderby){
//ascending & orderby
}else{
//ascending
}
}else{
//orderby
}
如果混乱格式化,请使用Pastebin。 http://pastebin.com/ECgASU3C
任何人都可以采用更好的解决方案吗?请救救我!
答案 0 :(得分:0)
你可以使用切换案例 ......但 if else 是更好的选择