抽象基类提供函数接口,不需要提供函数实现,此处区别于虚函数。
abstract class CashSuper{
public abstract double acceptCash(double money);
}
1.2.正常收费子类
class CashNormal:CashSuper{
public double acceptCash(double money){
return money;
}
}
1.3.打折收费子类
class CashRebate:CashSuper{
private double moneyRebate=1d;
public CashRebate(string moneyRebate){
this.moneyRebate=double.Parse(moneyRebate);
}
public override double acceptCash(double money){
return money*moneyRebate;
}
}
1.4.返利收费子类
class CashReturn{
private double moneyCondition=0.0d;
private double moneyReturn=0.0d;
public CashReturn(string moneyCondition,string moneyReturn){
this.moneyCondition=double.Parse(moneyCondition);
this.moneyReturn=double.Parse(moneyReturn);
}
public override double acceptCash(double money){
double result=money;
if(money>=moneyCondition)
result=money-Math.Floor(money/moneyCondtion)*moneyReturn;
return result;
}
}
1.5.现金收费工厂
class CashFactory{
public static CashSuper createCashAccept(string type){
CashSuper cs=null;
switch(type){
case "正常收费":
cs=new CashNormal;
break;
case "满300返100":
cs=new CashReturn("300","100");
break;
case "打8折":
cs=new CashRebate("0.8");
break;
}
return cs
}
}
1.6.客户端实现
double total=0.0d;
private void btnOk_Click(object sender,EventArgs e){
CashSuper csuper=CashFactory.createCashAccept(cbxType.SelectedItem.ToString());
double totalPrices=0d;
totalPrices=csuper.acceptCash(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNum.Text));
total=total+totalPrices;
lbxList.Item.Add("单价:"+textPrice.Text+"数量:"+txtNum.Text+" "+cbxType.SelectedItem+"合计:"+totalPrices.ToString());
lblResult.Text=total.ToString();
}
2.策略模式实现
之前的CashSuper、CashNormal、CashRebate和CashReturn都不用更改,只需添加一个CashContext类,并修改客户端就好了。
2.1.CashContext类class CashContext{
private CashSuper cs;
public CashContext(CashSuper csuper){
this.cs=csuper;
}
public double GetResult(double money){
return cs.acceptCash(money);
}
}
2.2.客户端主要代码
double total =0.0d;
private void btnOk_Click(object sender,EventArgs e){
CashContext cc=null;
switch(cbxType.SelectedItem.ToString()){
case "正常收费":
cc=new CashContext(new CashNormal());
break;
case "满300返100":
cc=new CashContext(new CashReturn("300","100"));
break;
case "打8折":
cc=new CashContex(new CashRebate("0.8");
break;
}
double totalPrices=0d;
totalPrices==cc.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(textNum.Text));
total=total+totalPrices;
lbxList.Items.Add("单价:"+txtPrice.Text+"数量:"+txtNum.Text+" "+cbxType.SelectedItem+"合计:"+totalPrices.ToString());
lblResult.Text=total.ToString();
}
3.策略与简单工厂结合
3.1改造后的CashContext
class CashContext{
CashSuper cs=null;
public CashContext(string type){
switch(type){
case "正常收费":
cs=new CashNormal();
break;
case "满300返100":
cs=new CashReturn("300","100");
break;
case "打8折":
cs=new CashRebate("0.8");
break;
}
}
public double GetResult(double money){
return cs.acceptCash(money);
}
}
3.2客户端代码
double total=0.0d;
private void btnOk_Click(object sender,EventArgs e){
CashContext csuper=new CashContext(cbxType.SelectedItem.ToString());
double totalPrices=0d;
totalPrices=csuper.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNum.Text));
total=total+totalPrices;
lbxList.Items.Add("单价:"+txtPrice.Text+"数量:"+txtNum.Text+" "+cbxType.SelectedItem+"合计:"+totalPrices.ToString());
lblResult.Text=total.ToString();
}
简单工厂模式的做法,客户端需要认识两个类,CashSuper和CashFactory,而策略模式与简单工厂结合的用法,客户端只需要认识CashContext就可以了,如此耦合进一步降低。



