Scene
Call take-out payment, you can have the following benefits:
1, over 30 yuan minus 12
2, is the member to reduce the delivery fee, for example 5 yuan
3. Coupon
....
Problem? How to be more flexible to control the change of the offer without changing the code???
Some code may not be compatible with the actual business, designed to experience the usefulness of reflection
Public InterfaceIdiscount {/// <summary> ///offer name/// </summary> stringName {Get; } /// <summary> ///Amount/// </summary> /// <param name= "Money" ></param> /// <returns></returns> DoubleCalcmoney (DoubleTotalmoney); }
--Vouchers
namespacecashlib{/// <summary> ///Vouchers/// </summary> Public classCashdiscount:idiscount { Public stringName {Get{return "Voucher: Rabbit food stamps 2 yuan, can be deducted 2 yuan"; } } Public DoubleCalcmoney (DoubleTotalmoney) { returnTotalmoney-2; } }}
--Member
namespacemembelib{/// <summary> ///member/// </summary> Public classMemberdiscount:idiscount { Public stringName {Get{return "member free shipping fee of 5 Yuan"; } } Public DoubleCalcmoney (DoubleTotalmoney) { returnTotalmoney-5; } }}
-Full Amount reduction
namespaceminuslib{/// <summary> ///reduced amount of the full price/// </summary> Public classMinusdiscount:idiscount { Public stringName {Get{return "over 30 minus 12 Yuan"; } } Public DoubleCalcmoney (DoubleTotalmoney) { returnTotalmoney- A; } }}
--Read the assembly of the preferential policy, dynamically create the object of the class and invoke the members of the class
Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Dictionary<string,Object> dict =Newdictionary<string,Object>(); Private voidForm1_Load (Objectsender, EventArgs e) { stringDllPath = AppDomain.CurrentDomain.BaseDirectory +"discountlib/"; string[] Assfiles = System.IO.Directory.GetFiles (DllPath,"*.dll"); foreach(varDlliteminchassfiles) {Assembly=Assembly.LoadFrom (Dllitem); foreach(Type typeinchThe . GetTypes ()) {if(typeof(Idiscount). IsAssignableFrom (type)) {ObjectInstancediscount =The . CreateInstance (type. FullName); PropertyInfo Propdiscountname= Type. GetProperty ("Name"); stringDiscountname =Propdiscountname.getvalue (Instancediscount). ToString (); This. LISTBOX1.ITEMS.ADD (Discountname); Dict. ADD (Discountname, Instancediscount); } } } } Private voidButton1_Click (Objectsender, EventArgs e) {CustomerOrder Cusorder=NewCustomerOrder ("Zhang San", *); foreach(stringIteminch This. Listbox1.selecteditems) {Cusorder.adddiscount (Dict[item] asidiscount); } cusorder.usediscount (); } }
--Store Coupon assembly
-Results
-No Coupon
--Selected vouchers for coupons
--member + voucher
--Select All
--With a new coupon, simply place the new DLL in the Debug\discountlib folder and the system will load all the offers automatically
--Remove or modify a promotion, simply modify the code or delete an assembly, without affecting other preferential business ....
--Reflection combined with abstract class or interface will greatly improve the extension of the program.
--time rush, not written very thin, more details and reflection of the operation see the next article, to ensure that the life will always be familiar with the use of reflection, such as interested in scanning to join the programmer work Chat (must be a programmer) discussion group or QQ group
--code in QQ group
. NET actual combat reflected takeaway billing