Reconstruction-multiple methods are used to remove students and reconstruct students
When designing the function of discontinuing students, I have read many blogs and think it is appropriate to use the responsibility chain model, we know that when a student goes off the machine to calculate the consumption balance, it is calculated based on the machine time. The preparation time and at least the machine time generate three situations. But although I thought of it, I didn't implement it in detail. I wrote an SQL function for the reason of laziness, in this way, when a student goes down, he can directly call this function at Layer D and directly return the consumption time, consumption amount, and balance. The function code is less and the implementation is good. This is the idea at the time, but the acceptance was not passed, so it was not feasible to be lazy. Let's make a good design.
First of all, what are the requirements for the function implementation? A card number is on the machine. This requires us to first verify whether the card number exists, has been canceled, or has not been on the machine, these steps clarify the next step, determine the consumption time --> the downtime time-the time on the machine, and check the charging method and perform the calculation. After understanding the requirements, the following lists the students' offline functions I have implemented using triggers + database functions and the responsibility chain mode + policy mode.
Triggers Enable Automatic Filling of downtime, automatic calculation of consumption time, automatic calculation of balance, and automatic update in the card table.
<Span style = "font-size: 24px;"> USE [MyDB] GO/****** Object: Trigger [dbo]. [t_linelog] Script Date: 02/13/2015 15:02:09 *****/SET ANSI_NULLS ONGOSETQUOTED_IDENTIFIER ONGOALTER trigger [dbo]. [t_linelog] on [dbo]. [Linelog] after insert, updateas -- declare the variable declare @ linelogidint, @ cardno varchar (10), @ consumetime int, @ consumecash intselect @ linelogid = id, @ cardno = CardNO frominsertedbegin -- update linelogif update (status) when the status changes -- update the offline time LinelogsetOffTime = GETDATE (), OffDate = GETDATE () where ID = @ linelogid -- calculate the consumption time update LinelogsetConsumeTime = DateDiff ("MINUTE", OnTime, OffTime) + DateDiff ("MINUTE", OnDate, OffDate) where ID = @ linelogid -- call the function and calculate the consumption amount. update LinelogsetConsumeCash = dbo. f_ConsumeCash (@ linelogid) where ID = @ linelogid -- update the balance in the card to update CardsetCash = card. cash-Linelog. consumeCashfrom Card left joinLinelog on Card. cardNO = Linelog. cardNO where ID = @ linelogid END </span>
Database Function compute consumption amount
<Span style = "font-size: 24px;"> USE [MyDB] GO/****** Object: UserDefinedFunction [dbo]. [f_ConsumeCash] Script Date: 02/12/2015 20:10:38 *****/SET ANSI_NULLS ONGOSETQUOTED_IDENTIFIER ONGOALTER function [dbo]. [f_ConsumeCash] (@ linelogid int) returns intasbegin -- declare the variable declare @ preparetimeint, @ leasttime int, @ unittime int, @ rate int, @ tmprateint, @ cardtypevarchar (10 ), @ consumecashint, @ cash int, @ consumetimeint, @ cardno varchar (10) -- assign the select @ preparetime = PrepareTime, @ leasttime = LeastTime, @ unittime = UnitTime, @ rate = Rate, @ tmprate = tmpRatefrom BasicDataselect @ cardtype = cardtype from Card where CardNO = @ cardnoselect @ consumetime = consumetime, @ cardno = CardNO fromLinelog where ID = @ linelogid -- determine the user category if @ cardtype = 'fixed user' set @ cash = @ rateelseset @ cash = @ tmprate -- calculate the consumption amount if @ consumetime <@ preparetimeset @ consumecash = 0 elseif @ consumetime <@ leasttimeset @ consumecash = @ leasttime * @ cashelseset @ consumecash = (@ consumetime/@ unittime) * @ cash -- return the consumption amount. return @ consumecashEndGO/****** Object: UserDefinedFunction [dbo]. [f_ConsumeCash] Script Date: 02/12/2015 20:10:38 *****/SET ANSI_NULLS ONGOSETQUOTED_IDENTIFIER ONGOALTER function [dbo]. [f_ConsumeCash] (@ linelogid int) returns intasbegin -- declare the variable declare @ preparetimeint, @ leasttime int, @ unittime int, @ rate int, @ tmprateint, @ cardtypevarchar (10 ), @ consumecashint, @ cash int, @ consumetimeint, @ cardno varchar (10) -- assign the select @ preparetime = PrepareTime, @ leasttime = LeastTime, @ unittime = UnitTime, @ rate = Rate, @ tmprate = tmpRatefrom BasicDataselect @ cardtype = cardtype from Card where CardNO = @ cardnoselect @ consumetime = consumetime, @ cardno = CardNO fromLinelog where ID = @ linelogid -- determine the user category if @ cardtype = 'fixed user' set @ cash = @ rateelseset @ cash = @ tmprate -- calculate the consumption amount if @ consumetime <@ preparetimeset @ consumecash = 0 elseif @ consumetime <@ leasttimeset @ consumecash = @ leasttime * @ cashelseset @ consumecash = (@ consumetime/@ unittime) * @ cash -- return the consumption amount return @ consumecashEnd </span>
The following is the responsibility chain + policy mode to implement the down-to-machine function. First, let's take a look at the structure of these two modes, which is a pattern structure that matches my code:
Rule mode structure
Responsibility Chain Model Structure
Responsibility Chain Mode calculates the consumption time
U-layer call code:
<Span style = "font-size: 24px;"> 'responsibility Chain Mode calculates the consumption time Dim ontimecount AsNew BLL. onLineTimeCountBLLlinelog. consumeTime = ontimecount. costTime (time, endatabasic: = BLL. dataBasicBLL. mydatabasic) </span>
(The time here is the specific time that students have experienced in the process from getting on to getting off the machine using the trigger in the database)
Calculate the time function and call the responsibility chain mode, which is similar to the appearance
<Span style = "font-size: 24px;"> Public ClassOnLineTimeCountBLL Public m_TimeHanderBLL As TimeHanderBLL ''' <summary> ''' process the responsibility chain, computer time Calculation ''' </summary> ''' <param name = "time"> Computer Recording entity </param> ''' <param name = "endatabasic"> basic data entity </param> Public Function CostTime (ByVal time AsInteger, byVal endatabasic As List (Of Entity. dataBasicEntity) As Integer Dim Bpreparetime As hour (endatabasic) Dim Bleasttime As NewLeastTimeHanderBLL (endatabasic) Dim Bunittime As NewUnitTimeHanderBLL (endatabasic) Bpreparetime. setSucessor (Bleasttime) Bleasttime. setSucessor (Bunittime) Return Bpreparetime. handleTime (time) End FunctionEnd Class </span>
At least Machine Time
<Span style = "font-size: 24px;"> Public ClassLeastTimeHanderBLL Inherits TimeHanderBLL Private leastTime As Integer ''' <summary> ''' determines whether the host time is earlier than the minimum host time, otherwise, go to the next '''' </summary> ''' <paramname = "time"> </param> Public Overrides Function HandleTime (ByValtime As Integer) as Integer If time <leastTime Then Return time Else Return successor. handleTime (time) End If End Function ''' <summary> ''' constructor, enter the value ''' </summary> ''' <paramname = "endatabasic"> </param> Public Sub New (ByVal endatabasic As List (OfEntity. dataBasicEntity) Me. leastTime = endatabasic. item (0 ). leastTime End SubEnd Class </span>
Preparation Time
<Span style = "font-size: 24px;"> Public ClassPrePareTimeHnaderBLL Inherits TimeHanderBLL Private Preparetime As Integer ''' <summary> ''' determines whether the computer time is earlier than the preparation time, otherwise, go to the next successor ''' </summary> ''' <paramname = "time"> time </param> Public Overrides Function HandleTime (ByValtime As Integer) as Integer If time <Preparetime Then Return time Else Return successor. handleTime (time) End If End Function ''' <summary> ''' constructor, input the value of the preparation time ''' </summary> ''' <paramname = "endatabasic"> </param> Public Sub New (ByVal endatabasic As List (OfEntity. dataBasicEntity) Me. preparetime = endatabasic. item (0 ). prepareTime End SubEnd Class </span>
Increment time
<Span style = "font-size: 24px;"> Public ClassUnitTimeHanderBLL Inherits TimeHanderBLL Private unitTime As Integer Public m_TimeHanderBLL As TimeHanderBLL ''' <summary> ''' is greater than or equal to the machine time, returns the actual consumption time ''' </summary> ''' <paramname = "time"> </param> Public Overrides Function HandleTime (ByValtime As Integer) As Integer Return Math. abs (Int (time/unitTime) + 1) End Function ''' <summary> ''' constructor, value of the unit increment time ''' </summary> ''' <paramname = "endatabasic"> </param> Public Sub New (ByVal endatabasic As List (OfEntity. dataBasicEntity) Me. unitTime = endatabasic. item (0 ). unitTime End SubEnd Class </span>
Calculation of consumption amount in Rule Mode
Context to reference a policy
<Span style = "font-size: 24px;"> Public ClassCashContextBLL Public myCashSuperBLL As CashSuperBLL ''' <summary> ''' calls the consumption amount calculation method of the abstract class, get the consumption amount ''' </summary> ''' <paramname = "Time"> </param> Public Function GetResult (ByVal Time AsInteger) As Integer Return myCashSuperBLL. costCash (Time) End Function ''' <summary> ''' using reflection, select the class ''' to be instantiated Based on the card number type </summary> ''' <paramname = "CardType"> card type </param> Public Sub New (ByVal CardType As String) dim strInstance As String = "BLL. cash "+ CardType +" BLL "myCashSuperBLL = CType (Assembly. load ("BLL "). createInstance (strInstance), CashSuperBLL) End SubEnd Class </span>
Specific settlement method 1
<Span style = "font-size: 24px;"> Public ClassCashFixBLL: inherits CashSuperBLL ''' <summary> ''' calculates the consumption amount of a fixed user ''' </summary> ''' <paramname = "Time"> </param> Public Overrides Function costCash (Time AsInteger) as Integer Dim databasic As New List (OfEntity. dataBasicEntity) Dim rate As Integer = BLL. dataBasicBLL. mydatabasic. item (0 ). rate Return rate * Time End FunctionEnd Class </span>
Specific settlement method 2
<Span style = "font-size: 24px;"> Public ClassCashTmpBLL: inherits CashSuperBLL ''' <summary> ''' calculate the consumption amount of a temporary user ''' </summary> ''' <paramname = "Time"> </param> Public Overrides Function costCash (Time AsInteger) as Integer Dim databasic As New List (OfEntity. dataBasicEntity) Dim Tmprate As Integer = BLL. dataBasicBLL. mydatabasic. item (0 ). tmpRate Return Tmprate * Time End FunctionEnd Class </span>
U layer call
'Based on the above time, the consumption amount is calculated using the Policy mode Dim CardType AsString Select Case showmessage. item (0 ). typeCase "fixed user" CardType = "Fix" Case "temporary user" CardType = "Tmp" Case ElseCardType = "" End select' instantiation class CashContextBLL, input user type Dim cashcontext As NewCashContextBLL (CardType) 'call policy mode to calculate the consumption amount and assign the value to the physical checkcard recorded on the computer. concash = cashcontext. getResult (linelog. consumeTime)
The Skillful Use of the design pattern will make programming easier. I hope this article will help you with your suggestions.