职责链模式实现上机业务

上机这块业务,觉得和之前那些简单的一条线判断或者返回数据就可以实现的功能有些不一样了,也不是说他有多复杂的逻辑,其实只是多此判断真的很麻烦,重复的代码让我闻到了代码的坏味道,所以想要借用一下设计模式的一些套路。


why 职责链模式?

###上机需要的判断有哪些?

  • 卡号是否存在,且正在使用
  • 卡号余额是否充足(大于基础数据表设定最少金额)
  • 卡号是否正在上机
    如果让你写四条线来判断在七层里面走来走去,不知道你是不是会疯掉。。

职责链模式介绍?

这里写图片描述

什么是链?

  • 一系列节点的集体
  • 各节点灵活可拆分

----------这里写图片描述
###职责链模式UML图
这里写图片描述

  • Handler是一个处理请求的抽象接口,在上机中表示处理判断的抽象父类,用来设置具体判断类的下一继承者,比如第一个卡号判存完毕之后设置下一个判断–余额是否充足。
  • 上机的三个条件分别为实现抽象接口的三个子类。
  • 当返回的一个泛型记录携带者他的信息进入这个链条的时候,会经历这三个子类链条式的判断,满足第一个条件,则通过第一个子类连接到下一个子类进行判断,如果满足第二个则连接到下一个子类去判断。
    如果第一个子类就判断不通过,那第一个子类就会直接做出处理了。

所以当这个判断请求进入这个链条时,这个请求会被链条传递下去,直到有一个对象处理它为止。请求的发送者和接受者是没有关联的,所以做到了解耦和。


##Demo–上机

UI

  ''' <summary>
    ''' 上机
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnOnline_Click(sender As Object, e As EventArgs) Handles btnOnline.Click
        If txtCardno.Text = "" Then
            MsgBox("上机请输入卡号!")
        Else
            '定义实体层 
            Dim CardInfo As New Entity.CardEntity
            Dim OnlineInfo As New Entity.OnlineEntity
            Dim result As Boolean
            Dim flag As Boolean
            Dim onlinefac As New Facade.T_OnlineFacade
            CardInfo.caroNo = txtCardno.Text.Trim
            result = onlinefac.CheckOnline(CardInfo, OnlineInfo)

            OnlineInfo.CardNo = txtCardno.Text.Trim
            Dim currentDate As DateTime = DateTime.Now
            OnlineInfo.OnDate = currentDate.ToShortDateString
            OnlineInfo.OnTime = currentDate.ToLongTimeString
            OnlineInfo.PcName = Environment.MachineName

            If result = True Then     '满足上机的三个条件,可以上机
                '增加上机表的记录
                Dim addonlineFAC As New Facade.T_OnlineFacade
                flag = addonlineFAC.Addonline(OnlineInfo)
                If flag = True Then
                    txtStuno.Text = OnlineInfo.StudentNo
                    txtName.Text = OnlineInfo.StudentName
                    txtDepart.Text = OnlineInfo.Department
                    txtSex.Text = OnlineInfo.Sex
                    txtType.Text = OnlineInfo.CardType
                    txtRemaincash.Text = OnlineInfo.RemainCash.ToString
                    txtOndate.Text = OnlineInfo.OnDate.ToString
                    txtOntime.Text = OnlineInfo.OnTime.ToString

                    MsgBox("上机成功!")
              
                End If
            Else
                MsgBox("不满足上机条件,请重新输入卡号或充值!")
            End If
        End If

        '调用模块方法,清空
        Call Rdim()
        AllEmpty(arrayControl)

    End Sub

Facade (部分)

 ''' <summary>
    ''' 职责连模式判断是否满足上机条件
    ''' </summary>
    ''' <param name="CardInfo"></param>
    ''' <param name="OnlineInfo"></param>
    ''' <returns>boolean</returns>
    ''' <remarks></remarks>
    Public Function CheckOnline(ByVal CardInfo As CardEntity, ByVal OnlineInfo As OnlineEntity) As Boolean

        Dim bllCardno As New BLL.T_OnlineBLL.CheckCardnoBLL
        Dim bllCardcash As New BLL.T_OnlineBLL.CheckCashBLL
        Dim bllOnline As New BLL.T_OnlineBLL.CheckOnBLL
        Dim flag As Boolean

        '设置继承链条
        bllCardno.setsuccessor(bllCardcash)
        bllCardcash.setsuccessor(bllOnline)

        flag = bllCardno.HandleCheck(CardInfo, OnlineInfo)
        Return flag

    End Function

BLL (部分)

    ''' <summary>
    ''' 抽象类
    ''' </summary>
    ''' <remarks></remarks>
    Public MustInherit Class HandlerBLL
        Protected successor As HandlerBLL
        Public Sub setsuccessor(ByVal successor As HandlerBLL) '设置继承类
            Me.successor = successor
        End Sub

        '处理请求的抽象方法
        Public MustOverride Function HandleCheck(ByVal CardInfo As Entity.CardEntity, ByVal OnlineInfo As OnlineEntity) As Object

    End Class

    ''' <summary>
    ''' 子类1:CheckCardnoBLL  确定卡号是否存在
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CheckCardnoBLL : Inherits HandlerBLL

        Public Overrides Function HandleCheck(ByVal CardInfo As CardEntity, ByVal OnlineInfo As OnlineEntity) As Object  '重写抽象方法

            Dim icheckcard As IDAL.IOnlineDAL
            Dim checkfactory As New Factory.T_OnlineFactory
            Dim mylist As New List(Of Entity.CardEntity)

            icheckcard = checkfactory.CreateIonline             '创建接口实例
            mylist = icheckcard.IselectCard(CardInfo)

            '小于0 ,没有卡号,提示用户;大于0, 继续往下判断
            If mylist.Count <= 0 Then
                Return False
            Else
                '设置下一个继承者,给余额
                Return successor.HandleCheck(CardInfo, OnlineInfo)
            End If
        End Function

    End Class
    ''' <summary>
    ''' 子类2:CheckCashBLL  确定卡号余额是否充足
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CheckCashBLL : Inherits HandlerBLL
      
        Public Overrides Function HandleCheck(ByVal CardInfo As CardEntity, ByVal OnlineInfo As OnlineEntity) As Object
            '获取卡表余额
            Dim icheckcash As IDAL.IOnlineDAL
            Dim checkfactory1 As New Factory.T_OnlineFactory
            Dim mylist1 As New List(Of Entity.CardEntity)

            icheckcash = checkfactory1.CreateIonline   '创建接口实例
            mylist1 = icheckcash.Iselectmoney(CardInfo)

            '获取基本数据表最少金额
            Dim icheckleastcash As IDAL.IOnlineDAL
            Dim checkfactory2 As New Factory.T_OnlineFactory
            Dim mylist2 As New List(Of Entity.DataBaseEntity)
            icheckleastcash = checkfactory2.CreateIonline
            mylist2 = icheckleastcash.Icheckmoney()

            '对比余额是否少于最少金额
            If CDec(mylist1(0).remainCash) < CDec(mylist2(0).leastCash) Then
                Return False
            Else
                Return successor.HandleCheck(CardInfo, OnlineInfo)
            End If
        End Function
    End Class

    ''' <summary>
    ''' 子类3:CheckOnBLL  确定卡号是否正在上机
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CheckOnBLL : Inherits HandlerBLL

        Public Overrides Function HandleCheck(CardInfo As CardEntity, OnlineInfo As OnlineEntity) As Object
            Dim icheckon As IDAL.IOnlineDAL
            Dim checkonfactory As New Factory.T_OnlineFactory
            Dim mylist As New List(Of Entity.OnlineEntity)
            icheckon = checkonfactory.CreateIonline
            mylist = icheckon.IselectOn(OnlineInfo, CardInfo)

            If mylist.Count <= 0 Then                   ' <=0 说明没有正在上机
                Return True
                Exit Function
            End If
            Return False
        End Function

    End Class

##总结
学习是i+1 的过程,i就是之前学过的知识,如果你觉得i+1比较困难,说明你的i没有做好,那就踏踏实实回去做好这个i,编制知识网就是在这个i的基础上做的,i很重要,i+1 更重要。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页