前几天一直在讲设计模式,在和师哥师姐的讨论过程中,发现能在机房收费个人重构版中用到好几个设计模式,首先来讨论讨论职责链模式:
然后就是三个子类对于父类的继承:
首先当看见这个模式的第一眼就觉得这是一个很简单的模式,可是当使用起来真的得考虑许多,首先要明白什么是链?链是一系列节点的集合,可以灵活拆分再重组。这也是与 链表不同的地方,用户可以去访问节点中的任何一点作为开始节点。
定义:
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止。
举例:
现在正在敲个人重构版机房收费系统,在下机的过程中,我们需要和基本数据的设定联系起来,即上机时间是分为三段来考虑的,准备时间,至少上机时间,单位递增时间,在判断处理的时候他则是一次来进行的,这不正与职责链模式不谋而合了吗?参照职责链中涨工资的小故事,我们可以把分成三个类,准备时间,至少上机时间和单位时间
看类图:
看相关代码:
首先得需要一个抽象类,处理请求的一个接口:
- '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
'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
然后就是三个子类对于父类的继承:
- '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
'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
- '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
'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
- '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
'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
至此职责链模式就到此结束了,为了便于调用,可以增加一个接口,方便调用,这样就只需要传入两个参数就可了。- 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
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
- 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)
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)
分类:
这样在自己的个人机房重构中就完整的对职责链模式熟悉了一遍,对于职责链的学习,也只是处于一个初级阶段,这也纯属属于“纯的职责链模式(要么承担责任,要么将责任全部推给下一家”,而且要求这个请求在过程结束之前必须能够被一个处理者对象所接收;除此之外还有对应的“不纯的职责链模式(允许某一个请求允许某个请求被一个具体处理者部分处理后再向下传递,或者一个具体处理者处理完某请求后其后继处理者可以继续处理该请求,而且一个请求可以最终不被任何处理者对象所接收)”未完