VB.net & Policy mode (selection of down-machine user types)

Source: Internet
Author: User

The previous article described the timing of the operation and basic data setting, today is mainly the application of "strategy mode" to understand the choice between fixed users and temporary users, to see the learning design patterns of their own understanding of the strategy model , We can encapsulate both fixed and temporary users so that the system can be selected according to the type of user. Of course, the premise is to abstract a class to encapsulate the two user types.

See class Diagram:


Code materialization:

First look at the abstract class: Bl_cashsuper, defining two common interfaces for supporting algorithms

' <summary> ' abstract class, defining all common interfaces that support the algorithm ' ' </summary> ' <remarks></remarks>public MustInherit Class bl_cashsuper    ' Calculates the consumption amount (abstract method) public    MustOverride Function Getconsumemondey (ByVal time as Integer) as Singleend Class
see two more specific algorithm classes: BL_CASHTMP,BL_CASHVIP

imports entity.rechargeentitypublic class bl_cashtmp:inherits Bl_cashsuper Dim QueryBasi CDATA As new BASICDATABLL ' instantiation Class BASICDATABLL Dim enbasicdata As New entity.basicdataentity ' Define underlying data entity Dim basicda Talist as IList (of Entity.basicdataentity) ' defines a generic collection of entities Dim Tmphourcash as single ' defines a variable-holds temporary user hourly cost public overr Ides Function Getconsumemondey (time as Integer) as single is assigned to the entity generic collection basicdatalist = Querybasicdata.readbasi C (enbasicdata) Tmphourcash = basicdatalist (0). Tmprate ' Assign value to variable, temporary user hourly fee Dim Consumecash as single ' define variable deposit consumption amount Consumecash = CSng (Time) * CSng (Tmphou RCASH/60) ' Calculate consumption amount (CSng convert expression to single type) Return Consumecash End Function 
Imports entity.rechargeentity ' <summary> ' ' Specific strategy class, calculates the amount of user consumption, encapsulates the specific algorithm or behavior, inherits from the class Bl_cashsuper ' ' </summary > ' <remarks></remarks>public Class bl_cashvip:inherits bl_cashsuper    Dim querybasicdata as New BASICDATABLL   ' Instantiation class Basicdatabll    Dim enbasicdata As New entity.basicdataentity    ' define underlying data entity    Dim Basicdatalist as IList (of entity.basicdataentity)   ' defines a generic collection of entities    Dim Viphourcash as single   ' Define a variable-hold a fixed user half-hour fee public    Overrides Function Getconsumemondey (time as Integer) as single        assignment to the entity generic collection        Basicdatalist = Querybasicdata.readbasic (enbasicdata)        Viphourcash = basicdatalist (0).    rate ' assigns a variable to a fixed user half-hour fee        Dim consumecash as single    ' defines the variable to hold the consumption amount        Consumecash = CSng (Time) * CSNG ( VIPHOURCASH/30)    ' Calculate consumption amount (CSng convert expression to single type)        Return consumecash    End Function
Finally, we need to create an interface to pass in the user's type and make a judgment:

Imports bllimports system.reflection ' <summary> ' Application of simple factory, through the type of cards passed in, to select the application of the algorithm, plus a reflection, so as to perfect the implementation of the principle of closure, When I need to add an algorithm again, I just need to add a B layer to the other, without having to modify any place ' </summary> ' <remarks></remarks>public Class bl_ Cashcontext    Private cashsuper as bl_cashsuper    ' definition abstract class public    Sub New (ByVal cardtype as String)        ' Apply reflection technology automatically selects the class that should be instantiated based on the card number type  , optimizing the Simple factory        Dim strinstance as String = "BLL. Bl_cash "+ cardtype   ' bl_cash+ (Tmp Or VIP)        cashsuper = CType (" BLL "). CreateInstance (strinstance), bl_cashsuper)    End Sub public    Function GetResult (ByVal time as Integer) as single< c10/> ' calls the related consumption processing class to calculate the billing method Dim times as single        ' specific calculation times        = Cashsuper. Getconsumemondey (time)        Return times    End Function
let's see how the U-layer implements the invocation:

Select Case Cardlist (0). Cardtype case            "fixed user"                Cardtype = "VIP" Case            "temporary user"                Cardtype = "TMP"            case Else                cardtype = "        End Select        ' instantiates class Bl_cashcontext, incoming user type        Dim cashcontext as New bl_cashcontext ( Cardtype)        ' Call the policy mode to calculate the balance and assign the value to Consumecash        Dim consumecash as single = Cashcontext. GetResult (enline.consumetime)        ' defines the variable newbalance, which holds the latest balance        Dim newbalance as single = CSng (QCARDL (0). Balance . ToString)-CSng (Consumecash)

This use policy mode is very good to automatically call the card type of its needs, really is very human ah!! It is also convenient for later maintenance. The return to their first trip to the first example, is actually a truth, the establishment of an abstract class, encapsulation algorithm, so that its automatic invocation is good. It also simplifies the connection between users and their various algorithms! Why not?

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.