机房重构之下机

来源:互联网 发布:有哪些是网络信息安全 编辑:程序博客网 时间:2024/06/06 08:25
   在下机的时候用到了策略模式,经过不断地尝试和调错终于把功能实现了,话不多说,看看到底是怎么实现的。
U层
 ''' <summary>    ''' 下机功能    ''' </summary>    ''' <param name="sender"></param>    ''' <param name="e"></param>    ''' <remarks></remarks>    Private Sub btnOff_Click(sender As Object, e As EventArgs) Handles btnOff.Click        '判断TxtCardID是否为空或是否为数字        If txtCardID.Text = "" Or txtCardID.Text = "请输入卡号" Then            MsgBox("请输入卡号", vbOKOnly, "提示")            txtCardID.Text = ""            End        ElseIf IsNumeric(Trim(txtCardID.Text)) = False Then            MsgBox("卡号请输入数字", vbOKOnly, "提示")            txtCardID.Text = ""        End If        '判断该卡号是否存在        Dim Card As New Entity.OffLineInfo        Card.CardID = txtCardID.Text        Dim facade As New MainFacade        Dim dt As New DataTable        dt = facade.Selectcard(Card)        If dt.Rows.Count = 0 Then            MsgBox("该卡号不存在", vbOKOnly, "提示")        Else            '获取金额和类型            Dim Type As String            Type = dt.Rows(0).Item(3).ToString().Trim()            Dim cash As String            cash = dt.Rows(0).Item(2)            '判断该卡号是否正在上机(T_OnOffLine)            Dim cardOn As New Entity.OnLineInfo            cardOn.CardID = txtCardID.Text            Dim table As New DataTable            table = facade.selectOn(cardOn)            If table.Rows.Count = 0 Then                MsgBox("该卡号未上机", vbOKOnly, "提示")            Else                '获取上机时间                cardOn.Ondate = table.Rows(0).Item(1).ToString()                cardOn.OnTime = table.Rows(0).Item(2).ToString()                '查询基本数据                Dim basic As New Entity.BasicDataInfo                Dim table1 As New DataTable                table1 = facade.SelectBasic(basic)                basic.UnitTime = table1.Rows(0).Item(1).ToString.Trim()                basic.PreareTime = table1.Rows(0).Item(8).ToString.Trim()                basic.LeastTime = table1.Rows(0).Item(0).ToString.Trim()                '获取下机时间                cardOn.OffDate = Date.Now.ToString("yyyy-MM-dd")                cardOn.OffTime = Format(Now, "HH:mm:ss").ToString                '计算消费时间                Dim ConTime As Integer                '利用状态模式,先实例化一个Timestate                Dim BLLObject As New BLL.COR_OnlineTimeCountBLL                ConTime = BLLObject.CostTime(basic, cardOn)                cardOn.ConsumeTime = ConTime                '计算消费金额                Select Case Type                    Case "固定用户"                        Type = "Vip"                    Case "临时用户"                        Type = "Tmp"                End Select                Dim cashContext As New BLL.STG_CashContext(Type)                Dim ConsumeMoney As Single = CStr(cashContext.GetResult(ConTime))  '调用策略模式计算出余额并且赋值给consumeMoney                Dim newCash As Single = CSng(cash) - CSng(ConsumeMoney)                '更新T_OnOffLineRecord                Dim consume As New Entity.OnLineInfo                consume.CardID = txtCardID.Text                consume.OffDate = DateTime.Now.ToString("yyyy-MM-dd")                consume.OffTime = DateTime.Now.ToShortTimeString().ToString                consume.Computer = System.Net.Dns.GetHostName().ToString                consume.ConsumeMoney = ConsumeMoney                consume.ConsumeTime = ConTime                consume.CheckStatus = "下机"                Dim flag1 As Boolean                flag1 = facade.UpLIne(consume)                If flag1 = True Then                    '修改卡上的余额                    Card.CardID = txtCardID.Text                    Card.Account = newCash                    Dim flag As Boolean                    flag = facade.UpCard(Card)                    If flag = True Then                        '窗体显示基本信息                        txtCardID.Text = Card.CardID                        TxtBalance.Text = newCash                        TxtConMoney.Text = ConsumeMoney                        TxtConTime.Text = ConTime                        txtOndate.Text = cardOn.Ondate                        txtOnTime.Text = cardOn.OnTime                        TxtOffDate.Text = Date.Now.ToString("yyyy-MM-dd")                        TxtOffTime.Text = Format(Now, "HH:mm:ss").ToString                        Txtsex.Text = dt.Rows(0).Item(5)                        TxtstudentID.Text = dt.Rows(0).Item(1)                        TxtType.Text = dt.Rows(0).Item(3)                        TxtDepart.Text = dt.Rows(0).Item(6)                        TxtName.Text = dt.Rows(0).Item(4)                    Else                        MsgBox("更新下机记录失败,请稍后重试", vbOKOnly, "提示")                    End If                End If            End If        End If    End Sub
Facade层,和平常的使用相同,定义可以访问到B层的函数,在这里就不展示。
B层
 和平常的使用相同,不同的就是在B层中另外定义了策略的一些类。下图分别定义了计算消费时间和消费金额的类。另外需要注意的是由于策略模式的使用破坏了完整的七层结构,要在U层中直接引用B层。
  实现到接口层和工厂的方法和之前一样,只是新建了上面的几个类,接下来就展示一下策略模式的几个类。
COR_TimeHandler 类
 
Public MustInherit Class COR_TimeHandler    Protected successor As COR_TimeHandler    Public Sub setsuccessor(ByVal successor As COR_TimeHandler)        Me.successor = successor  '设置继承者    End Sub    Public MustOverride Function HandleTime(onlineTime As Integer) As Integer  '处理请求的抽象方法End Class

COR_PrepareTimeHandlerBLL 类
Public Class COR_PrepareTimeHandlerBLL : Inherits COR_TimeHandler    Private preparetime As Integer    Public Sub New(ByVal basic As Entity.BasicDataInfo)        '构造函数,传入准备时间的值        Me.preparetime = CInt(basic.PreareTime)    End Sub    Public Overrides Function HandleTime(onlineTime As Integer) As Integer        'if函数盘算上机的时间收费在本场婚礼范围内,不在转到下一个处理类,返回0        If onlineTime <= preparetime Then            Return 0        Else '转到下一个计算方法            Return successor.HandleTime(onlineTime)        End If    End FunctionEnd Class
COR_UnitTimeHandlerBLl 类
Public Class COR_UnitTimeHandlerBLl : Inherits COR_TimeHandler    Private unittime As Integer    Public Sub New(ByVal basic As Entity.BasicDataInfo)        Me.unittime = CInt(basic.UnitTime)    End Sub    Public Overrides Function HandleTime(onlineTime As Integer) As Integer        'Int是将一个数值向下取整为最接近的整数的函数        'Abs函数返回指定数值的绝对值        '该函数意味着:类似四舍五入        Return Math.Abs(Int(-onlineTime / unittime)) * unittime    End FunctionEnd Class
COR_LeastTimeHandlerBLL 类
Public Class COR_LeastTimeHandlerBLL : Inherits COR_TimeHandler    Private leasttime As Integer    '构造函数,传入至少上机时间的值    Public Sub New(ByVal basic As Entity.BasicDataInfo)        Me.leasttime = CInt(basic.LeastTime)    End Sub    Public Overrides Function HandleTime(onlineTime As Integer) As Integer        '如果上机时间小于至少上机时间,返回时间        If onlineTime <= leasttime Then            Return leasttime        Else  '否则 转到下一位继承者            Return successor.HandleTime(onlineTime)        End If    End FunctionEnd Class
COR_OnlineTimeCountBLL  类
Public Class COR_OnlineTimeCountBLL        Public Function CostTime(basic As Entity.BasicDataInfo, cardOn As Entity.OnLineInfo) As Integer            '实例化类,通过构造函数,传递参数            Dim bPrepareTime As New COR_PrepareTimeHandlerBLL(basic)            Dim bLeastTime As New COR_LeastTimeHandlerBLL(basic)            Dim BsetpTime As New COR_UnitTimeHandlerBLL(basic)            bPrepareTime.setsuccessor(bLeastTime) '设置职责链继承者即后继承者            bLeastTime.setsuccessor(BsetpTime)            '计算上下机时间差            Dim onlineTime As Integer            onlineTime = DateDiff("n", cardOn.OnTime, cardOn.OffTime) + DateDiff("n", cardOn.Ondate, cardOn.OffDate)            '职责链处理,返回上机时间            Return bPrepareTime.HandleTime(onlineTime)        End Function    End Class
计算完时间了,接下来开始说说怎么收钱,
STG_CashContext 类
Imports System.Reflection'应用简单工厂模式,通过传入的卡的类型,来具体选择用那种算法Public Class STG_CashContext    Private cashsuper As STG_CashSuperBLL '定义抽象类    Public Sub New(ByVal Type As String)        '应用反射技术根据卡号类型自动选择应该实例化的类        Dim strInstance As String = "BLL.STG_Cash" + Type + "BLL"        cashsuper = CType(Assembly.Load("BLL").CreateInstance(strInstance), STG_CashSuperBLL)    End Sub    Public Function GetResult(ByVal onlineTime As Integer) As Single        '调用相关的消费处理类计算收费方法        Return cashsuper.GetConsumeCash(onlineTime)    End FunctionEnd Class
STG_CashSuperBLL 类
Public MustInherit Class STG_CashSuperBLL    '根据上机时间,卡的类型,计算出消费金额的抽象方法    Public MustOverride Function GetConsumeCash(ByVal onlineTime As Integer) As SingleEnd Class
STG_CashVipBLL 类
Public Class STG_CashVipBLL : Inherits STG_CashSuperBLL    Dim queryBasedate As New MainBLL  '设立查询基础数据    Dim ebasedate As New Entity.BasicDataInfo    Dim table As New DataTable    Dim vipHourCash As Single  '定义变量存放固定用户每小时费用    Public Overrides Function GetConsumeCash(onlineTime As Integer) As Single        table = queryBasedate.basicData(ebasedate)        vipHourCash = table.Rows(0).Item(2)  '查询数据库中对于固定用户的值        Dim consumecash As Single        consumecash = CSng(onlineTime) * CSng(vipHourCash / 60) '计算消费金额        Return consumecash    End FunctionEnd Class
STG_CashTmpBLL 类
Public Class STG_CashTmpBLL : Inherits STG_CashSuperBLL    Dim queryBasedate As New MainBLL    Dim ebasicdate As New Entity.BasicDataInfo    Dim table As DataTable    Dim TmpHourCash As Single '定义变量存放临时用户每小时费用    Public Overrides Function GetConsumeCash(onlineTime As Integer) As Single        '查询数据库中对于临时用户的值        table = queryBasedate.basicData(ebasicdate)        TmpHourCash = table.Rows(0).Item(3)        '计算消费金额        Dim consumecash As Single        consumecash = CSng(onlineTime) * CSng(TmpHourCash / 60)        Return consumecash    End FunctionEnd Class
  其它层的编写和之前的七层相同,大家应该能写出,就不一一展示了。
【总结】
   下机相对来说是比较麻烦,再敲之前先理清整个过程的顺序,先验证什么后需要什么等等,大致上有了方向,在去敲的时候就比价简单了。
                                             
0 0
原创粉丝点击