上篇文章讲述了对于下机操作和基本数据设定的时间联系,今天主要就是应用“策略模式”来了解了解对于固定用户,以及临时用户之间的选择,看学习设计模式的时候自己对于策略模式的理解,我们可以把固定用户和临时用户封装起来,这样系统就可以按照用户的类型来进行选择了。当然前提首先要抽象一个类,来封装这两个用户类型。
看类图:
代码具体化:
首先看抽象类:BL_CashSuper,定义两种支持算法的公共接口
''' <summary> ''' 抽象类,定义所有支持算法的公共接口 ''' </summary> ''' <remarks></remarks> Public MustInherit Class BL_CashSuper '根据上机时间,卡的类型,计算出消费金额(抽象方法) Public MustOverride Function GetconsumeMondey(ByVal time As Integer) As Single End Class再看两个具体的算法类:BL_CashTmp,BL_CashVIP
Imports Entity.RechargeEntity Public Class BL_CashTmp : Inherits BL_CashSuper Dim QueryBasicdata As New BasicDataBLL '实例化类BasicdataBLL Dim EnBasicdata As New Entity.BasicDataEntity '定义基础数据实体 Dim BasicdataList As IList(Of Entity.BasicDataEntity) '定义实体的泛型集合 Dim TmpHourCash As Single '定义一个变量-存放临时用户每小时费用 Public Overrides Function GetconsumeMondey(time As Integer) As Single '赋值给实体泛型集合 BasicdataList = QueryBasicdata.ReadBasic(EnBasicdata) TmpHourCash = BasicdataList(0).TmpRate '给变量赋值,临时用户每小时费用 Dim Consumecash As Single '定义变量存放消费金额 Consumecash = CSng(time) * CSng(TmpHourCash / 60) '计算消费金额(CSng把表达式转化成Single类型) Return Consumecash End Function
Imports Entity.RechargeEntity ''' <summary> ''' 具体策略类,计算会员用户消费金额,封装的具体的算法或行为,继承于类BL_CashSuper ''' </summary> ''' <remarks></remarks> Public Class BL_CashVIP : Inherits BL_CashSuper Dim QueryBasicdata As New BasicDataBLL '实例化类BasicdataBLL Dim EnBasicdata As New Entity.BasicDataEntity '定义基础数据实体 Dim BasicdataList As IList(Of Entity.BasicDataEntity) '定义实体的泛型集合 Dim VIPHourCash As Single '定义一个变量-存放固定用户半小时费用 Public Overrides Function GetconsumeMondey(time As Integer) As Single '赋值给实体泛型集合 BasicdataList = QueryBasicdata.ReadBasic(EnBasicdata) VIPHourCash = BasicdataList(0).Rate '给变量赋值,固定用户半小时费用 Dim Consumecash As Single '定义变量存放消费金额 Consumecash = CSng(time) * CSng(VIPHourCash / 30) '计算消费金额(CSng把表达式转化成Single类型) Return Consumecash End Function最后我们需要建立一个接口,来将用户的类型传入进来,进行判断:
Imports BLL Imports System.Reflection ''' <summary> ''' 应用简单工厂,通过传入的卡的类型,来具体选择应用那个算法,加一个反射,这样才能完美的实现开发封闭的原则,当我再需要增加一个算法的时候,我只需要在另外增加一个B层就好,而无需修改任何地方 ''' </summary> ''' <remarks></remarks> Public Class BL_CashContext Private cashsuper As BL_CashSuper '定义抽象类 Public Sub New(ByVal CardType As String) '应用反射技术根据卡号类型自动选择应该实例化的类 ,优化简单工厂 Dim strInstance As String = "BLL.BL_Cash" + CardType 'BL_Cash+(Tmp Or VIP) cashsuper = CType(Assembly.Load("BLL").CreateInstance(strInstance),BL_CashSuper) End Sub Public Function GetResult(ByVal time As Integer) As Single '调用相关的消费处理类计算收费方法 Dim times As Single '具体计算 times = cashsuper.GetconsumeMondey(time) Return times End Function来看看U层是如何实现调用的:
Select Case cardList(0).cardtype Case "固定用户" CardType = "VIP" Case "临时用户" CardType = "Tmp" Case Else CardType = "" End Select '实例化类BL_CashContext,传入用户类型 Dim cashcontext As New BL_CashContext(CardType) '调用策略模式计算出余额并赋值给consumecash Dim consumecash As Single = cashcontext.GetResult(enline.consumeTime) '定义变量newbalance,用于存放最新的余额 Dim newbalance As Single = CSng(QCardL(0).balance.ToString) - CSng(consumecash)
这样使用策略模式则很好的就自动调用了其需求的卡类型,真的是很人性化啊!!而且还方便后期的维护。回归到自己刚开始的出门旅行的例子,其实都是一个道理,建立一个抽象类,封装算法,让其自动调用就好。既简单化同时还降低了使用者与其各种算法的联系!何乐而不为呢? 原文链接:https://www.f2er.com/vb/257842.html