rtn check(act,btn1,btn2,...){
return checkExcute(act,btn1,...)
}
rtn checkExcute(act,btn1,btn2,...){
rtn = null
if(rtn = hasExcuteBtn1(act,btn1) != null)
return rtn
if(rtn = hasExcuteBtn2(act,btn2) != null)
return rtn
...
}
-- n个函数,每个函数m个case
rtn hasExcuteBtn1(act,btn){
switch(act){
case 1: 1 dosomething
case 2: 2 dosomething
...
}
dosomething
return rtn
}
rtn hasExcuteBtn1(act,btn){
switch(act){
case 1: 3 dosomething
case 2: 4 dosomething
...
}
dosomething
return rtn
}
...
rtn check(act,btn1,btn2,...){
return checkExcute(act,btn1,...)
}
rtn checkExcute(act,btn1,btn2,...){
rtn = null
if(rtn = hasExcuteBtn(act,btnType,btn1) != null)
return rtn
if(rtn = hasExcuteBtn(act,btnType,btn2) != null)
return rtn
...
}
-- 1个函数,每个函数m个case
rtn hasExcuteBtn(act,btnType,Btn){
switch(act){
case 1: handleAct1(btnType,Btn) dosomething
case 2: handleAct2(btnType,Btn) dosomething
...
}
dosomething
return rtn
}
-- m个函数,每个函数n个case
-- 本来2m个函数,每个函数重载了一次。后来想到上面的Btn抽象成1个,这里也抽象成1个,具体再在函数中转换!
handleAct1(btnType,Btn){
switch(btnType){
case a:1
case b:3
...
}
}
handleAct2(btnType,Btn){
switch(btnType){
case a:2
case b:4
...
}
}
...
一开始的思路分得太细,虽然合并了一层,但是应该再往上抽象,先不要管具体的子情况,就当做是1种情况来思考代码结构,整个结构会清晰简单很多。
代码分多一两个层级,每个函数可以不用那么挤。目前想法?:看起来确实没什么差别,主要在于少写了重复的dosomething,这样看起来hasExcuteBtn流程更清晰。而且本来打算handleBtn的case需要的btn类型有两种,要重载,数量反而还多了一倍,幸亏后来抽象了。
刚才开始茅塞顿开,思路可能未完整,欢迎评论提点,我会及时吸取意见!