机房重构之职责链模式

机房重构下机的完成我用的是职责链模式,一开始也是不怎么了解这个设计模式,不过在摸索着用了之后还是感觉着很不错的。对这个模式的理解也有了比较深刻一点的理解。下边就来介绍一下职责链模式。
首先,在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新组织和分配责任。
机房重构之职责链模式_第1张图片
责任链模式涉及到的角色如下所示:
● 抽象处理者(Handler)角色:定义出一个处理请求的接口。如果需要,接口可以定义 出一个方法以设定和返回对下家的引用。这个角色通常由一个Java抽象类或者Java接口实现。上图中Handler类的聚合关系给出了具体子类对下家的引用,抽象方法handleRequest()规范了子类处理请求的操作。
● 具体处理者(ConcreteHandler)角色:具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。由于具体处理者持有对下家的引用,因此,如果需要,具体处理者可以访问下家。

UI层

Dim cash As String
'计算消费金额
cash = faca.Count(OnlineInfo)

外观层

 Function Count(Online As OnlineEntity) As String
        Dim Bfirst As New OfflineBLLL1
        Dim cash1 As String
        Dim Bsecond As New OfflineBLLL2
        Dim Bthird As New OfflineBLLL3
        Bfirst.SetSuperior(Bsecond)       
     '设置上级,完全可以根据实际需求来更改设置  
        Bsecond.SetSuperior(Bthird)
        cash1 = Bfirst.GetCash(Online)     '进行计算  
        Return cash1
    End Function

B层(逻辑处理)
父类

'计算上机花费金额的父类 
Public MustInherit Class OfflineBLLL
    Protected superior As OfflineBLLL
    '管理者的上级,即所有计算金额的父类  
    Public Sub SetSuperior(ByVal superior As OfflineBLLL)
        Me.superior = superior
    End Sub

    '计算所花费的金额  
    Public MustOverride Function GetCash(ByVal OnlineInfo As JFEntity.OnlineEntity) As String
End Class

各个子类

Public Class OfflineBLLL1
    Inherits OfflineBLLL

    Public Overrides Function GetCash(OnlineInfo As OnlineEntity) As String
        '查询基本表的数据,获取最少上机时间,准备时间
        Dim basicdate As New JFEntity.BasicDateEntity
        Dim fa As New Factory.SetBasicDateFactory
        Dim Iuser As SetBasicDateIDAL
        Dim mylist As New List(Of JFEntity.BasicDateEntity)
        Iuser = fa.selectBasic
        mylist = Iuser.selectBasic(basicdate)
        Dim cash1 As String
        '计算第一步时间间隔所花费的时间  
        If Val(OnlineInfo.UseTime) < (Val(mylist(0).LeastTime) + Val(mylist(0).ReadyTime)) Then
            cash1 = 0
            Return cash1
        Else
          Return   superior.GetCash(OnlineInfo)
        End If

    End Function
Public Class OfflineBLLL2
    Inherits OfflineBLLL

    Public Overrides Function GetCash(OnlineInfo As OnlineEntity) As String
        '查询基本表的数据
        Dim basicdate As New JFEntity.BasicDateEntity
        Dim fa As New Factory.SetBasicDateFactory
        Dim Iuser As SetBasicDateIDAL
        Dim mylist As New List(Of JFEntity.BasicDateEntity)
        Iuser = fa.selectBasic
        mylist = Iuser.selectBasic(basicdate)

        '查询卡的信息
        Dim CardInfo As New JFEntity.CardEntity
        CardInfo.CardNo = OnlineInfo.CardNo
        Dim fa1 As New Factory.ViewBalanceFactory()
        Dim Iuser1 As ViewBalanceIDAL
        Dim mylist1 As New List(Of JFEntity.CardEntity)
        Iuser1 = fa1.selectCardNo
        mylist1 = Iuser1.selectMoney(CardInfo)
        Dim cash1 As String
        If Val(OnlineInfo.UseTime) > (Val(mylist(0).LeastTime) + Val(mylist(0).ReadyTime)) And Val(OnlineInfo.UseTime) <= Val(mylist(0).IncreaseTime) Then
            If mylist1(0).CardType = "固定用户" Then
                cash1 = mylist(0).VIPRate
            Else
                cash1 = mylist(0).CommonRate
            End If
            Return cash1
        Else
            Return superior.GetCash(OnlineInfo)
        End If
    End Function
End Class
Public Class OfflineBLLL3
    Inherits OfflineBLLL

    Public Overrides Function GetCash(OnlineInfo As OnlineEntity) As String
        '查询基本表的数据
        Dim basicdate As New JFEntity.BasicDateEntity
        Dim fa As New Factory.SetBasicDateFactory
        Dim Iuser As SetBasicDateIDAL
        Dim mylist As New List(Of JFEntity.BasicDateEntity)
        Iuser = fa.selectBasic
        mylist = Iuser.selectBasic(basicdate)

        '查询卡的信息
        Dim CardInfo As New JFEntity.CardEntity
        CardInfo.CardNo = OnlineInfo.CardNo
        Dim fa1 As New Factory.ViewBalanceFactory()
        Dim Iuser1 As ViewBalanceIDAL
        Dim mylist1 As New List(Of JFEntity.CardEntity)
        Iuser1 = fa1.selectCardNo
        mylist1 = Iuser1.selectMoney(CardInfo)
        Dim cash1 As String
        Dim x As Integer   '取整  
        Dim y As Integer   '取余  
        Dim a As String       
        x = OnlineInfo.UseTime \ mylist(0).IncreaseTime              '单位时间内获得的整数  
        y = OnlineInfo.UseTime Mod mylist(0).IncreaseTime            '取单位时间内除得的余数  
        a = 60 / mylist(0).IncreaseTime
        If mylist1(0).CardType = "固定用户" Then
            If y = 0 Then              '如果余数=0,则正常计算  
                cash1 = (mylist(0).VIPRate / a) * x        '固定用户花费金额  
            ElseIf y > 0 And y < mylist(0).IncreaseTime Then         '如果小于单位递增时间,则按照单位递增时间算  
                cash1 = (mylist(0).VIPRate / a) * (x + 1)
            End If
        Else
            If y = 0 Then
                cash1 = (mylist(0).CommonRate / a) * x        '临时用户花费金额  
            ElseIf y > 0 And y < mylist(0).IncreaseTime Then
                cash1 = (mylist(0).CommonRate / a) * (x + 1)
            End If
        End If
        Return cash1
    End Function

小结:职责链模式充分的体现了设计模式的七大原则之一的开闭原则,我们的类和模块,对扩展开放,对修改关闭,以后有新的计算下机的方法我们只需要添加新的子类进去即可,不需要修改源代码。我们的系统也更加灵活了。才疏学浅,还请大家多多指教。

你可能感兴趣的:(--------机房重构个人版)