职责链模式应用——下机(机房重构知识点总结)
??? 下機涉及兩個方面,消費時間和消費金額。
對消費時間的處理用的是職責鏈模式,感覺這個模式用的非常妙,參考的師哥的博客:《機房收費下機中用到的策略與職責鏈解析》;消費金額的處理用策略模式。針對不同的用戶類型。
??? 這里著重介紹職責鏈的應用。
??? 依據需求,將時間分為三個階段,準備時間:不收取費用;至少上機時間:大于準備時間,小于至少上機時間的,一律按至少上機時間算。單位遞增時間:大于至少上機時間后。按單位遞增時間累加。
?
TimeHandler類,定義一個處理請示的接口
Public MustInherit Class TimeHandlerBLLProtected successor As TimeHandlerBLLPublic Sub SetSuccessor(ByVal successor As TimeHandlerBLL) '設置繼承者Me.successor = successorEnd SubPublic MustOverride Function HandleTime(ByVal time As Integer) As Integer '處理請求的抽象方法End Class?
PrepareTimeHandler類。準備時間處理者,處理時間請求,假設能夠處理就處理,否則把請求轉給繼承者
<pre class="vb" name="code">Public Class PrepareTimeHandlerBLL : Inherits TimeHandlerBLLDim preparetime As IntegerPublic Sub New(ByVal esBasicData As List(Of Entity.BasicDataEntity)) '構造函數。傳入準備時間的值Me.preparetime = CInt(esBasicData(0).PrepareTime)End SubPublic Overrides Function HandleTime(time As Integer) As Integer '重寫父類函數If time <= preparetime Then '假設上機時間小于準備時間,返回0Return 0ElseReturn successor.HandleTime(time) '否則轉到下一位繼承者End IfEnd Function End Class?
LeastTimeHandler類,同理
Public Class LeastTimeHandlerBLL : Inherits TimeHandlerBLLDim leasttime As IntegerPublic Sub New(ByVal esBasicData As List(Of Entity.BasicDataEntity)) '構造函數,傳遞至少上機時間Me.leasttime = CInt(esBasicData(0).LeastTime)End SubPublic Overrides Function HandleTime(time As Integer) As Integer '重寫父類函數If time <= leasttime ThenReturn leasttime '假設小于至少上機時間。返回至少上機時間ElseReturn successor.HandleTime(time) '轉到下一位End IfEnd Function End Class?
StepTimeHandler類。處理剩余的請求
Public Class StepTimeHandlerBLL : Inherits TimeHandlerBLLDim steptime As IntegerPublic Sub New(ByVal esBasicData As List(Of Entity.BasicDataEntity)) '構造函數,傳遞單位遞增時間Me.steptime = esBasicData(0).StepTimeEnd SubPublic Overrides Function HandleTime(time As Integer) As Integer '大于至少時間。返回實際消費時間Return Math.Abs(Int(-time / steptime)) * steptimeEnd Function End ClassMath.Abs(Int(-time / steptime)) * steptime 這個地方看不明確的,參見博客:《史上最簡潔的向上取整》
??? 由于我這些代碼寫在B層,須要從U層調用。而參數須要通過實體來傳遞,所以添加一個類,在B層實例化類,既達到傳實體的須要。又減少了U層與B層的耦合。
Public Class OnlineTimeBLL''' <summary>''' 上機時間處理------上下機''' </summary>''' <param name="esBasicData">BasicDataEntity實體</param>''' <param name="eOnlineRecord">OnlineRecordEntiry實體</param>''' <returns>OnlineRecordEntiry實體</returns>''' <remarks>劉曉春 2014年6月26日</remarks>Public Function CostTime(ByVal esBasicData As List(Of Entity.BasicDataEntity), eOnlineRecord As Entity.OnlineRecordEntiry)'實例化類,通過構造函數,傳遞參數Dim bPrepareTime As New BLL.PrepareTimeHandlerBLL(esBasicData)Dim bLeastTime As New BLL.LeastTimeHandlerBLL(esBasicData)Dim bStepTime As New BLL.StepTimeHandlerBLL(esBasicData)bPrepareTime.SetSuccessor(bLeastTime) '設置職責鏈繼承者bLeastTime.SetSuccessor(bStepTime)Dim time As Integer '計算上下機時間差time = DateDiff("n", eOnlineRecord.OnTime, eOnlineRecord.OffTime) + DateDiff("n", eOnlineRecord.OnDate, eOnlineRecord.OffDate)eOnlineRecord.CostTime = bPrepareTime.HandleTime(time) '職責鏈處理,返回消費時間Return eOnlineRecordEnd Function End Class??? DateDiff("n", eOnlineRecord.OnTime, eOnlineRecord.OffTime) + DateDiff("n", eOnlineRecord.OnDate, eOnlineRecord.OffDate)? 這里分別計算日期和時間的差,然后求和,經測試。小的時間減大的時間會得出負值。再和日期得出的值相加,準確可靠。
?
然后是U層調用的代碼:
Dim eOnlineRecord As New Entity.OnlineRecordEntiry '上機記錄實體,傳遞上下機時間eOnlineRecord.CardNo = esOnline(0).CardNoeOnlineRecord.OnDate = esOnline(0).OnDateeOnlineRecord.OnTime = esOnline(0).OnTimeeOnlineRecord.OffDate = Format(DateTime.Now, "yyyy/MM/dd")eOnlineRecord.OffTime = Format(DateTime.Now, "HH:mm:ss")Dim bOnlineTime As New BLL.OnlineTimeBLLeOnlineRecord = bOnlineTime.CostTime(esBasicData, eOnlineRecord) '調用函數。傳遞實體
??? 關于日期時間的格式。也須要我們特別注意,這里有篇博客不錯。大家有須要的能夠參考下:《VB.net中時間和日期的格式化》
?
??? 職責鏈的應用到此就結束了,初學設計模式。有什么不正確的地方,歡迎大家批評指正。
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/mengfanrong/p/5329763.html
總結
以上是生活随笔為你收集整理的职责链模式应用——下机(机房重构知识点总结)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 辞旧迎新的句子 辞旧迎新的感悟人生句子2
- 下一篇: sqlserver索引维护(重新组织生成