本文主要是介绍【机房重组】 职责链模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
职责链模式:
对多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递请求,知道有一个对象处理它为止。
职责链模式:
机房中我用职责链模式来计算消费时间的问题
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 TimeHandlerBLLPublic Sub setsuccessor(ByVal successor As TimeHandlerBLL) '设置继承类 Me.successor = successorEnd SubPublic 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 IntegerPublic Sub New(ByVal EnBasicData As Entity.BasicInfo) '构造函数,传入准备时间值 Dim baseinfo As New BLL.BBasicInfoDim table As DataTabletable = baseinfo.CheckLimitCash1(EnBasicData)EnBasicData.PrepareTime = table.Rows(0).Item(4)Me.preparetime = CInt(EnBasicData.PrepareTime)End SubPublic Overrides Function handleTime(time As Integer) As IntegerIf time <= preparetime Then '如果上机时间小于准备时间,返回0 Return 0ElseReturn successor.handleTime(time) '否则转到下一位继承者 End IfEnd Function
End Class</span>
UnitTimeHandlerBLL类
<span style="font-size:24px;">Public Class UnitTimeHandlerBLL : Inherits TimeHandlerBLL '单位时间计算 Private unittime As IntegerPublic Sub New(ByVal basicdata As Entity.BasicInfo)Dim baseinfo As New BLL.BBasicInfoDim table As DataTabletable = baseinfo.CheckLimitCash1(basicdata)basicdata.UnitTime = table.Rows(0).Item(2)Me.unittime = CInt(basicdata.UnitTime)End SubPublic Overrides Function handleTime(time As Integer) As IntegerReturn Math.Abs(Int(-time / unittime)) * unittimeEnd Function
End Class
</span>
BLeastTimeHandlerBLL类
<span style="font-size:24px;">Public Class BLeastTimeHandlerBLL : Inherits TimeHandlerBLL '至少上机时间处理 Private leasttime As IntegerPublic Sub New(ByVal basicdata As Entity.BasicInfo)Dim baseinfo As New BLL.BBasicInfoDim table As DataTabletable = baseinfo.CheckLimitCash1(basicdata)basicdata.LeastTime = table.Rows(0).Item(3)Me.leasttime = CInt(basicdata.LeastTime) '将基础数据赋给leasttime这个变量 End SubPublic Overrides Function handleTime(time As Integer) As IntegerIf time <= leasttime ThenReturn leasttimeElseReturn successor.handleTime(time)End IfEnd 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'添加上机时间TryDim table As DataTableonlineinfo.status = "正在使用"table = online.Oncardno(onlineinfo)onlineinfo.ondate = table.Rows(0).Item(1)onlineinfo.ontime = table.Rows(0).Item(2).ToStringCatch ex As ExceptionEnd TrybPrepareTime.setsuccessor(bLeastTime) '设置下一个继承者 bLeastTime.setsuccessor(bAddTime)'添加下机时间onlineinfo.offdate = DateTime.Now.ToString("yyyy-MM-dd")onlineinfo.offtime = TimeOfDayDim 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>
好了就写到这里面了期待下一篇文章吧!
这篇关于【机房重组】 职责链模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!