在重构的过程中,通过查询一些博客,最后决定用职责链模式来计算下机消费时间。
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递改请求,直到有一个对象处理它为止。
小编解读:这里的多个对象就是我们需求中的三个时间对象,定义三个对象,准备时间对象、至少上机时间对象、递增单位时间对象,并且三个对象之间具有链的关系,把实际时间丢给链的开端,在整个链中总有一个对象能处理它。根据需求我们知道每个对象中都要有一个判断,如果该对象可以处理实际时间,则返回需要的值,如果处理不了实际时间则丢给下一个对象。
结合类图和需求,我们可以进行编码,由于这些属于判断逻辑,所以我们要在B层建立相应的对象。
抽象类:
<span style="font-size:18px;"><strong>Public MustInherit Class TimeHandlerBLL Protected successor As TimeHandlerBLL Public Sub setsuccessor(ByVal successor As TimeHandlerBLL) '设置继承类 Me.successor = successor End Sub Public MustOverride Function handleTime(ByVal time As Integer) As Integer '处理请求的抽象方法 End Class </strong></span>
相应的三个时间处理对象:
准备时间:
<span style="font-size:18px;"><strong>Public Class PrepareTimeHandlerBLL : Inherits TimeHandlerBLL Dim preparetime As Integer Public Sub New(ByVal EnBasicData As List(Of Entity.BasicSetEntity)) '构造函数,传入准备时间值 Me.preparetime = CInt(EnBasicData(0).Preparetime) End Sub Public Overrides Function handleTime(time As Integer) As Integer If time <= preparetime Then '上机时间小于准备时间,返回0 Return 0 Else Return successor.handleTime(time) '传入下一继承者 End If End Function End Class </strong></span>
至少上机时间:
<span style="font-size:18px;"><strong>Public Class LeastTimeHandlerBLL : Inherits TimeHandlerBLL '处理最少上机时间 Private leasttime As Integer Public Sub New(ByVal basicdata As List(Of Entity.BasicSetEntity)) Me.leasttime = CInt(basicdata(0).Leasttime) '将基础数据赋给leasttime这个变量 End Sub Public Overrides Function handleTime(time As Integer) As Integer If time <= leasttime Then Return leasttime Else Return successor.handleTime(time) End If End Function End Class </strong></span>
递增单位时间:
<span style="font-size:18px;"><strong>Public Class UnitTimeHandlerBLL : Inherits TimeHandlerBLL '单位时间计算 Private unittime As Integer Public Sub New(ByVal basicdata As List(Of Entity.BasicSetEntity)) Me.unittime = CInt(basicdata(0).Unittime) End Sub Public Overrides Function handleTime(time As Integer) As Integer Return Math.Abs(Int(-time / unittime)) * unittime End Function End Class </strong></span>
外观层:
<span style="font-size:18px;"><strong>Public Class CountTimeFac Public Function costTime(ByVal basicdata As List(Of Entity.BasicSetEntity), lineinfo As Entity.OnOffLineEntity) As Integer '实例化,通过构造函数,传递参数 Dim bPrepareTime As New BLL.PrepareTimeHandlerBLL(basicdata) Dim bLeastTime As New BLL.LeastTimeHandlerBLL(basicdata) Dim bAddTime As New BLL.UnitTimeHandlerBLL(basicdata) Dim time As Integer bPrepareTime.setsuccessor(bLeastTime) '设置下一个继承者 bLeastTime.setsuccessor(bAddTime) '计算在线时间 time = DateDiff("n", lineinfo.Ontime, lineinfo.Offtime) Return bPrepareTime.handleTime(time) End Function End Class </strong></span>
U层(传入实际时间):
<span style="font-size:18px;"><strong> '查询基本数据设定数据数据库,得到需要的时间 Dim basicfac As New Facade.BasicSetFac Dim basicdata As New List(Of Entity.BasicSetEntity) basicdata = basicfac.BasicSetInfo() '查询T_Line表获得上机时间 Dim onoff As New Entity.OnOffLineEntity Dim onofffac As New Facade.OffLineFac Dim list As New List(Of Entity.OnOffLineEntity) onoff.CardID = txtCardID.Text.Trim list = onofffac.OnOffInfo(onoff) '获取上机时间</strong></span>
在这里我们需要查询基本数据设定表,返回实体,以便于传入B层与实际时间做相应的比较。
至此,职责链模式应用完毕,多多思考,多多收获,继续加油!