2013年9月10日星期二

4 & 4 multi-condition to make a judgment , what a good solution?

There is such a problem : void charge (param1, param2) {}

parameters param1 have front A, B, C, D four values, parameters param2 have to do 1,2,3,4 four values ​​as follows:
if (A && 1) {};
else if (A && 2) {};
else if (A && 3) {};
else if (A && 4) {};
else if (B && 1) {};
else if (B && 2) {};
else if (B && 3) {};
else if (B && 4) {};
else if (C && 1) {};
else if (C && 2) {};
else if (C && 3) {};
else if (C && 4) {};
Do you really want to write ? There are other way to do this ? Great God who



------ Solution ------------------------------------ --------
switch
------ Solution ---------------------------- ----------------
look GOF strategy mode you have no inspiration.
you that A, B, C, D, 1,2,3,4 later if plus one on the tragedy .
------ Solution ---------------------------------------- ----
this depends on if you qualify what is executed after the operation , if the operation performed under different conditions and if there is a certain relationship between the conditions should be simplified way , without any relationship is independent estimates can not simplify the operation .

没有评论:

发表评论