"Model" method is boundless-policy mode + simple factory to achieve off-site Charging

Source: Internet
Author: User

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!

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.