天天看点

机房上机--职责连模式前言:内容:三、总结

前言:

机房上机,我们需要判断卡号是否存在,余额是否充足,该卡是否正在上机,这一系列的请求正好使用设计模式中的职责连模式解决这个问题,从而减少If语句的使用,减少代码的使用。

内容:

一、职责连模式

职责连模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止。

机房上机--职责连模式前言:内容:三、总结

二、机房上机流程图

机房上机--职责连模式前言:内容:三、总结

流程:单击上机按钮,将上机信息(卡号)传到外观层(想当于接口),逻辑层根据实际情况调用请求,直至请求处理为止。

三、代码

1、UI层

Private Sub btnOnLine_Click(sender As Object, e As EventArgs) Handles btnOnLine.Click
        If txtCardNo.Text = "" Then
            MsgBox("请输入卡号")
            Return
        End If

        Dim Card As New Entity.EntityCard
        Dim CheckMoney As New Entity.EntityCard
        Dim line As New Entity.LineLogEntity
        Dim student As New Entity.EntityStudent
        Dim facade As New Facade.OnFacade
        Dim result As Boolean

        Card.CardNo = Trim(txtCardNo.Text)
        line.CardNo = txtCardNo.Text
        student.CardNo = txtCardNo.Text
        '监测是否符合上机要求
        result = facade.CheckOn(Card, line)
        line.OnDate = Format(Now, "yyyy-MM-dd HH:mm:ss")
        line.CardNo = txtCardNo.Text
        '符合上机要求
        If result = True Then
            Dim facade1 As New Facade.OnFacade
            Dim table As Boolean
            '更新上机记录表
            table = facade1.AddOnline(line)
            '更新成功
            If table = True Then
                Dim facadeS As New Facade.OnFacade
                Dim table1 As New DataTable

                '显示上机信息
                table1 = facadeS.selectstudentF(Card)

                txtType.Text = table1.Rows(0).Item(2)
                txtStudentNo.Text = table1.Rows(0).Item(1)
                txtStudentName.Text = table1.Rows(0).Item(3)
                txtDepartment.Text = table1.Rows(0).Item(4)
                txtSex.Text = table1.Rows(0).Item(5)
                txtOnDate.Text = line.OnDate
                txtBalance.Text = table1.Rows(0).Item(6)
                MsgBox("上机成功!")
            End If
        Else
            MsgBox("卡号不存在或已上机,或金额不足,请确认满足以上条件!")


        End If


    End Sub
           

2、Facade层

Public Function CheckOn(ByVal card As Entity.EntityCard, ByVal line As Entity.LineLogEntity) As Boolean
        Dim CheckCard As New BLL.CheckCardBLL
        Dim CheckMoney As New BLL.CheckMoneyBLL
        Dim CheckOnLine As New BLL.CheckOnLineBLL

        CheckCard.SetSuccessor(CheckMoney)
        CheckMoney.SetSuccessor(CheckOnLine)
        Return CheckCard.OnRequest(card, line)



    End Function
           

3、BLL层

(1)抽象基类

Imports System.Data.SqlClient
Imports Entity
Imports IDAL
Imports Factory

'MustInherit-定义抽象基类
Public MustInherit Class OnBLL  '上机抽象类


    Protected successory As OnBLL

    Public Sub SetSuccessor(ByVal successory As OnBLL)  '设置继任者
        Me.successory = successory
    End Sub

    'MustOverride--定义抽象方法,用于子类的重写
    Public MustOverride Function OnRequest(ByVal card As Entity.EntityCard, ByVal line As Entity.LineLogEntity) As Object

End Class
           

(2)检查卡号是否存在

Public Class CheckCardBLL : Inherits OnBLL

    '查看卡号是否存在
    Public Overrides Function OnRequest(card As Entity.EntityCard, line As Entity.LineLogEntity) As Object
        Dim factory As New Factory.OnLineFactory
        Dim Icardno As IDAL.IOnLine
        Dim mylist As New List(Of Entity.EntityCard)
        Icardno = factory.OnLinefactory()
        mylist = Icardno.CheckCardNo(card)

        If mylist.Count <= 0 Then
            Return False
        Else
            Return successory.OnRequest(card, line)
        End If



    End Function
End Class
           

(3)检查余额是否充足

Public Class CheckMoneyBLL : Inherits OnBLL


    Public Overrides Function OnRequest(card As Entity.EntityCard, line As Entity.LineLogEntity) As Object
        Dim factory As New Factory.OnLineFactory
        Dim Icardno As IDAL.IOnLine
        Dim mylist As New List(Of Entity.EntityCard)
        Icardno = factory.OnLinefactory()
        mylist = Icardno.CheckCardNo(card)

        If (CDec(mylist(0).Balance)) <= 0.0 Then
            Return False

        Else
            Return successory.OnRequest(card, line)
        End If


    End Function
End Class
           

(4)检查是否正在上机

Public Class CheckOnLineBLL : Inherits OnBLL


    Public Overrides Function OnRequest(card As Entity.EntityCard, line As Entity.LineLogEntity) As Object
        Dim factory As New Factory.OnLineFactory
        Dim Icardno As IDAL.IOnLine
        Dim mylist As New List(Of Entity.LineLogEntity)

        Icardno = factory.OnLinefactory()
        mylist = Icardno.CheckOnLine(line)

        If mylist.Count <= 0 Then
            Return True
            Exit Function
        Else
            Return False
        End If
    End Function
End Class
           

4、接口层

Public Interface IOnLine
    '检查卡表是否存在,是否余额充足
    Function CheckCardNo(ByVal card As Entity.EntityCard) As List(Of Entity.EntityCard)
    '检查是否正在上机
    Function CheckOnLine(ByVal line As Entity.LineLogEntity) As List(Of Entity.LineLogEntity)
    '更新上机记录表
    Function updateOnLine(ByVal line As Entity.LineLogEntity) As Boolean

    Function SelectStudent(ByVal card As Entity.EntityCard) As DataTable


End Interface
           

5、DAL层

Public Class OnDAL : Implements IOnLine

    '检查卡表是否存在,是否余额充足
    Public Function CheckCardNo(card As EntityCard) As List(Of Entity.EntityCard) Implements IOnLine.CheckCardNo
        Dim sql As String
        Dim table As New DataTable
        Dim sqlhelper As New SQLHelper.sqlhelper

        Dim paras As SqlParameter() = {New SqlParameter("@CardNo", card.CardNo),
                                       New SqlParameter("@state", "使用")}
        sql = "select * from Y_Card_Info where CardNo [email protected] and [email protected]"
        table = sqlhelper.ExecSelect(sql, CommandType.Text, paras)
        Dim mylist As List(Of Entity.EntityCard)
        mylist = ConvertHelper.convertToList(Of EntityCard)(table)
        Return mylist


    End Function
    '检查是否正在上机
    Public Function CheckOnLine(line As LineLogEntity) As List(Of Entity.LineLogEntity) Implements IOnLine.CheckOnLine
        Dim sql As String
        Dim table As New DataTable
        Dim sqlhelper As New SQLHelper.sqlhelper

        Dim paras As SqlParameter() = {New SqlParameter("@CardNo", line.CardNo),
                                       New SqlParameter("@state", "正在上机")}
            
        sql = "select * from Y_LineLog_Info where [email protected] and state [email protected]"
        table = sqlhelper.ExecSelect(sql, CommandType.Text, paras)

        Dim mylist As List(Of Entity.LineLogEntity)
        mylist = ConvertHelper.convertToList(Of LineLogEntity)(table)
        Return mylist

    End Function
    '更新上机记录表
    Public Function updateOnLine(line As LineLogEntity) As Boolean Implements IOnLine.updateOnLine
        Dim sql As String
        Dim table As Boolean
        Dim sqlhelper As New SQLHelper.sqlhelper
        line.Computer = System.Net.Dns.GetHostName().ToString()
        Dim paras As SqlParameter() = {New SqlParameter("@CardNo", line.CardNo),
                                       New SqlParameter("@UserID", Entity.CommonVariable.CommonUserID),
                                       New SqlParameter("@OnDate", line.OnDate),
                                       New SqlParameter("@state", "正在上机"),
                                       New SqlParameter("@Computer", line.Computer)}

        sql = "insert into Y_LineLog_Info (CardNo,UserID,OnDate,state,Computer) values(@CardNo,@UserID,@OnDate,@state,@Computer)"
        table = sqlhelper.ExecAddDelUpdate(sql, CommandType.Text, paras)

        Return table

    End Function
End Class
           

三、总结

 通过敲机房,又重新温习了一遍设计模式,设计模式还需要我们再去学习,只有多实践才能更加灵活的去使用它们。