最近又重新拿起了机房合作,经过几天的奋战,代码实现已经基本完工。学生下机计算消费时间和消费是一个比较复杂的逻辑,经过两天的考虑之后发现其实也没有那么难,结合大话设计模式,发现只是对设计模式的简单应用。
下机需要做的事情:
1)计算下机时间
2)根据不同用户计算消费金额
先看一下类图,类图如下:这三个类依次为准备时间、最少上机时间、单位上机时间。职
模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止。这三个类实现从准备时间--->最少上机时间----->单位上机时间依次进行处理,直到处理完成。
LineTimeHander:定义抽象的方法
public abstract class LineTimeHandler { protected LineTimeHandler successor; #region 设置继任者 void SetSuccessor(LineTimeHandler successor) /// <summary> /// 设置继任者 /// </summary> /// <param name="successor"></param> public void SetSuccessor(LineTimeHandler successor) { this.successor = successor; } #endregion #region 处理请求的抽象方法 abstract int HandleTime(int request) /// <summary> /// 处理请求的抽象方法 /// </summary> /// <param name="request"></param> /// <returns></returns> public abstract int HandleTime(int request); #endregion }
PrepareTimeHandler1:准备时间
public class PrepareTimeHandler1 : LineTimeHandler { private int ReadyTime; public PrepareTimeHandler1(List<BasicDataEntity> basicdata){ this.ReadyTime = int.Parse(basicdata[0].ReadyTime); } ~PrepareTimeHandler1(){ } public override int HandleTime(int ConsumeTime) { if (ConsumeTime <= ReadyTime) { return 0; } else { return successor.HandleTime(ConsumeTime); } } }
LeastTimeHander:最少上机时间,继承抽象类
public class LeastTimeHander:LineTimeHandler { private int leastTime; #region 最少上机时间 LeastTimeHander(List<BasicDataEntity> basicdata) /// <summary> /// 最少上机时间 /// </summary> /// <param name="basicdata"></param> public LeastTimeHander(List<BasicDataEntity> basicdata) { this.leastTime = basicdata[0].LeastTime; } #endregion #region 重写LineTimeHandler中HandleTime方法 override int HandleTime(int ConsumeTime) /// <summary> /// 重写LineTimeHandler中HandleTime方法 /// </summary> /// <param name="ConsumeTime"></param> /// <returns></returns> public override int HandleTime(int ConsumeTime) { if (ConsumeTime < leastTime) { return leastTime; } else { return successor.HandleTime(ConsumeTime); } } #endregion }
LineTimeHandler2:单位递增时间,继承抽象类
public class LineTimeHandler2 : LineTimeHandler { private int IncreaseTime; #region 构造函数 LineTimeHandler2(List<BasicDataEntity> basicdata) /// <summary> /// 构造函数 /// </summary> /// <param name="basicdata"></param> public LineTimeHandler2(List<BasicDataEntity> basicdata) { this.IncreaseTime = basicdata[0].IncreaseTime; } #endregion #region 重写LineTimeHandler中的HandleTime方法 override int HandleTime(int ConsumeTime) /// <summary> /// 重写LineTimeHandler中的HandleTime方法 /// </summary> /// <param name="ConsumeTime"></param> /// <returns></returns> public override int HandleTime(int ConsumeTime) { return ConsumeTime; } #endregion }
另外在类图中还看到一个类,是为了方便在客户端的调用,我在外观写了一个类,给客户端一个统一的接口,只要传入参数,我就可以直接调用B层的方法。
LineTimeHandlerCountFacade:
public class LineTimeHandlerCountFacade { #region int CostTime(List<BasicDataEntity> basicdata, OnOffLineEntity line) /// <summary> /// 计算上机时间 /// </summary> /// <param name="basicdata"></param> /// <param name="line"></param> /// <returns></returns> public int CostTime(List<BasicDataEntity> basicdata, OnOffLineEntity line) { //实例化类,通过构造函数传递参数 PrepareTimeHandler1 fPrepareTime = new PrepareTimeHandler1(basicdata); LeastTimeHander fLeastTime = new LeastTimeHander(basicdata); LineTimeHandler2 fLineTime = new LineTimeHandler2(basicdata); //设置职责链的上家和下家 fPrepareTime.SetSuccessor(fLeastTime); fLeastTime.SetSuccessor(fLineTime); //计算上机时间 int ConsumeTime; TimeSpan tMinute = (Convert.ToDateTime(line.OffTime)).Subtract(Convert.ToDateTime(line.OnTime)); TimeSpan tHour = (Convert.ToDateTime(line.OffDate)).Subtract(Convert.ToDateTime(line.OnDate)); ConsumeTime = tMinute.Minutes + tMinute.Hours * 60 + tHour.Days * 12 * 60; return fPrepareTime.HandleTime(ConsumeTime); } #endregion }
客户端调用:
//调用职责链模式,计算上机时间 oole.ConsumeTime = LineFacade.CostTime(basicdataEntity, oole); int ConsumeTime = oole.ConsumeTime;