Responsibility Chain Design Model-calculate the billing Validity Period

Source: Internet
Author: User
I. project: Data room Charging System
Ii. Analysis on the demand for offline checkout

YesSet the preparation time (minutes), minimum machine time (minutes), incremental time (minutes), fixed user fee of half an hour, and temporary user hourly fee. When a student goes off to check out, if the computer time is not billed during the preparation time, if the computer time is later than the computer time and less than the minimum computer time, the minimum computer time will be charged, if the host time is later than the minimum host time, fees are charged in an incremental period.

A. Problem Solving Analysis


 Obtain the Time of the student's computer based on the login time and the next time, and determine the computer time. If the computer time is not billed during the preparation time, if the host time is later than the host time and less than the minimum host time, the minimum host time is charged. If the host time is later than the minimum host time, the incremental time is charged by phase. the key to solving the charge problem is to get the effective time of the calculation, and then calculate the user's consumption amount on the computer by the user type by the effective time of the billing amount within the unit time.

B. Purpose of the function module


 Obtain the valid time of the student's computer to calculate the amount consumed on the computer.

Iii. Implementation without a design pattern A. Principle:

Use if... Else... The statement compares the difference between the student login time and the logout time (onlinetime) with the system-set preparation time (preparedtime), at least the onlinetimeatleast, and onlinetimeincrease. If onlinetime is between 0-preparedtime, the effective time on the current machine is 0, that is, no charge; onlinetime is between preparedtime and onlinetimeatleast, the effective time on the current machine is at least the time set by the system (onlinetimeatleast ), that is, the onlinetimeatleast is charged for the current time on the machine. If onlinetime is later than the minimum time on the machine, the effective time on the machine is onlinetime.

B. Code implementation: Calculate the effective machine time implementation class:

''' <Summary> ''' Calculation on-machine consumption effective time implementation class ''' </Summary> public class consumetimehandler private preparedtime as integer 'On-machine preparation time private timeonlineleast as integer' Minimum machine Time private timeincrease as integer increase time 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> ''' Calculation on-board effective time ''' </Summary> ''' <Param name = "onlinetime"> On-board time </ 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. Test cases:

Public class form2 private sub button#click (byval sender as system. object, byval e as system. eventargs) handles button1.click dim A as new consumetimehandler (5, 10, 30) 'create a consumetimehandler object instance and initialize the member variable msgbox (. consumetime (textbox1.text) 'textbox1 enter the current time on the machine, and the end subend class is displayed after calculation.

IV. Implementation of the responsibility chain model

A. Principle:

An abstract class can be abstracted to obtain the effective time of a student's computer. Its role is to calculate the effective time onlinetimehandler of the student's computer. According to the requirements, it can be seen that the validity period needs to be determined three times (judge the relationship between the machine time and the preparation time, at least the Machine Time, And the incremental time ), in this way, we can abstract three specific classes: preparedtimehandler, onlinetimeleasthandler, and timeincreasehandler to compare the machine time with the preparation time, at least the Machine Time, and incremental time, and inherit the onlinetimehandler class.

B. Class diagram design:

C. Code implementation: onlinetimehandler abstract class
''' <Summary> ''' on-board time processing class, processing the machine time is used to calculate the machine consumption ''' </Summary> Public mustinherit class onlinetimehandlerprotected calculate as onlinetimehandler ''' <summary> '''machine time processing' </Summary> ''' <Param name = "onlinetime"> host time, in minutes </param> Public mustoverride function request (byval onlinetime as integer) as integer ''' <summary> ''' sets the calculate successor ''' </Summary> ''' <Param name = "Calculate"> machine time processing </param> public overridable sub setcalculate (byval calculate as onlinetimehandler) end sub end class 'onlinetimehandler

Preparedtimehandler class:

Public class preparedtimehandlerinherits charge. chargebll. onlinetimehandler private preparedtime as integer 'machine preparation time public sub new () end sub public sub new (byval preparedtime as integer) me. preparedtime = preparedtime end sub ''' <Param name = "onlinetime"> host time, in minutes </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> ''' sets the calculate successor ''' </Summary> ''' <Param name = "Calculate"> machine time processing </ param> ''' public overrides sub setcalculate (byval calculate as onlinetimehandler) me. calculate = calculate end subend class 'preparedtimehandler
Onlinetimeleasthandler class:
''' <Summary> ''' least time processing class ''' </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"> host time, in minutes </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> ''' sets the calculate successor ''' </Summary> ''' <Param name = "Calculate"> machine time processing </Param> Public overrides sub setcalculate (byval calculate as onlinetimehandler) me. calculate = calculateend subend class 'onlinetimeleasthandler

Timeincreasehandler class:

''' <Summary> '''machine time growth processing ''' </Summary> public class timeincreasehandler inherits charge. chargebll. onlinetimehandler private timeincrease as integer 'increment time public sub new () end sub public sub new (byval timeincrease as integer) me. timeincrease = timeincrease end sub ''' <Param name = "onlinetime"> host time, in minutes </param> Public overrides function request (byval onlinetime as integer) as integer return onlinetime end function ''' <summary> ''' sets the calculate successor ''' </Summary> ''' <Param name = "Calculate"> On-board Time Processing </param> Public overrides sub setcalculate (byval calculate as onlinetimehandler) me. calculate = calculate end sub end class 'timeincreasehandler

D. Test Cases

Public class form1 private sub button#click (byval sender as system. object, byval e as system. eventargs) handles button1.click dim A as new onlinetimeleasthandler (10) 'creates an object that processes at least the machine time and initializes dim B as new preparedtimehandler (5) 'create processing preparation time object and initialize preparation time dim C as new timeincreasehandler (35) 'create processing incremental time and present incremental time B. setcalculate (a) 'sets successor. setcalculate (c) 'sets the successor msgbox (B. request (textbox1.text) 'textbox1 data is the machine time, end subend class

V. Summary:

Benefits of the responsibility chain: When a request is submitted, the request is transmitted along the chain until an object is responsible for processing it, so that the request recipient and the sender do not have the explicit information of the other party, the objects in the chain do not know the structure of the chain. The result is that the responsibility chain can simplify the mutual connection between objects. They only need to maintain a reference pointing to their successor, rather than retaining the reference of all their candidate recipients, greatly reducing the coupling degree. The application of the responsibility chain model can add or modify the structure of a request at any time. This enhances the flexibility of assigning roles to objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.