Analysis of policies and responsibility chains used in data center fees and downloading

Source: Internet
Author: User

In cooperative development, there are some algorithms and models that are responsible for business logic. The following describes the basic operations on the machine and the two design modes: responsibility chain and strategy.

 

Responsibility chain:

 

Is to allow multiple objects to process requests. This avoids the coupling between request senders and receivers.

Link this object into a chain. Send the request along the chain. Until an object is processed.

 

Policy:

 

Defines a set of algorithms. So that they can follow each other. This mode changes the algorithm and does not affect the customers who use the algorithm.

 

The policy mode uses different charges for fixed users and temporary users.

 

 

The first step is to define an abstract base class that contains the common interfaces of all supported algorithms.

Subclass specific policy class encapsulates the behavior of algorithms and inherits from the base class

Cashcontext references abstract classes and obtains their consumption details.

 

See the following example

 

Abstract class:

 

Contains abstract algorithms, allowing sub-classes to implement algorithms

''' <Summary> ''' check-out class to handle machine consumption problems, abstract class ''' </Summary> publicmustinheritclasscashierbll''' <summary> ''' calculates the consumption amount based on the machine time and card type. (abstract method) ''' </Summary> ''' <Param name = "onlinetime"> host time, in minutes </param> ''' publicmustoverridefunction getconsumemoney (byval onlinetime asinteger) assingleendclass'

 

Fixed user class:

 

Contains constructor methods to initialize basic fees for fixed users

''' <Summary> ''' fixed user consumption amount processing ''' </Summary> publicclassregularbll inherits charge. chargebll. cashierbll private moneyregularhourassingle 'defines the hourly fee of a fixed user ''' <summary> ''' Based on the machine time, calculate the consumption amount ''' </Summary> ''' <Param name = "onlinetime">, the time unit is minute </param> when getconsumemoney (byval onlinetimeasinteger) assingle dim consumeassingle 'define the consumption amount' calculates the consumption amount consume = csng (onlinetime) * csng) return consume endfunction ''' <summary> ''' constructor initializes fixed user charges ''' </Summary> publicsubnew (moneyregularhourassingle) me. moneyregularhour = moneyregularhour endsub endclass 'regularbll

 

Temporary user class:

 

''' <Summary> ''' temporary user consumption amount processing ''' </Summary> publicclasstemporarybll inherits charge. chargebll. cashierbll private moneytemporaryhalfhourassingle 'temporary user's half-hour fee ''' <summary> ''' Based on the host time, calculate the consumption amount of a temporary user ''' </Summary> ''' <Param name = "onlinetime"> host time, in minutes </param> publicoverridesfunction getconsumemoney (byval onlinetimeasinteger) assingle dim consumeassingle 'define the consumption amount' calculate the consumption amount consume = csng (onlinetime) * csng (moneytemporaryhalfhour/30) return consume endfunction ''' <summary> ''' constructor ''' </Summary> publicsubnew (moneytemporaryhalfhourassingle) me. moneytemporaryhalfhour = moneytemporaryhalfhour endsub endclass 'temporarybll

 

Reference of the context class

 

References to abstract classes

''' <Summary> '''consumption amount configuration class ''' </Summary> publicclasscashcontextbll ''' <summary> '''consumption amount processing ''' </Summary> private cashierascashierbll public m_cashierascashierbll ''' <summary> ''' based on the specific cashier object, '''' </Summary> ''' <Param name = "onlinetime"> Method for calling the algorithm for calculating the consumption amount, the time unit is minute </param> publicfunction getresult (byval onlinetimeasinteger) assing'. Call the billing method of consumption Processing class computing return cashier. getconsumemoney (onlinetime) endfunction ''' <summary> ''' constructor for accepting card types ''' </Summary> ''' <Param name = "cardType"> card type, fixed or temporary users </param> publicsubnew (byval cardType asstring) dim basicbllasnewbasicdatamanagerbll 'define basic query operation class dim basicentityasnewbasicdataentity basicentity = basicbll. getbasicdata () 'obtain basic information data' SELECT statement to determine the input card type, 'automatically create the consumption type selectcase cardType case "fixed user" cashier = newregularbll (basicentity. moneyforregularperhour) 'create a fixed user billing type case "temporary user" cashier = newtemporarybll (basicentity. moneyfortemporaryhalfhour) 'create a temporary user billing type case else cardType = nothing endselect endsub endclass' cashcontextbll

 

At last, the client only needs to call the context to know the specific consumption situation and achieve a good decoupling effect.

 

 

Responsibility Chain Model

 

Here we use the time judgment.

For students on the machine, the preparation time, minimum time on the machine, and incremental events are required.

No charge is collected during the preparation time, and the minimum time is used for the fee. If the time exceeds the minimum time, fees are charged based on the incremental time.

The responsibility chain first defines a base class to ensure that only one request interface is processed.

Then the subclass is a specific handler class that processes the requests it is responsible for, and can access its sequeners. How can we process the requests, otherwise, the request will be forwarded to subsequent successors.

 

 

The base class contains virtual successors and virtual processing methods. To allow the subclass to redefine the algorithm.

See the specific example below

 

Base Class (abstract class)

 

''' <Summary> ''' on-board time processing class, processing Machine Time is used to calculate machine consumption ''' </Summary> publicmustinheritclassonlinetimehandlerbll' <summary> ''' machine time processing ''' </Summary> protected calculateasonlinetimehandlerbll '''' ''<Param name =" onlinetime "> host time, in minutes </param> publicmustoverridefunction request (byval onlinetimeasinteger) asinteger ''' <summary> ''' sets the calculate successor ''' </Summary> ''' <Param name = "Calculate"> machine time processing </param> publicoverridablesub setcalculate (byval calculateasonlinetimehandlerbll) endsubendclass 'onlinetimehandlerbll

 

The following are the successors and handlers.

 

Prepared:

 

Constructor: Initialize private preparation time

''' <Summary> ''' machine preparation time ''' </Summary> publicclasspreparedtimehandlerbll inherits charge. chargebll. onlinetimehandlerbll ''' <summary> ''' preparation time ''' </Summary> private preparedtimeaslong ''' <Param name = "onlinetime"> host time, in minutes </param> publicoverridesfunction request (byval onlinetimeasinteger) asinteger 'if function checks whether the time on the machine is within the processing range and does not go to the next processing class, 0 else returnme is returned when 0 if onlinetime <= preparedtimetatereturn 0' is returned. calculate. request (onlinetime) endif endfunction ''' <summary> ''' sets the calculate successor ''' </Summary> ''' <Param name = "Calculate"> machine time processing </param> publicoverridessub setcalculate (byval calculateasonlinetimehandlerbll) me. calculate = calculate 'set calculate's successor endsub ''' <summary> ''' Construction Method for initializing member variables ''' </Summary> ''' <Param name = "preparedtime "> preparation time </param> publicsubnew (byval preparedtime aslong) me. the preparedtime = preparedtime 'constructor assigns an endsub endclass 'preparedtimehandlerbll value to the private time variable.

 

Minimum Time handler:

 

There are also constructor methods, the minimum time for private Initialization

 

''' <Summary> ''' least time processing ''' </Summary> publicclassonlinetimeleasthandlerbll inherits charge. chargebll. onlinetimehandlerbll ''' <summary> ''' least computer time ''' </Summary> private onlinetimeleastaslong''' <summary> ''' machine time, in minutes, ''' </Summary> ''' <Param name = "onlinetime"> </param> ''' <returns> </returns> ''' <remarks> </remarks> publicoverridesfunction request (byval onlinetimeasinteger) asinteger 'if statement determines whether the machine time is within the minimum time. If so, onlinetimeleast is returned. Otherwise, it is transferred to the next bit to process if onlinetime <onlinetimeleastthen returncint (onlinetimeleast) 'less than the minimum time, returns the minimum machine time else greater than the minimum time, and forwards it to the next processing returnme. calculate. request (onlinetime) endif endfunction ''' <summary> ''' sets the calculate successor ''' </Summary> ''' <Param name = "Calculate"> machine time processing </param> publicoverridessub setcalculate (byval calculateasonlinetimehandlerbll) me. calculate = calculate 'sets the successor endsub ''' <summary> ''' to initialize the least-time construction method ''' </Summary> ''' <Param name = "onlinetimeleast"> minimum onboard time </param> publicsubnew (byval onlinetimeleast aslong) me. onlinetimeleast = onlinetimeleast 'The minimum initialization time on the machine endsub endclass' onlinetimeleasthandlerbll

The last is the incremental time handler class:

 

''' <Summary> ''' machine time growth processing ''' </Summary> publicclasstimeincreasehandlerbll inherits charge. chargebll. onlinetimehandlerbll ''' <summary> ''' machine growth time''' </Summary> private timeincreaseaslong ''' <summary> ''' Method for initializing machine growth time'' '</Summary> ''' <Param name = "timeincrease"> machine growth time </param> publicsubnew (byval timeincrease aslong) me. timeincrease = timeincrease 'initialization increase time on the machine endsub ''' <Param name = "onlinetime"> machine time, in minutes </param> publicoverridesfunction request (byval onlinetimeasinteger) asinteger return onlinetime' returns the host time endfunction ''' <summary> ''' sets the successor of calculate ''' </Summary> ''' <Param name = "Calculate"> time Processing </param> publicoverridessub setcalculate (byval calculateasonlinetimehandlerbll) me. calculate = calculate 'set the successor endsub endclass' timeincreasehandlerbll

 

 

Finally, set the successor sequence between calls and the initial caller of the call.

'Call the Basic Data Query Method

 

Basicentity = basicdatamanagerbll. getbasicdata ()

'Create preparation time and pay the preparation time

Dim preparetimeasnewpreparedtimehandlerbll (basicentity. timeprepared)

Dim listtimeasnewonlinetimeleasthandlerbll (basicentity. timeonlineleast)

'Create incremental time and pay the incremental time

Dim increasetimeasnewtimeincreasehandlerbll (basicentity. timeincrease)

'Set the successor

Preparetime. setcalculate (listtime )'

Listtime. setcalculate (increasetime )'

'Preparetime. Request ()

Onlinerecord. getontime = cdate (dtcard. Rows (0). Item ("logintime"). tostring) 'Get the consumption time

 

Finally, let's take a look at the specific

 

Offline Method

 

 

''' <Summary> ''' indicates the method for removing a student from the machine by using a certain card number, whether the comment is normal or forced to shut down ''' </Summary> ''' <Param name = "comment"> force or normal to shut down ''' </param> ''' <Param name = "card"> recharge card information </param> publicfunction logout (byval commentasstring, byval entity) asboolean dim entity (newcardentity) dim costtimeasinteger dim consumeassingle dim balanceassingle dim entity cardisfound = entity. validate (card) 'Call to query whether the card number has a method 'If statement to determine whether the card number exists, true exists, false does not exist if cardisfound = falset0000' the card number does not exist prompt thrownewexception ("this card is not registered, please re-enter the card number ") exit function endif onisfound = isonline (card) 'to call the query method 'if statement to determine whether the card is online if onisfound = falsett' the card is not online, thrownewexception ("the card is not online and cannot be down again") exit function endif dtcard = onlinerecordmanagerbll. queryonlineinfo (card) 'query students' information on boarding 'Call the basic data query method basicentity = basicdatamanagerbll. getbasicdata () 'create the preparation time and pay the preparation time dim preparetimeasnewpreparedtimehandlerbll (basicentity. timeprepared) dim listtimeasnewonlinetimeleasthandlerbll (basicentity. timeonlineleast) 'create incremental time and pay the incremental time dim increasetimeasnewtimeincreasehandlerbll (basicentity. timeincrease) 'sets the successor preparetime. setcalculate (listtime) 'is set and the successor is listtime. setcalculate (increasetime) 'sets the successor 'preparetime. request () onlinerecord. getontime = cdate (dtcard. rows (0 ). item ("logintime "). tostring) 'Get the consumption time onlinerecord. getdowntime = cdate (timeofday. tostring) costtime = CINT (datediff ("N", format (onlinerecord. getontime, "HH: mm: SS"), timeofday) 'convert to get consumption time' call the initial receiver costtime = preparetime. request (costtime) card. cardType = dtcard. rows (0 ). item (9 ). tostring 'Incoming card type' refers to the object in call policy mode. The incoming user type is dim conteshascashcontextbll = newcashcontextbll (card. cardType) 'Call the billing amount algorithm of the policy' insert parameters for the responsibility chain return value consume = contesh. getresult (costtime) balance = csng (dtcard. rows (0 ). item ("balance "). tostring)-csng (consume) 'calculates the balance card. balance = balance 'Update balance cardmanagerbll. recharge (consume, card) 'Call the recharge method to update the balance 'object Value onlinerecord. comment = comment 'force down or normal down onlinerecord. getdowndate = now onlinerecord. getdowntime = now onlinerecord. isonline = trim ("no") onlinerecord. ischeckout = "no" 'Whether to check onlinerecord. consumemoney = consume 'consumption amount onlinerecord. student. card. cardNumber = card. cardNumber 'dismounting card number' calls the update method of the dismounting record. If statements are used to determine whether the dismounting is successful, true is successful, and false is not used to determine whether outisfound = onlinerecordmanagerbll is successful. getdownregist (card, onlinerecord) If outisfound = truetaskreturntrue else returnfalse 'Throw new exception ("failed to get down, please contact the Administrator") endif endfunction

 

 

The basic order is over. Please confirm.

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.