职责链的入口
public class CostMoneyChain
{
public decimal getCostMoney(string rate, int costTime)
{
LimitMinuteHandler limitMinuteHandler = new LimitMinuteHandler();
LessHourHandler lessHourHandler = new LessHourHandler();
MoreHourHandler moreHourHandler = new MoreHourHandler();
limitMinuteHandler.SetSuccessor(lessHourHandler);
lessHourHandler.SetSuccessor(moreHourHandler);
limitMinuteHandler.consumecash(rate, costTime);
return CostMoneyHandler.costMoney;
}
}
抽象类
public abstract class CostMoneyHandler
{
public static decimal costMoney;
protected CostMoneyHandler successor;
public void SetSuccessor(CostMoneyHandler successor)
{
this.successor = successor;
}
public abstract void consumecash(string rate, int costTime);
}
处理5-60分钟的消费
public class LessHourHandler : CostMoneyHandler
{
public override void consumecash(string rate, int costTime)
{
if (costTime <= 60)
{
costMoney = Convert.ToDecimal(rate);
}
else if (successor != null)
{
successor.consumecash(rate, costTime);
}
}
}
处理最小分钟以内的金额
public class LimitMinuteHandler:CostMoneyHandler
{
public override void consumecash(string rate, int costTime)
{
string setuptime = System.Configuration.ConfigurationManager.AppSettings["setuptime"];
int intSetupTime = Convert.ToInt32(setuptime);
if (costTime < intSetupTime)
{
costMoney = 0;
}
else if (successor != null)
{
successor.consumecash(rate, costTime);
}
}
}
处理大于60分钟的消费
public class MoreHourHandler: CostMoneyHandler
{
public override void consumecash(string rate, int costTime)
{
if (costTime > 60)
{
if (costTime % 60 == 0)
{
costMoney = Convert.ToDecimal(rate) * costTime / 60;
}
else
{
costMoney = Convert.ToDecimal(rate) * (costTime / 60 + 1);
}
}
else if (successor != null)
{
successor.consumecash(rate, costTime);
}
}
}