VB.NET & 职责链模式 (下机时间操作)

前几天一直在讲设计模式,在和师哥师姐的讨论过程中,发现能在机房收费个人重构版中用到好几个设计模式,首先来讨论讨论职责链模式:

     首先当看见这个模式的第一眼就觉得这是一个很简单的模式,可是当使用起来真的得考虑许多,首先要明白什么是链?链是一系列节点的集合,可以灵活拆分再重组。这也是与 链表不同的地方,用户可以去访问节点中的任何一点作为开始节点。

定义:

     使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止。

举例:

     现在正在敲个人重构版机房收费系统,在下机的过程中,我们需要和基本数据的设定联系起来,即上机时间是分为三段来考虑的,准备时间,至少上机时间,单位递增时间,在判断处理的时候他则是一次来进行的,这不正与职责链模式不谋而合了吗?参照职责链中涨工资的小故事,我们可以把分成三个类,准备时间,至少上机时间和单位时间

看类图:


看相关代码:

首先得需要一个抽象类,处理请求的一个接口:

  1. 'BL_TimeHandler,抽象类,定义一个处理请求的接口 
  2. Public MustInherit Class BL_TimeHandler 
  3.  
  4.     Protected calaulate As BL_TimeHandler 
  5.  
  6.     Public Sub Setcalaulate(ByVal calaulate As BL_TimeHandler)  '设置继承者 
  7.         Me.calaulate = calaulate 
  8.     End Sub 
  9.     '处理请求的抽象方法 
  10.     Public MustOverride Function HandleTime(ByVal time As Integer) As Integer 
  11.  
  12. End Class 
'BL_TimeHandler,抽象类,定义一个处理请求的接口
Public MustInherit Class BL_TimeHandler

    Protected calaulate As BL_TimeHandler

    Public Sub Setcalaulate(ByVal calaulate As BL_TimeHandler)  '设置继承者
        Me.calaulate = calaulate
    End Sub
    '处理请求的抽象方法
    Public MustOverride Function HandleTime(ByVal time As Integer) As Integer

End Class

然后就是三个子类对于父类的继承:

  1. 'BL_PrepareTimeHandler,准备时间处理类,继承BL_TimeHandler 
  2. Public Class BL_PrepareTimeHandler : Inherits BL_TimeHandler 
  3.     Dim preparetime As Integer 
  4.     '构造函数,利用泛型集合传入准备时间的值 
  5.     Public Sub New(ByVal EnBasicdata As IList(Of Entity.BasicDataEntity)) 
  6.         Me.preparetime = CInt(EnBasicdata(0).prepareTime)  '返回表达式 
  7.     End Sub 
  8.  
  9.     Public Overrides Function HandleTime(time As Integer) As Integer 
  10.         If time <= preparetime Then   '如果上机时间<准备时间,返回0 
  11.             Return
  12.         Else 
  13.             Return calaulate.HandleTime(time)    '转到下一位继承者 
  14.         End If 
  15.     End Function 
  16. End Class 
'BL_PrepareTimeHandler,准备时间处理类,继承BL_TimeHandler
Public Class BL_PrepareTimeHandler : Inherits BL_TimeHandler
    Dim preparetime As Integer
    '构造函数,利用泛型集合传入准备时间的值
    Public Sub New(ByVal EnBasicdata As IList(Of Entity.BasicDataEntity))
        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 calaulate.HandleTime(time)    '转到下一位继承者
        End If
    End Function
End Class
  1. 'BL_unitTimeHandler,单位时间处理类 
  2. Public Class BL_unitTimeHandler : Inherits BL_TimeHandler 
  3.     Private unittime As Integer 
  4.     '构造函数,传入递增时间 
  5.     Public Sub New(ByVal enBasicdata As IList(Of Entity.BasicDataEntity)) 
  6.         Me.unittime = CInt(enBasicdata(0).unitTime) 
  7.     End Sub 
  8.     '大于至少时间,返回实际消费时间 
  9.     Public Overrides Function HandleTime(time As Integer) As Integer 
  10.         Return Math.Abs(Int(-time / unittime)) * unittime 
  11.     End Function 
'BL_unitTimeHandler,单位时间处理类
Public Class BL_unitTimeHandler : Inherits BL_TimeHandler
    Private unittime As Integer
    '构造函数,传入递增时间
    Public Sub New(ByVal enBasicdata As IList(Of Entity.BasicDataEntity))
        Me.unittime = CInt(enBasicdata(0).unitTime)
    End Sub
    '大于至少时间,返回实际消费时间
    Public Overrides Function HandleTime(time As Integer) As Integer
        Return Math.Abs(Int(-time / unittime)) * unittime
    End Function
  1. 'BL_LeastTimeHandler,   至少上机时间类 
  2. Public Class BL_LeastTimeHandler : Inherits BL_TimeHandler 
  3.  
  4.     Private LeastTime As Integer 
  5.     '构造函数,传入最少上机时间的值 
  6.     Public Sub New(ByVal enBasicdata As IList(Of Entity.BasicDataEntity)) 
  7.         Me.LeastTime = CInt(enBasicdata(0).leastTime) 
  8.     End Sub 
  9.  
  10.     Public Overrides Function HandleTime(time As Integer) As Integer 
  11.         If time <= LeastTime Then 
  12.             Return LeastTime 
  13.         Else 
  14.             Return calaulate.HandleTime(time) 
  15.         End If 
  16.     End Function 
  17. End Class 
'BL_LeastTimeHandler,   至少上机时间类
Public Class BL_LeastTimeHandler : Inherits BL_TimeHandler

    Private LeastTime As Integer
    '构造函数,传入最少上机时间的值
    Public Sub New(ByVal enBasicdata As IList(Of Entity.BasicDataEntity))
        Me.LeastTime = CInt(enBasicdata(0).leastTime)
    End Sub

    Public Overrides Function HandleTime(time As Integer) As Integer
        If time <= LeastTime Then
            Return LeastTime
        Else
            Return calaulate.HandleTime(time)
        End If
    End Function
End Class
至此职责链模式就到此结束了,为了便于调用,可以增加一个接口,方便调用,这样就只需要传入两个参数就可了。

  1. Public Class BL_OnlineTimeCount 
  2.     Public Function CostTime(ByVal enbasicdata As IList(Of Entity.BasicDataEntity), enLineInfo As Entity.LineEntity) As Integer 
  3.         '实例化类,通过构造函数,传递函数 
  4.         Dim bPrepareTime As New BL_PrepareTimeHandler(enbasicdata) 
  5.         Dim bLeastTime As New BL_LeastTimeHandler(enbasicdata) 
  6.         Dim bStepTime As New BL_unitTimeHandler(enbasicdata) 
  7.  
  8.         bPrepareTime.Setcalaulate(bLeastTime)  '设置职责链继承者 
  9.         bLeastTime.Setcalaulate(bStepTime) 
  10.  
  11.         Dim time As Integer   '计算上下机时间差 
  12.         time = DateDiff("n", enLineInfo.Ontime, enLineInfo.Offtime) + DateDiff("n", enLineInfo.Ondate, enLineInfo.Offdate) 
  13.  
  14.         Return bPrepareTime.HandleTime(time)   '职责链处理,返回上机时间 
  15.     End Function 
  16.  
  17. End Class 
Public Class BL_OnlineTimeCount
    Public Function CostTime(ByVal enbasicdata As IList(Of Entity.BasicDataEntity), enLineInfo As Entity.LineEntity) As Integer
        '实例化类,通过构造函数,传递函数
        Dim bPrepareTime As New BL_PrepareTimeHandler(enbasicdata)
        Dim bLeastTime As New BL_LeastTimeHandler(enbasicdata)
        Dim bStepTime As New BL_unitTimeHandler(enbasicdata)

        bPrepareTime.Setcalaulate(bLeastTime)  '设置职责链继承者
        bLeastTime.Setcalaulate(bStepTime)

        Dim time As Integer   '计算上下机时间差
        time = DateDiff("n", enLineInfo.Ontime, enLineInfo.Offtime) + DateDiff("n", enLineInfo.Ondate, enLineInfo.Offdate)

        Return bPrepareTime.HandleTime(time)   '职责链处理,返回上机时间
    End Function

End Class


在U层中的调用代码:

  1. Dim enline As New Entity.LineEntity 
  2.         With enline 
  3.             .Ondate = lineRe(0).Ondate 
  4.             .Ontime = lineRe(0).Ontime 
  5.             .Offdate = CStr(Format(Now(), "yyyy-MM-dd")) 
  6.             .Offtime = CStr(Format(Now(), "HH:mm:ss")) 
  7.         End With 
  8.  
  9.         Dim basicdataList As IList(Of Entity.BasicDataEntity) 
  10.         Dim BLLBasicdata As BasicDataBLL = New BasicDataBLL() 
  11.         Dim enbasicdata As New Entity.BasicDataEntity 
  12.   '调用返回基本数据函数,返回实体集合 
  13.         basicdataList = BLLBasicdata.ReadBasic(enbasicdata) 
  14.  
  15.         '调用职责链模式,计算上机时间 
  16.         enline.consumeTime = onTimeCount.CostTime(basicdataList, enline) 
Dim enline As New Entity.LineEntity
        With enline
            .Ondate = lineRe(0).Ondate
            .Ontime = lineRe(0).Ontime
            .Offdate = CStr(Format(Now(), "yyyy-MM-dd"))
            .Offtime = CStr(Format(Now(), "HH:mm:ss"))
        End With

        Dim basicdataList As IList(Of Entity.BasicDataEntity)
        Dim BLLBasicdata As BasicDataBLL = New BasicDataBLL()
        Dim enbasicdata As New Entity.BasicDataEntity
  '调用返回基本数据函数,返回实体集合
        basicdataList = BLLBasicdata.ReadBasic(enbasicdata)

        '调用职责链模式,计算上机时间
        enline.consumeTime = onTimeCount.CostTime(basicdataList, enline)

分类:

     这样在自己的个人机房重构中就完整的对职责链模式熟悉了一遍,对于职责链的学习,也只是处于一个初级阶段,这也纯属属于“纯的职责链模式(要么承担责任,要么将责任全部推给下一家”,而且要求这个请求在过程结束之前必须能够被一个处理者对象所接收;除此之外还有对应的“不纯的职责链模式(允许某一个请求允许某个请求被一个具体处理者部分处理后再向下传递,或者一个具体处理者处理完某请求后其后继处理者可以继续处理该请求,而且一个请求可以最终不被任何处理者对象所接收)”未完

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值