标签:
/// <summary>
/// 现金收费的抽象类
/// </summary>
abstract class CashSuper
{
/// <summary>
/// 现金收取超类的抽象方法,收取现金,参数为原价,返回为当前价
/// </summary>
/// <param name="money"></param>
/// <returns></returns>
public abstract double acceptCash(double money);
}
现金收费的子类 (我在项目中放在一个文件中了CashSubAll.cs)
/// <summary>
/// 正常收费的子类
/// </summary>
class CashNormal : CashSuper
{
public override double acceptCash(double money)
{
//正当收费,原价返回
return money;
}
}
/// <summary>
/// 打折收费子类
/// </summary>
class CashRebate : CashSuper
{
private double moneyRebate = 1;
/// <summary>
/// 通过构造方法为私有变量赋值
/// </summary>
/// <param name="moneyRebate"></param>
public CashRebate(string moneyRebate)
{
this.moneyRebate = double.Parse(moneyRebate);
}
public override double acceptCash(double money)
{
return money * moneyRebate;
}
}
/// <summary>
/// 返利收费子类
/// </summary>
class CashReturn : CashSuper
{
private double moneyCondition = 0;
private double moneyReturn = 0;
/// <summary>
/// 通过构造方法,为返利收费初始化必要参数,比如“满300返100”,则moneyCodition为300,moneyReturn为100
/// </summary>
/// <param name="moneyCondition"></param>
/// <param name="moneyReturn"></param>
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/moneyCondition)*moneyReturn;
}
return result;
}
}
现金收费工厂类
/// <summary>
/// 现金收费工厂类
/// </summary>
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;
}
}
客户端调用
//通过简单工厂创建实例
CashSuper cs = CashFactory.createCashAccept(cbxType.SelectedItem.ToString());
double totalPrices = double.Parse(txtPrice.Text)*double.Parse(txtNum.Text);
//调用简单工厂的方法
double result = cs.acceptCash(totalPrices);
lboxListResult.Items.Add("总金额为:" + totalPrices + ",参加的活动为:" + cbxType.SelectedItem + ",应收金额:" + result);
下面是今天的主角策略模式,我们先来看看策略模式的简易代码:
// 抽象算法类
public abstract class Strategy
{
//算法方法
public abstract void AlgoritymInterface();
}
// 具体算法A
public class ConcreteStrategyA : Strategy
{
//算法A实现
public override void AlgoritymInterface()
{
Console.WriteLine("算法A");
}
}
// 具体算法B
public class ConcreteStrategyB : Strategy
{
//算法B实现
public override void AlgoritymInterface()
{
Console.WriteLine("算法B");
}
}
/// <summary>
/// 上下文,用一个ConcreteStrategy来配置,维护一个对Strategy对像的引用
/// </summary>
public class Context
{
Strategy stg = null;
public Context(Strategy stg)
{
this.stg = stg;
}
//上下文接口
public void ContextInterface()
{
stg.AlgoritymInterface();
}
}
class Program
{
/// <summary>
/// 客户端实现
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Context context = null;
context = new Context(new ConcreteStrategyA());
context.ContextInterface();
context = new Context(new ConcreteStrategyB());
context.ContextInterface();
Console.Read();
}
}
/// <summary>
/// 上下文
/// </summary>
public class CashContext
{
private CashSuper cs = null;
public CashContext(CashSuper cs)
{
this.cs = cs;
}
/// <summary>
/// 调用具体策略
/// </summary>
/// <param name="moeny"></param>
/// <returns></returns>
public double GetResult(double moeny)
{
return cs.acceptCash(moeny);
}
}
客户端调用
double totalPrices = double.Parse(txtPrice.Text) * double.Parse(txtNum.Text);
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 CashContext(new CashRebate("0.8"));
break;
}
double result = cc.GetResult(totalPrices);
lboxListResult.Items.Add("总金额为:" + totalPrices + ",参加的活动为:" + cbxType.SelectedItem + ",应收金额:" + result);
看到这样的客户端调用是不是欲哭无泪,又回到最古老的方式,在客户端判断写了好像代码,其实在这里我们还可以结合一下简单工厂模式:
/// <summary>
/// 上下文
/// </summary>
public class CashContext
{
private CashSuper cs = null;
/// <summary>
/// 结合简单工厂
/// </summary>
/// <param name="type"></param>
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;
}
}
/// <summary>
/// 调用具体策略
/// </summary>
/// <param name="moeny"></param>
/// <returns></returns>
public double GetResult(double moeny)
{
return cs.acceptCash(moeny);
}
}
客户端调用
double totalPrices = double.Parse(txtPrice.Text) * double.Parse(txtNum.Text);
//策略上下文
CashContext cc = new CashContext(cbxType.SelectedItem.ToString());
double result = cc.GetResult(totalPrices);
lboxListResult.Items.Add("总金额为:" + totalPrices + ",参加的活动为:" + cbxType.SelectedItem + ",应收金额:" + result);
好了,完成。策略模式实现的代码:点击下载
标签:
原文地址:http://www.cnblogs.com/itmu89/p/5217996.html