设计模式之(策略模式)(C#实现)(程杰)
作者:互联网
abstract class CashSuper //定义一个抽象类
{
public abstract double acceptCash(double money);
}
class CashRebate:CashSuper
{
private double moneyRebate=0.0d;
public CashRebate(string moneyRebate)
{
this.moneyRebate=double.Parse(moneyRebate);//把几折数据保存到对象字段中
}//实现父类方法
public override double acceptCash(double money) //参数是价钱
{
return money*moneyRebate;
}
}
//正常收费的子类
class CashNormal:CashSuper
{
public override double acceptCash(double money)
{
return money;
}
}
class CashReturn:CahSuper
{
private double moneyCondition=0.0d;
private double moneyReturn=0.0d;
public CashReturn(string moneyCondition,string moneyReturn)
{
this.moneyCondition=moneyCondition;
this.moneyReturn=moneyReturn;
}
public override double acceptCash(double money)
{
double result=money;
if(money>moneyCondition)
{
result=money-Math.Floor(money/moneyCondition)*moneyReturn;
}
return result;
}
}
//策略类
class Context
{
private CashSuper cash;
public Context(CashSuper cash)
{
this.cash=cash;
}
public double acceptCash(double money)
{
cash.acceptCash(money)
}
}
客户端进行调用://应用了某一种策略
Context context=null;
context=new Context(new CashReturn(a.Text,b.Text));
context.acceptCash(c.Text);
策略模式的附图以后:
转载于:https://www.cnblogs.com/slove/archive/2012/07/16/2594360.html
标签:moneyRebate,C#,double,acceptCash,程杰,moneyCondition,money,设计模式,public 来源: https://blog.csdn.net/weixin_30651273/article/details/97737328