一、項目:機房收費系統
二、下機結賬收費需求分析
可以設定上機準備時間(分鐘)、最少上機時間(分鐘)、遞增時間(分鐘)、固定使用者半小時費用、臨時使用者每小時費用。學生下機結賬時,如果上機時間在準備時間內這段時間不計費,如果上機時間大於上機時間小於最少上機時間則按最少上機時間收費,如果上機時間大於最少上機時間,則按遞增時間段收費。
a.問題解決分析
根據登陸時間和下機時間得到學生上機時間,判斷上機時間,如果上機時間在準備時間內這段時間不計費,如果上機時間大於上機時間小於最少上機時間則按最少上機時間收費,如果上機時間大於最少上機時間,則按遞增時間按階段收費.解決收費問題的關鍵是先得到計算收費的有效時間,然後按使用者類型用單位時間內收費金額乘有效時間來計算使用者本次上機消費金額。
b.功能模組實現目的
獲得學生上機的有效時間,以便計算本次上機消費金額。
三、不加設計模式實現 a.原理:
利用if…else…語句將學生登陸時間與登出時間的差(OnlineTime)與系統設定的準備時間(PreparedTime)、至少上機時間(OnlineTimeAtleast)、上機遞增時間(OnlineTimeIncrease)分別進行比較。如果OnlineTime介於0—PreparedTime之間則本次上機的有效時間為0,即不收費;OnlineTime介於PreparedTime與OnlineTimeAtleast之間則本次上機有效時間為系統設定的至少上機時間(OnlineTimeAtleast),即以OnlineTimeAtleast本次上機時間進行收費;如果OnlineTime大於至少上機時間,則本次上機有效時間為OnlineTime。
b.代碼實現:計算有效上機時間實作類別:
''' <summary> ''' 計算上機消費有效時間實作類別 ''' </summary>Public Class ConsumeTimeHandler Private preparedtime As Integer '上機準備時間 Private timeonlineleast As Integer '最少上機時間 Private timeincrease As Integer '遞增時間 Public Sub New(ByVal preparedtime As Integer, ByVal timeonlineleast As Integer, ByVal timeincrease As Integer) Me.preparedtime = preparedtime Me.timeincrease = timeincrease Me.timeonlineleast = timeonlineleast End Sub ''' <summary> ''' 計算上機有效時間 ''' </summary> ''' <param name="onlinetime">上機時間</param> ''' <returns></returns> Public Function ConsumeTime(ByVal onlinetime As Integer) As Integer If onlinetime < preparedtime Then Return 0 ElseIf onlinetime < timeonlineleast Then Return timeonlineleast Else Return onlinetime End If End FunctionEnd Class
c.用例測試:
Public Class Form2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a As New ConsumeTimeHandler(5, 10, 30) '建立ConsumeTimeHandler對象執行個體,並初始化準備時間、最少上機時間、遞增時間等成員變數 MsgBox(a.ConsumeTime(TextBox1.Text)) 'TextBox1中輸入本次上機時間,經過計算顯示收費的有效時間 End SubEnd Class
四、職責鏈模式實現
a.原理:
獲得上機有效時間可以抽象一個抽象類別,其職責是計算學生上機的有效時間OnlineTimeHandler。根據需求可以看出獲得有效時間需要經過三次判斷(判斷上機時間與準備時間、至少上機時間、遞增時間三者之間的關係),進而可以抽象出三個具體類PreparedTimeHandler、OnlineTimeLeastHandler、TimeIncreaseHandler,分別實現上機時間與準備時間、至少上機時間、遞增時間之間關係的對比,並繼承OnlineTimeHandler類。
b.類圖設計:
c.代碼實現:OnlineTimeHandler抽象類別
''' <summary>''' 上機時間處理類,處理上機時間用來計算上機消費''' </summary>Public MustInherit Class OnlineTimeHandlerProtected calculate As OnlineTimeHandler''' <summary>''' 上機時間處理''' </summary>''' <param name="onlinetime">上機時間,以分鐘為單位</param>Public MustOverride Function Request(ByVal onlinetime As Integer) As Integer''' <summary>''' 設定calculate的繼任者''' </summary>''' <param name="calculate">上機時間處理</param> Public Overridable Sub SetCalculate(ByVal calculate As OnlineTimeHandler) End Sub End Class ' OnlineTimeHandler
PreparedTimeHandler類:
Public Class PreparedTimeHandlerInherits Charge.ChargeBLL.OnlineTimeHandler Private preparedtime As Integer'上機準備時間 Public Sub New() End Sub Public Sub New(ByVal preparedtime As Integer) Me.preparedtime = preparedtime End Sub''' ''' <param name="onlinetime">上機時間,以分鐘為單位</param> ''' Public Overrides Function Request(ByVal onlinetime As Integer) As Integer If onlinetime <= preparedtime Then Return 0 Else Return Me.calculate.Request(onlinetime) End If End Function''' <summary>''' 設定calculate的繼任者''' </summary> ''' <param name="calculate">上機時間處理</param> ''' Public Overrides Sub SetCalculate(ByVal calculate As OnlineTimeHandler) Me.calculate = calculate End SubEnd Class ' PreparedTimeHandler
OnlineTimeLeastHandler類:
''' <summary>''' 最少上機時間處理類''' </summary>Public Class OnlineTimeLeastHandlerInherits Charge.ChargeBLL.OnlineTimeHandler Private leasttime As Integer Public Sub New() End Sub Public Sub New(ByVal leasttime As Integer) Me.leasttime = leasttime End Sub''' ''' <param name="onlinetime">上機時間,以分鐘為單位</param>Public Overrides Function Request(ByVal onlinetime As Integer) As Integer If onlinetime < leasttime Then Return 1 Else Return Me.calculate.Request(onlinetime) End IfEnd Function''' <summary>''' 設定calculate的繼任者''' </summary>''' <param name="calculate">上機時間處理</param>Public Overrides Sub SetCalculate(ByVal calculate As OnlineTimeHandler) Me.calculate = calculateEnd SubEnd Class ' OnlineTimeLeastHandler
TimeIncreaseHandler類:
''' <summary> ''' 上機時間增長處理 ''' </summary> Public Class TimeIncreaseHandler Inherits Charge.ChargeBLL.OnlineTimeHandler Private timeIncrease As Integer'遞增時間 Public Sub New() End Sub Public Sub New(ByVal timeIncrease As Integer) Me.timeIncrease = timeIncrease End Sub ''' ''' <param name="onlinetime">上機時間,以分鐘為單位</param> Public Overrides Function Request(ByVal onlinetime As Integer) As Integer Return onlinetime End Function ''' <summary> ''' 設定calculate的繼任者 ''' </summary> ''' <param name="calculate">上機時間處理</param> Public Overrides Sub SetCalculate(ByVal calculate As OnlineTimeHandler) Me.calculate = calculate End Sub End Class ' TimeIncreaseHandler
d.用例測試
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a As New OnlineTimeLeastHandler(10)’建立處理至少上機時間對象並初始化最少上機時間 Dim b As New PreparedTimeHandler(5)'建立處理準備時間對象並初始化準備上機時間 Dim c As New TimeIncreaseHandler(35)’建立處理遞增時間並出示化遞增時間 b.SetCalculate(a)'設定後繼者 a.SetCalculate(c)'設定後繼者 MsgBox(b.Request(TextBox1.Text))'TextBox1中資料為上機時間, End SubEnd Class
五、總結:
職責鏈的好處:當提交一個請求時,請求是沿著鏈傳遞直至有一個對象負責處理它,使得請求的接收者與寄件者都沒有對方的明確資訊,且鏈中的對象自己也不知道鏈的結構。結果是職責鏈可簡化對象的相互串連,他們僅需保持一個指向其後繼者的引用,而不需保持他所有的候選接收者的引用,大大降低了耦合度。職責鏈模式的應用可以隨時增加或修改處理一個請求的結構。增強了給對象指派職責的靈活性。