疑问1:在重构的过程中,我是无意中浏览到别人博客才知道职责链模式在重构中的应用,之前在敲的过程中怎么没有想起?而别人怎么就能想到此模式的应用?
引入正题:在机房下机的过程中,其中有一个计算消费时间的过程,按照之前的版本系统,是利用一系列的判断语句来计算,其实不然,加入职责链模式来解决此问题。
职责链模式(加薪非要老总批?):使多个对象都有机会处理请求,从而避免请求的发送者和接受者(处理者)之间的耦合关系,将这些对象连成一条链,而请求则沿着此链进行传递,直到有对象处理为止。
联系个人重构,在计算消费时间的过程中,要先后和准备时间,至少上机时间,递增时间比较,UI层获得的消费时间即“请求”,而准备时间,至少上机时间,递增时间则为一个个的接受对象,它们被封装成一个个的类,然后继承于抽象类,废话不多说,见类图。
对照设计模式那本书,除了代码语言由c#变成vb.net外,其它基本没有变化,所以代码还是很好写的,我在敲代码的过程中唯一与课本上不一样的是又引入了外观模式,减少U层的代码量,只需传递参数即可。
详见代码:
抽象类层OnlineTimeHandlerBLL层:
Imports Factory Imports Entity Imports [Interface] ''' <summary> ''' 抽象类,利用职责链模式,计算消费时间。如若时间计算上变方式,也只需加具体类便好。 ''' </summary> ''' <remarks></remarks> Public MustInherit Class OnlineTimeHandlerBLL '获得消费时间 'Protected onlineTime As Integer '设置继任者(上级) Property calculate As OnlineTimeHandlerBLL Public Sub SetNext(ByVal calculate As OnlineTimeHandlerBLL) Me.calculate = calculate End Sub '处理请求(消费时间) Public MustOverride Function Request(ByVal onlineTime As Integer) As Integer End Class具体类层:PrepareTimeHandlerBLL层:
Public Class PrepareTimeHandlerBLL : Inherits OnlineTimeHandlerBLL Private prepareTime As Integer '构造函数,传入准备时间,具体实现使用传入实体 Public Sub New(ByVal enBasicData As List(Of BasicDataEntity)) Me.prepareTime = CInt(enBasicData(0).prepareTime) End Sub Public Overrides Function Request(onlineTime As Integer) As Integer If onlineTime < prepareTime Then Return 0 End If Return calculate.Request(onlineTime) '转移到最少上机时间 End Function End ClassLeastTimeHandlerBLL层:
Public Class LeastTimeHandlerBLL : Inherits OnlineTimeHandlerBLL Private leastTime As Double Public Sub New(ByVal enBasicData As List(Of BasicDataEntity)) '构造函数初始化 Me.leastTime = CInt(enBasicData(0).leastTime) End Sub Public Overrides Function Request(onlineTime As Integer) As Integer If onlineTime <= leastTime Then Return leastTime End If Return calculate.Request(onlineTime) End Function End ClassUnitTimeHandlerBLL层:
Public Class unitTimeHandlerBLL : Inherits OnlineTimeHandlerBLL Private unitTime As Double Public Sub New(ByVal enBasicData As List(Of BasicDataEntity)) Me.unitTime = CInt(enBasicData(0).unitTime) End Sub Public Overrides Function Request(onlineTime As Integer) As Integer Return onlineTime End Function End Class外观层FacOnlineTimeHandler:
Public Class FacOnlineTimeHandler Public Function onlineTime(ByVal consumeTime As Integer) As Integer Dim enBasicData As New BasicDataEntity Dim mylist As New List(Of BasicDataEntity) Dim limitMoney As New BLL.SetBasicDataBLL() mylist = limitMoney.DataRecord(enBasicData) '返回基本数据表信息 Dim prepareTime As New BLL.PrepareTimeHandlerBLL(mylist) '设定基本数据 Dim leastTime As New BLL.LeastTimeHandlerBLL(mylist) Dim unitTime As New BLL.unitTimeHandlerBLL(mylist) prepareTime.SetNext(leastTime) '设置上级 leastTime.SetNext(unitTime) Return prepareTime.Request(consumeTime) End FunctionUI层:
'计算消费时间 Dim calculate As New Facade.FacOnlineTimeHandler() Dim consumeTime As Double mylistData = limitMoney.DataRecord(enBasicData) '从数据信息获取记录 Dim consumeTime1 As Integer = DateDiff("n",CDate(txtOnDate.Text),CDate(txtOffDate.Text)) + DateDiff("n",CDate(txtOnTime.Text),CDate(txtOffTime.Text)) consumeTime = calculate.onlineTime(consumeTime1) '向外观传递参数"消费时间",接收返回值存在consumeTime consumeTime = FormatNumber(consumeTime / mylistData(0).unitTime,1)大致内容就是这些,我认为虽然加入设计模式貌似看起来反而更复杂,但如果加入处理消费时间的参数,则只需要加入具体处理类即可,其次,在学完设计模式后这也是一个很好的实践机会,反复的过程,强化的过程。 原文链接:/vb/257489.html