//商场收银软件
面向对象的编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类
策略模式:它定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户
//抽象算法类
abstract class Strategy
{
public abstract void AlgorithmInterface();
}
//具体算法A
class ConcreteStrategyA:Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("算法A实现");
}
}
//具体算法B
class ConcreteStrategyB:Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("算法B实现");
}
}
//具体算法C
class ConcreteStrategyC:Strategy
{
public override void AlgorithmInterface()
{
Console.WriteLine("算法C实现");
}
}
//Context 维护一个对Strategy对象的引用
class Context
{
Strategy strategy;
public Context(Strategy strategy)
{
this.strategy=strategy;
}
public void ContextInterface()
{
strategy.AlgorithmInterface();
}
}
//客户端代码
static void Main(string[] args)
{
Context context;
context=new Context(new ConcreteStrategyA());
context.ContextInterface();
context=new Context(new ConcreteStrategyB());
context.ContextInterface();
context=new Context(new ConcreteStrategyC());
context.ContextInterface();
}
//简单工厂
//现金收费抽象类
abstract class CashSuper
{
public abstract double acceptCask(double money);
}
//正常收费子类
class CashNormal:CashSuper
{
public overrie double acceptCash(double money)
{
return money;
}
}
//打折收费子类
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;
}
}
//返利收费子类
class CashReturn:CashSuper
{
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/monerCondition)*moneyReturn;
return result;
}
}
//现金收费工厂类
class CashFactory
{
public static CashSuper createCashAccept(string type)
{
CashSuper cs=null;
switch(type)
{
case "正常收费":
cs=new CashNormal();
break;
case "满300返100":
CashReturn cr1=new CashReturn("300","100");
cs=cr1;
break;
case "打8折":
CashRebate cr2=new CashRebate("0.8");
cs=cr2;
break;
}
return cs;
}
}
//客户端代码
double total=0.0d;
private void btnOk_Click(object sender,EventArgs e)
{
CashSuper csuper=CashFactory.createCashAccept(cbxType.SelectItem.ToString());
double totalPrices=0d;
totalPrices=csuper.acceptCash(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNum.Text))
total=total=totalPrices;
lbxList.Item.Add("单价:"+txtPrice.Text+"数量:"+txtNum.Text+""+cbxType.SelectItem+"合计:"+totalPrices.ToString());
lblResult.Text=total.ToString();
}
缺点:每次维护和扩展收费方式都要改动这个工厂
//策略模式
CashSuper,CashNormal,CashRebate,CashReturn 这些更工厂模式一样
只要加一个CashContext类
class CashContext
{
private CashSuper cs;
pulic CashContext(CashSuper csuper)
{
this.cs=csuper;
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
}
//客户端代码
double total=0.0d;
private void btnOk_Click(object sender,EventArgs e)
{
CashContext cc=null;
switch(cbxType.SelectItem.ToString())
{
case "正常收费":
cc=new CashContext(new CashNormal());
break;
case "满300返100":
cc=new CashContext(new CashReturn("300","100"));
break;
case "打8折":
cc=new CashContext(new CashRebate("0.8"));
break;
}
double totalPrices=0d;
totalPrices=cs.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNum.Text))
total=total=totalPrices;
lbxList.Item.Add("单价:"+txtPrice.Text+"数量:"+txtNum.Text+""+cbxType.SelectItem+"合计:"+totalPrices.ToString());
lblResult.Text=total.ToString();
}
//策略与简单工厂结合
class CashContext
{
CashSuper cs=null;
pulic CashContext(string type)
{
switch(type)
{
case "正常收费":
CashNormal cs0=new CashNormal();
cs=cs0;
break;
case "满300返100":
CashReturn cr1=new CashReturn("300","100");
cs=cr1;
break;
case "打8折":
CashRebate cr2=new CashRebate("0.8");
cs=cr2;
break;
}
}
public double GetResult(double money)
{
return cs.acceptCash(money);
}
}
//客户端代码
double total=0.0d;
private void btnOk_Click(object sender,EventArgs e)
{
CashContext csuper=new CashContext(cbxType.SelectItem.ToString());
double totalPrices=0d;
totalPrices=csuper.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNum.Text))
total=total=totalPrices;
lbxList.Item.Add("单价:"+txtPrice.Text+"数量:"+txtNum.Text+""+cbxType.SelectItem+"合计:"+totalPrices.ToString());
lblResult.Text=total.ToString();
}
策略模式:
只要在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性。