职责链模式:
对多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递请求,知道有一个对象处理它为止。
职责链模式:
机房中我用职责链模式来计算消费时间的问题
BLL层:
TimeHandlerBLL类
<span style="font-size:24px;">'**********************************************
'文件名:TimeHandlerBLL
'命名空间:BLL
'内容:
'功能:
'文件关系:
'作者:滕柳
'小组:
'生成日期:2016-3-25 18:23:13
'版本号:V1.0.0.0
'修改日志:
'版权说明:
'**********************************************
''' <summary>
''' '抽象基类
''' </summary>
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
</span>
PrepareTimeHandlerBLL类
<span style="font-size:24px;">Public Class PrepareTimeHandlerBLL : Inherits TimeHandlerBLL '准备时间
Dim preparetime As Integer
Public Sub New(ByVal EnBasicData As Entity.BasicInfo) '构造函数,传入准备时间值
Dim baseinfo As New BLL.BBasicInfo
Dim table As DataTable
table = baseinfo.CheckLimitCash1(EnBasicData)
EnBasicData.PrepareTime = table.Rows(0).Item(4)
Me.preparetime = CInt(EnBasicData.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</span>
UnitTimeHandlerBLL类
<span style="font-size:24px;">Public Class UnitTimeHandlerBLL : Inherits TimeHandlerBLL '单位时间计算
Private unittime As Integer
Public Sub New(ByVal basicdata As Entity.BasicInfo)
Dim baseinfo As New BLL.BBasicInfo
Dim table As DataTable
table = baseinfo.CheckLimitCash1(basicdata)
basicdata.UnitTime = table.Rows(0).Item(2)
Me.unittime = CInt(basicdata.UnitTime)
End Sub
Public Overrides Function handleTime(time As Integer) As Integer
Return Math.Abs(Int(-time / unittime)) * unittime
End Function
End Class
</span>
BLeastTimeHandlerBLL类
<span style="font-size:24px;">Public Class BLeastTimeHandlerBLL : Inherits TimeHandlerBLL '至少上机时间处理
Private leasttime As Integer
Public Sub New(ByVal basicdata As Entity.BasicInfo)
Dim baseinfo As New BLL.BBasicInfo
Dim table As DataTable
table = baseinfo.CheckLimitCash1(basicdata)
basicdata.LeastTime = table.Rows(0).Item(3)
Me.leasttime = CInt(basicdata.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
</span>
Facede层
<span style="font-size:24px;">'消费时间的计算
Public Function costTime(ByVal basicdata As Entity.BasicInfo, onlineinfo As Entity.OnlineInfo) As Integer
'实例化,通过构造函数,传递参数
Dim bPrepareTime As New BLL.PrepareTimeHandlerBLL(basicdata)
Dim bLeastTime As New BLL.BLeastTimeHandlerBLL(basicdata)
Dim bAddTime As New BLL.UnitTimeHandlerBLL(basicdata)
Dim online As New BLL.BOnlineInfo
'添加上机时间
Try
Dim table As DataTable
onlineinfo.status = "正在使用"
table = online.Oncardno(onlineinfo)
onlineinfo.ondate = table.Rows(0).Item(1)
onlineinfo.ontime = table.Rows(0).Item(2).ToString
Catch ex As Exception
End Try
bPrepareTime.setsuccessor(bLeastTime) '设置下一个继承者
bLeastTime.setsuccessor(bAddTime)
'添加下机时间
onlineinfo.offdate = DateTime.Now.ToString("yyyy-MM-dd")
onlineinfo.offtime = TimeOfDay
Dim time As Integer '计算实际在线时间
time = DateDiff("n", onlineinfo.ontime, onlineinfo.offtime) + DateDiff("n", onlineinfo.ondate, onlineinfo.offdate)
Return bPrepareTime.handleTime(time) '职责链处理,返回上机时间
End Function</span>
UI层
<span style="font-size:24px;"> '调入外观层 传入数据消费时间 调用职责链模式
txtCumeTime.Text = facade1.costTime(basicInfo, onlineInfo1)</span>
好了就写到这里面了期待下一篇文章吧!