Fees are charged for downloading. Temporary users and students are charged for downloading. This gives me the idea of using the Policy mode and choosing a specific algorithm based on whether it is a temporary user or a student, this is a simple factory. Well, it's a simple factory + A policy model to realize the off-site charging.
Policy-encapsulate specific policies
|
'Encapsulate a specific algorithm. You need to reference an algorithm. Public class consumecontext Dim strategyconsume as consume Sub new (byref strategy as consume) Me. strategyconsume = strategy End sub Public Function contextinterface () as double Return strategyconsume. getconsume () End Function End Class |
Algorithm parent class-Provides override methods and public members
|
Public class consume Protected line as lineinfo Protected consumemoney as double Protected chargestandard as chargestandardinfo Protected count as int16 Public overridable function getconsume () as double End Function End Class |
Algorithm |
Public class tempconsume Inherits consume Sub new (byref line as lineinfo, byref chargestandard as chargestandardinfo) Me. Line = line Me. chargestandard = chargestandard End sub Public overrides function getconsume () as double If line. consumetime Mod (convert. toint16 (chargestandard. unittime) <> 0 then Consumemoney = (line. consumetime \ chargestandard. unittime + 1) * chargestandard. tempcharge Else Consumemoney = (line. consumetime \ chargestandard. unittime) * chargestandard. tempcharge End if Return consumemoney End Function End Class |
Algorithm B |
Public class fixcomsume Inherits consume Sub new (byref line as lineinfo, byref chargestandard as chargestandardinfo) Me. Line = line Me. chargestandard = chargestandard End sub Public overrides function getconsume () as double If line. consumetime Mod (convert. toint16 (chargestandard. unittime) <> 0 then Consumemoney = (line. consumetime \ chargestandard. unittime + 1) * chargestandard. fixcharge Else Consumemoney = (line. consumetime \ chargestandard. unittime) * chargestandard. fixcharge End if Return consumemoney End Function End Class |
Client code |
'Get the charge amount. Although it is placed on layer B, it is equivalent to the client code Public Function getconsume (byref member as studentinfo, byref line as lineinfo, byref chargestandard as chargestandardinfo) as double Select case Member. isonline Case "true" Dim stragey as new fixcomsume (line, chargestandard) Dim cashcontext as new consumecontext (stragey) Return cashcontext. contextinterface () Case "false" Dim stragey as new tempconsume (line, chargestandard) Dim cashcontext as new consumecontext (stragey) Return cashcontext. contextinterface () End select End Function End Class |
In my opinion, the design pattern is a very big thing, "You can only watch and never play". This time, when other students use it, I also tried to use the design mode (although the most simple one). I think the design mode can also be very grounded. You can use it boldly!