[기계실 재구성] 하차-직책체인 모델+전략 모델(실천편)

6260 단어
전언
지난 글에서 우리는 직책 체인 모델과 전략 모델을 어떻게 사용하여 소비 금액을 계산하는지 알고 그 중에서 언급될 수 있는 몇 가지 문제에 대해 간단한 소개를 했다.다음은 vb를 어떻게 사용하는지 살펴보겠습니다.net에서 이전의 이론을 실현하다.
코드
직책 체인 모델
Facade 레이어
 '      
    Public Function costTime(ByVal BasicdataInfo As List(Of Entity.BasicDataEntity), Lineinfo As Entity.MainEntity) As Integer
        '   ,      ,      
        Dim bPrepareTime As New BLL.MainBLL.PrepareTimeHandler(BasicdataInfo)
        Dim bLeastTime As New BLL.MainBLL.BLeastTimeHandler(BasicdataInfo)
        Dim bAddTime As New BLL.MainBLL.UnitTimeHandler

        bPrepareTime.setsuccessor(bLeastTime) '          
        bLeastTime.setsuccessor(bAddTime)
        Dim time As Integer '        
        time = DateDiff("n", Lineinfo.ontime, Lineinfo.offtime)
        Return bPrepareTime.handleTime(time)
    End Function

계층 B: 추상 기본 클래스
 Public MustInherit Class TimeHandler
        Protected successor As TimeHandler
        Public Sub setsuccessor(ByVal successor As TimeHandler)  '       
            Me.successor = successor
        End Sub
        Public MustOverride Function handleTime(ByVal time As Integer) As Integer '           
    End Class

하위 클래스
 ''' 
    '''       
    ''' 
    ''' 
    Public Class PrepareTimeHandler : Inherits TimeHandler
        Dim preparetime As Integer
        Public Sub New(ByVal BasicDataInfo As List(Of Entity.BasicDataEntity)) '    ,         
            Me.preparetime = CInt(BasicDataInfo(0).PrepareTime)
        End Sub
        Public Overrides Function handleTime(time As Integer) As Integer
            If time <= preparetime Then  '            ,  0  
                Return 0
            Else
                Return successor.handleTime(time) '            
            End If
        End Function
    End Class
    ''' 
    '''         
    ''' 
    ''' 
    Public Class BLeastTimeHandler : Inherits TimeHandler
        Private leasttime As Integer
        Public Sub New(ByVal BasicdataInfo As List(Of Entity.BasicDataEntity))
            Me.leasttime = CInt(BasicdataInfo(0).LeastTime) '       leasttime      
        End Sub
        Public Overrides Function handleTime(time As Integer) As Integer
            If time <= leasttime Then    '              ,        
                Return leasttime
            Else
                Return successor.handleTime(time)
            End If
        End Function
    End Class
    ''' 
    '''         
    ''' 
    ''' 
    Public Class UnitTimeHandler : Inherits TimeHandler
        Public Overrides Function handleTime(time As Integer) As Integer
            Return time
        End Function
    End Class

정책 모드
추상 기류
Public MustInherit Class CashSuper
    'MustInherit           
    '    MustOveride ,     MustInherit    。 :                MustOverride。
    '   MustOverride              ,          MustInherit。       
    Public MustOverride Function GetConsumeMoney(ByVal BasicDataList As List(Of Entity.BasicDataEntity), ByVal OffInfo As Entity.MainEntity
) As Single
End Class

추상 전략
Public Class CashContext
    Dim cashsuper As CashSuper
    '         ,        
    Public Function GetResult(ByVal BasicDataList As List(Of Entity.BasicDataEntity), ByVal OffInfo As Entity.MainEntity) As Single
        Select Case OffInfo.Type.Trim()
            Case "    "
                cashsuper = New FixUser()  '         
            Case "    "
                cashsuper = New TmpUser()
            Case Else
                cashsuper = Nothing
        End Select
        Return cashsuper.GetConsumeMoney(BasicDataList, OffInfo)
    End Function
End Class

구체적 책략
Public Class TmpUser : Inherits CashSuper  '    
    Dim strTmpRate As Single
    Public Overrides Function GetConsumeMoney(ByVal BasicDataList As List(Of Entity.BasicDataEntity), ByVal OffInfo As Entity.MainEntity
) As Single
        strTmpRate = Trim(Int(BasicDataList(0).TmpRate))
        Dim consumeCash As Single
        consumeCash = Trim(Val(strTmpRate * Val(OffInfo.ConsumeTime * 1.0 / 60.0)))
        If consumeCash < Int(BasicDataList(0).TmpRate) Then
            consumeCash = Int(BasicDataList(0).TmpRate)
        End If
        Return consumeCash
    End Function
End Class
Public Class FixUser : Inherits CashSuper  '    
    Dim strFixRate As Single
    Public Overrides Function GetConsumeMoney(ByVal BasicDataList As List(Of Entity.BasicDataEntity), ByVal OffInfo As Entity.MainEntity
) As Single
        strFixRate = Trim(Int(BasicDataList(0).Rate))
        Dim consumeCash As Single
        consumeCash = Trim(Val(strFixRate * Val(OffInfo.ConsumeTime / 60.0)))
        If consumeCash < Trim(Int(BasicDataList(0).Rate)) Then
            consumeCash = Trim(Int(BasicDataList(0).Rate))
        End If
        Return consumeCash
    End Function
End Class

UI 레이어
        '           
        OfflineList = FacadeLineRecord.selectLineRecord(offInfo)

        offInfo.ontime = OfflineList(0).ontime
        offInfo.offtime = TimeOfDay
        offInfo.Type = OfflineList(0).Type

        Dim BasicdataList As List(Of Entity.BasicDataEntity) '        
        Dim FacadeBasicdata As New Facade.BasicDataFacade
        Dim BasicdataInfo As New Entity.BasicDataEntity
        Dim FacadeStrategy As New Facade.CashContext    '    

        BasicdataList = FacadeBasicdata.GetData(BasicdataInfo)  '          
        offInfo.ConsumeTime = FacadeLineRecord.costTime(BasicdataList, offInfo) '       ,       
        offInfo.Consume = FacadeStrategy.GetResult(BasicdataList, offInfo) '      ,       
        offInfo.Cash = (CDec(OfflineList(0).Cash) - CDec(offInfo.Consume)).ToString '      

소결
생각의 방향만 분명하게 정리하면 코드는 상대적으로 쉽게 쓸 수 있기 때문에 코드를 두드리기 전의 준비 작업은 반드시 잘 해야 한다~~

좋은 웹페이지 즐겨찾기