经过放假6天的调整,过年后,大家又可以整装待发,一起奋斗,一起迎接新一年的挑战了,祝大家日子过得猴开心!!程序设计的猴赛雷!!~~
今天总结一下职责链模式,以及职责链模式在机房下机中的应用。
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
自己的理解:这个模式为什么称之为职责链模式,在公司中,每个人的职责权限都是不同的,我们不能越权去处理一些事情,每个权限解决事物的条件不同,就拿请假来说吧,我请2小时假,纪委有权利批给我,但是如果我要请两天的假,纪委就没有权利批给我了,我就要去找米老师,这样这个请求就会一层一层的被传递下去,知道遇到有权限处理我这个问题的人。再说说其中的解耦作用,比如在公司中,有上千个人,我需要办一件事情,向离我最近的一个部门提出一个请求,之后,这个请求就会被传递下去知道解决为止,而这个”我“,是不知道最终问题是被哪一个部门所解决的,这就达到了解耦的目的。
Dim cardlineinfo As New Entity.LineRecordEntity cardlineinfo.Logouttime = TimeOfDay.ToShortTimeString '给上机记录实体附上下机时间 cardlineinfo.Logintime = txtOnlinetime.Text.ToString Dim logouttime As String = cardlineinfo.Logouttime '定义变量存放下机时间 Dim offFacade As New Facade.OnlinetimeFacade '调用外观层,获取消费时间 Dim basicdata As List(Of Entity.BasicData) Dim userbll As New BLL.UserBLL basicdata = userbll.getBasicList '得到基础数据 Dim consumetime As Integer = offFacade.costTime(basicdata, cardlineinfo) '调用外观,并入参数
Public Function costTime(ByVal basicdata As List(Of Entity.BasicData), lineinfo As Entity.LineRecordEntity) As Integer '实例化,通过构造函数,传递参数 Dim bPrepareTime As New PrepareTimeHandlerBLL(basicdata) Dim bLeastTime As New BLeastTimeHandlerBLL(basicdata) Dim bAddTime As New UnitTimeHandlerBLL(basicdata) bPrepareTime.setsuccessor(bLeastTime) '设置下一个继承者 bLeastTime.setsuccessor(bAddTime) 'Dim line As New Entity.LineRecordEntity Dim time As Integer '计算实际在线时间 time = DateDiff("n", lineinfo.Logintime, lineinfo.Logouttime) Return bPrepareTime.handleTime(time) End Function
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
B-PrepareTimeHandlerBLL:准备时间
Public Class PrepareTimeHandlerBLL : Inherits TimeHandlerBLL Dim preparetime As Integer Public Sub New(ByVal EnBasicData As List(Of Entity.BasicData)) '构造函数,传入准备时间值 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
B-LeastTimeHandlerBLL至少上机时间:
Public Class BLeastTimeHandlerBLL : Inherits TimeHandlerBLL '至少上机时间处理 Private leasttime As Integer Public Sub New(ByVal basicdata As List(Of Entity.BasicData)) Me.leasttime = CInt(basicdata(0).Limitedtime) '将基础数据赋给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
UnitTimeHandlerBLL
Public Class UnitTimeHandlerBLL : Inherits TimeHandlerBLL '单位时间计算 Private unittime As Integer Public Sub New(ByVal basicdata As List(Of Entity.BasicData)) Me.unittime = CInt(basicdata(0).Addtime) End Sub Public Overrides Function handleTime(time As Integer) As Integer Return Math.Abs(Int(-time / unittime)) * unittime End Function End Class
完成这一功能,更多的是借鉴大神们的博客,因为在敲上机的时候,我是自己敲得,深知不用设计模式的痛苦,而下机需要调用的数据又非常的多,逻辑判断算的上整个机房系统中最复杂的一部分,所以就看了很多很多师哥师姐们的博客,虽然少了一些自己对这个模式如何应用到机房中,和应该应用到机房中的哪个部分少了些思考,但是在借鉴的过程中,对这一个设计模式由最开始的模模糊糊,到现在的实际应用,也学到了不少的东西。