Today in the project encountered the use of switch statements to determine the condition, but the problem is more conditions, about dozens of conditions, full screen case judgment, whether there is a more elegant alternative to the switch statement?
Suppose there is a scenario where the shopping malls often take different discounts according to the situation, and if the discount scheme is less, consider using a switch statement for judgment. But if there are dozens of or even hundreds of discount schemes, using the switch statement is not elegant enough.
First, a discount interface.
public interface Ivalueprocessor
{
Decimal Dazhe (short policy,decimal orginprice);
}
The formal parameter policy is used to receive enumeration items about discounting, and the formal parameter orginprice represents the price before the discount. The enumeration entries for discounting are:
public enum Policyenum
{
Wuzhe = 0,
Liuzhe = 1,
Qizhe = 2,
Bazhe = 3,
}
Implement Ivalueprocessor interface, take different algorithm according to different policyenum items.
public class Myvalueprocessor:ivalueprocessor
{
Public decimal Dazhe (short policy,decimal orginprice)
{
Switch (Policy)
{
Case (short) Policyenum.wuzhe:
return ORGINPRICE/2;
Case (short) Policyenum.liuzhe:
Return Orginprice * (decimal) 0.6;
Case (short) Policyenum.qizhe:
Return Orginprice * (decimal) 0.7;
Case (short) Policyenum.bazhe:
Return Orginprice * (decimal) 0.8;
Case (short) Policyenum.jiuzhe:
Return Orginprice * (decimal) 0.9;
Default
return ORGINPRICE/2;
}
}
}
The client calls are as follows:
static void Main (string[] args)
{
Console.WriteLine ("Please enter the discount policy, 0 means 50 percent, 1 means 60 percent, 2 means 70 percent, 3 means 80 percent, 4 means 90 percent:");
String policy = Console.ReadLine ();
Decimal originprice = (decimal) 100.00;
Console.WriteLine ("The price before the discount is:" + originprice);
Myvalueprocessor processor = new Myvalueprocessor ();
Console.WriteLine ("Price After Discount:" + processor. Dazhe (short. Parse (Policy), originprice));
Console.readkey ();
}
The above is not too big question, whether there is a replacement switch judgment, a more elegant way of writing it?
In the Dazhe (short Policy,decimal Orginprice) method of the Myvalueprocessor class, receives a parameter of type short and a formal parameter of type decimal, returning the decimal type, inside the method, The parameter of the short type is used as the judging condition of the switch statement, and then the return value is obtained using different algorithms. Can be further abstracted: the short type as a key in the dictionary collection, the algorithm, that is, the delegate as a dictionary collection of value. In this way, we can encapsulate various discount schemes in the Dictionary collection. Modify the following:
public class Myvalueprocessor:ivalueprocessor
{
Private ReadOnly Dictionary<shortFunc<decimaldecimal> > _dic;
Public Myvalueprocessor ()
{
_dic = new Dictionary<shortFunc<decimaldecimal>
{0, M = = m * (decimal) 0.5},
{1, M = = m * (decimal) 0.6},
{2, M = = m * (decimal) 0.7},
{3, M = = m * (decimal) 0.8},
{4, M = = m * (decimal) 0.9}
};
}
Public decimal Dazhe (short policy,decimal orginprice)
{
if (_dic. ContainsKey (Policy))
{
Return _dic[policy]. Invoke (Orginprice);
}
return ORGINPRICE/2;
}
}
Thus, within the Dazhe (short Policy,decimal orginprice) method, it is only possible to determine if the incoming short type argument is a key to the dictionary collection.
A more elegant way to replace a switch statement in C #