职责链模式—消费金额
【摘要】
职责链的入口
/// <summary>
/// 该类是花费金额职责链的入口,用于b层调用
/// </summary>
public class...
职责链的入口
/// <summary>
/// 该类是花费金额职责链的入口,用于b层调用
/// </summary>
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;
}
}
抽象类
/// <summary>
/// 该类用于花费金额的抽象类,定义了骨架
/// </summary>
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分钟的消费
/// <summary>
/// 该类用于处理5-60分钟的消费金额的计算
/// </summary>
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);
}
}
}
处理最小分钟以内的金额
/// <summary>
/// 该类用于处理最小分钟以内的金额计算
/// </summary>
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分钟的消费
/// <summary>
/// 该类用于处理大于60分钟的消费金额的计算
/// </summary>
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);
}
}
}
文章来源: blog.csdn.net,作者:张艳伟_Laura,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Laura__zhang/article/details/114317732
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)