界面如下:
分析:
首先我们对于收钱写一个父类cashsuper。这个父类是用来包含其他的各种收费方式的:正常收费、七折优惠、八折优惠、九折优惠、满300减50、满400减70、满500减100等情况,其中折扣优惠虽然不同,但是类型相似,满减优惠同理,故此我们可以将正常收费、折扣优惠与满减优惠分别划分为三个不同的类:cashnormal、cashrebate、cashreturn。
而cashsuper这个父类是用于继承的,所以我们设置为abstract用来被重写的,其次这个父类所包含的的三个子类都会共同调用到一个参数:那就是实际需要收到的商品的价格,所以我们的参数只需要传进来共同的参数:acceptmoney即可。
1 abstract class cashsuper2 {3 public abstract double acceptcash(double acceptmoney);4 }
然后就是正常收费:cashnormal
首先他从父类获得参数是实际需要收到的商品的价格,他本身事正常收费没有任何的优惠,所以直接返回从父类传进来的值即可。
1 class cashnormal : cashsuper2 {3 public override double acceptcash(double acceptmoney)4 {5 return acceptmoney;6 }7 }
折扣优惠:cashrebate
他和正常收费类似,继承于父类cashsuper,会获得来自于父类的参数,得到实际需要收到的商品的价格,但是他需要实现的是对商品进行打折优惠,所以他需要自己定义一个折扣优惠参数,这样别人调用他的时候将折扣参数传进来,他就可以通过对原价进行折扣优惠反馈给用户了。
1 class cashrebate : cashsuper 2 { 3 //这就是cashrebate的属性了 4 private double monrebate = 1; 5 6 //调用cashrebate的时候需要从外面将优惠程度传进来 7 public cashrebate(string moneyrebate) 8 { 9 this.monrebate = double.parse(moneyrebate);10 }11 12 public override double acceptcash(double acceptmoney)13 {14 return acceptmoney * monrebate;15 }16 }
满减优惠:cashreturn
这个与折扣优惠类似了,只是他有两个参数而已:满减的水平线,和减扣多少。故此给此类定义两个参数即可。
1 class cashreturn : cashsuper 2 { 3 //这就是cashreturn的属性了 4 private double cashlevel = 0; 5 private double moneyreturn = 0; 6 7 //对外调用函数所以必须是public 8 public cashreturn(string level,string monreturn) 9 {10 this.cashlevel = double.parse(level);11 this.moneyreturn = double.parse(monreturn);12 }13 14 public override double acceptcash(double acceptmoney)15 {16 double result = acceptmoney;17 if (acceptmoney >= cashlevel)18 {19 result = acceptmoney - math.floor(acceptmoney / cashlevel) * moneyreturn;20 }21 return result;22 }23 }
现在获得了几种优惠情况了,但是我们需要判断什么时候需要调用哪种优惠情况,这点我们通过用户的选择,用户将选择的优惠方式传输过来,我们再确定调用哪一种优惠方式,这就是利用简单工厂模式,将所有的优惠方式封装好,在进一步调用。
1 class cashfactory 2 { 3 //cashsuper现在就类似double之类,返回值就是cashsuper 4 public static cashsuper createcashaccept(string type) 5 { 6 cashsuper cs = null; 7 8 switch (type) 9 {10 case 正常收费:11 cs = new cashnormal();12 break;13 case 满300减50:14 cs = new cashreturn(300, 50);15 break;16 case 满500减100:17 cs = new cashreturn(500, 100);18 break;19 case 满400减70:20 cs = new cashreturn(400, 70);21 break;22 case 满900减200:23 cs = new cashreturn(900, 200);24 break;25 case 八折优惠:26 cs = new cashrebate(0.8);27 break;28 case 九折优惠:29 cs = new cashrebate(0.9);30 break;31 case 七折优惠:32 cs = new cashrebate(0.7);33 break;34 }35 return cs;36 }37 }
最后在用户接口调用上面的函数即可。
1 private void ok_button_click(object sender, eventargs e) 2 { 3 /**对外这边需要了解两个函数 4 * 1.cashfactory.createcashaccept,这个是为了确定每一次的购物时调用的哪一种优惠方式 5 * 2.csuper.acceptcash,这个是为了获得每种优惠方式获得的优惠结果 6 */ 7 cashsuper csuper = cashfactory.createcashaccept(typecombobox.selecteditem.tostring()); 8 9 totalprices = csuper.acceptcash(double.parse(unitprice_textbox.text) * double.parse(amount_textbox.text));10 total += totalprices;11 listbox1.items.add(单价: + unitprice_textbox.text.tostring() + 数量: + amount.tostring() + + typecombobox.selecteditem.tostring() + 合计: + totalprices.tostring());12 resultlabel.text = total.tostring();13 }
以上就是c#简单工厂模式是什么?的详细内容。
