In the computer room charge system, the students to checkout the most people headache. Because of the student's spending time (down time-machine time-on-machine preparation time) There are three kinds of situations:
1. Consumption time <=0, that is, the time between the next machine and the time of the machine in the machine preparation time range, this situation is not charged.
2. Consumption time <= minimum on-time, in this case, the consumption time in accordance with the minimum charge between the machine.
3. Consumption time > Minimum on-time, this situation also should consider increment unit time. For example, the increment unit time is 10 minutes, the consumption time is 42 points
clock, 42/10=4.....2, this The situation is subject to a 50-minute charge.
The above consumption time is divided into three cases, more complicated is the second and the third situation also need to consider whether the user type is fixed user or temporary use
users, both of which class rates are not the same. In the first two computer room charge system, I spent a lot of time and energy, the whole large number of nested
of the If statement to calculate consumption. If the statement is used more , the program has a bad taste. Now that we have learned the design pattern, we should apply it to practice.
The definition of a responsibility chain pattern is to allow multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and the recipient of the request. Put this
object into a chain, and pass the request along this chain until an object has processed it. In the machine room charge system under the checkout, "request" in
With user consumption time and user type, "at Management "is the different billing methods under three different consumption times. Requests are passed in these three processes,
when the consumption time conforms to which processing, it is billed in which processing method. The following shows the class diagram and the specific code:
Request:
<span style= "Font-family:microsoft yahei;font-size:18px;" >public Class Request ' <summary> ' card type ' </summary> Private cardtype as String ' <summary> ' consumption time, Unit division ' </summary> Private Consumetime as Integer ' <summary> ' card type ' </summary> public property Procardtype () as String Get Return cardtype End get Set (ByVal Value as String) Cardtype = Value End Set End Property ' <summary> ' consumption time, Unit division ' </summary> Public Property Proconsumetime () as Integer get Return consumetime End get Set (ByVal value as Integer) Consumetime = value End Set End Property End Class ' Request</span>
Chargemethod:
<span style= "Font-family:microsoft yahei;font-size:18px;" > ' <summary> ' abstract class, billing method ' </summary> public MustInherit class Chargemethod ' <summary > ' superior ' </summary> Protected Superior as Chargemethod public MustOverride Sub Setsuperior (ByVal Superior as Chargemethod) ' <summary> ' virtual method, subclass rewrite ' ' </summary> "<param name=" request "></param> public MustOverride Function requestapplication (ByVal request as Request) as Decimal End Class ' chargemethod</span>
Chargemethoda:
<span style= "Font-family:microsoft yahei;font-size:18px;" > ' <summary> ' no way to spend time ' </summary> public Class chargemethoda:inherits Pack.BLL.ChargeMethod ' <summary> ' billing method: In preparation for the machine time range of no money, consumption of 0 "</summary> " <param name= "Request" ></param> public Overrides Function requestapplication (ByVal request as request) As Decimal If (Request. Proconsumetime <= 0) then return 0 Else return Me.superior.RequestApplication (Request) End If End Function Public Overrides Sub Setsuperior (ByVal Superior as Chargemethod) Me.superior = Superior End Sub end Class ' Chargemethoda</span>
Chargemethodb:
<span style= "Font-family:microsoft yahei;font-size:18px;" > ' <summary> ' consumes no more than the minimum time on machine ' </summary> public Class chargemethodb:inherits Pack.BLL.ChargeMetho d ' <summary> ' billing Method II: Consumption time is less than at least the machine time, according to at least the machine time billing ' </summary> ' <param nam e= "Request" ></param> public Overrides Function requestapplication (ByVal request as request) as Decimal If (Request. Procardtype = "Fixed user" and request. Proconsumetime <= leasttime) then Return rate * LEASTTIME/60 ElseIf (Request. Procardtype = "Temporary user" and request. Proconsumetime <= leasttime) then return tmprate * LEASTTIME/60 Else return Me.superior.RequestApplication (Request) End If End Function public Overrides Sub Setsuperior (Byva L Superior as Chargemethod) Me.superior = Superior End Sub End Class ' chargemethodb</span>
CHARGEMETHODC:
<span style= "Font-family:microsoft yahei;font-size:18px;" > "<summary>" Billing method Three: The cost of spending time is greater than at least the computer time of the billing methods ' </summary> ' ' <param name= ' request "></param> Public Overrides Function requestapplication (ByVal request as request) as Decimal Di M unitcount as Integer ' consumption time contains the number of increments per hour If (consumetime/unittime = 0) Then Unitcount = Consum Etime/unittime Else unitcount = Fix (consumetime/unittime) + 1 End If I F (Request. Procardtype = "Fixed user" and request. Proconsumetime > Leasttime) then Return rate * Unitcount * UNITTIME/60 ElseIf (Request. Procardtype = "Temporary user" and request. Proconsumetime > Leasttime) then Return tmprate * unitcount * unittime/60 End If End Function public Overrides Sub Setsuperior (ByVal Superior as Chargemethod) Me.superior = Superior End Sub End Class ' chargemethodc</span>
when used, the code is as follows:
<span style= "Font-family:microsoft yahei;font-size:18px;" >dim A As new Pack.BLL.ChargeMethodA Dim b As New Pack.BLL.ChargeMethodB Dim c As New Pack.bll.chargemethodc
dim request as New Pack.BLL.Request Dim Consumecash as Decimal ' consumption amount ' Sets the way the chain is delivered, 1→ way 2→ Way 3 A.setsuperior (b) B.setsuperior (c) request. Procardtype = "Temporary user" request. Proconsumetime = "units: minutes Consumecash = a.requestapplication (Request) </span>
This has created a lot of classes, but the if are encapsulated, treat all students equally, do not have to consider what kind of treatment he belongs to, complex
of the billing process to the program itself silently judge, calculate. The most important thing to use the responsibility chain model is two points: one is to set it in advance for each specific processing
"Successor", which is the next object that the chain passes. The other is to judge the request in each specific process, can it be handled, or transferred to
it successor. In this case, there are two types of users, if there are three kinds, four kinds ... It? This time you can introduce a strategy model and a simple factory. Through this daring
Try the application of responsibility chain mode, and realize the importance of practice deeply.
Responsibility chain mode--machine room charge system checkout