职责链模式

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

                                                                                 

        简单的理解就是让解决方之间有一定的关联  ,把用户不同的请求传递给不同的解决方,直到这个请求可以被解决为止。这样的方法让处理端对客户端隐形,只把结果返回给客户端,这样系统的更改可以在不影响客户端的情况下,动态的重新组织和分配责任使得客户端与最终处理端降低耦合度。

     在机房重构中的下机功能中就可以运用这种方法。因为下机时的所用时间有准备时间,最少时间,递增时间之分,他们是三个不同的时间段,所以他们的结账方式不同。在这里运用职责链模式,可以让下机结账的思路和流程更加清晰。


     抽象类:定义一个处理请示的接口   

''' <summary>
''' 结账,处理上机消费问题的抽象类
''' </summary>
''' <remarks></remarks>
Public MustInherit Class OffLineBll  '指定某个类只能用作基类,不能直接从该类创建对象。
    Protected successor As OffLineBll   '指定继承者
    Public Sub SetSuccessor(ByVal successor As OffLineBll)
        Me.successor = successor
    End Sub
    Public MustOverride Function HandleTime(ByVal time As Integer) As Integer  '处理请求的抽象方法
End Class

    

     具体处理类:处理它所负责的请求,可访问他的后继者,如果可处理该请求,就处理,否则就将该请求传递给其他处理者。

  Public Class PrepareTimeHandlerBll : Inherits OffLineBll

    ''' <summary>
    ''' 准备时间处理者,处理时间请求,如果可以处理就处理,否则把请求转给继承者
    ''' </summary>
    ''' <remarks></remarks>

    Dim preparetime As Integer
  
    Public Sub New(ByVal esBasicData As List(Of Entity.BasicDataEntity))  '构造函数,传入准备时间的值
        Me.preparetime = CInt(esBasicData(0).Prepare)
    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

''' <summary>
''' 最少上机时间处理者,同上
''' </summary>
''' <remarks></remarks>

Public Class LeastTimeHandlerBll : Inherits OffLineBll
    Dim leatsttime As Integer
   
    Public Sub New(ByVal esBasicData As List(Of Entity.BasicDataEntity))  '构造函数,传入至少上机时间的值
        Me.leatsttime = CInt(esBasicData(0).Leasttime)
    End Sub

    Public Overrides Function HandleTime(time As Integer) As Integer  '重写父类函数
        If time <= leatsttime Then     '如果上机时间小于至少时间,返回最少上机时间
            Return leatsttime
        Else
            Return successor.HandleTime(time)
        End If
    End Function
End Class

''' <summary>
''' 处理剩余的请求
''' </summary>
''' <remarks></remarks>
Public Class StepTimeHandlerBll : Inherits OffLineBll
    Dim steptime As Integer
   
    Public Sub New(ByVal esBasicData As List(Of Entity.BasicDataEntity))  '构造函数,传递单位递增时间
        Me.steptime = CInt(esBasicData(0).Leasttime)
    End Sub

    Public Overrides Function HandleTime(time As Integer) As Integer
        '如果上机时间大于至少时间,返回实际消费时间
        Return Math.Abs(Int(-time)) ' abs(int(-x))先转换为负数,取整后再变为正数即可
    End Function
End Class

     

    下机处理:在UI下机事件中调用职责链模式,并设置链中的每个继承者

     

                Dim basicData As IList(Of Entity.BasicDataEntity)
                Dim basic As New Bll.BasicDataBll
                '获取基本数据集()
                basicData = basic.ReadData
                '创建准备时间, 并赋给准备时间
                Dim preparetime As New Bll.PrepareTimeHandlerBll(basicData(0).Prepare)
                '创建上机最少时间,并赋给最少时间
                Dim listtime As New Bll.LeastTimeHandlerBll(basicData(0).Leasttime)
                '创建递增时间, 并赋给递增时间
                Dim increasetime As New Bll.StepTimeHandlerBll(basicData(0).Increase)

                '设置后继承者()
                preparetime.SetSuccessor(listtime)
                listtime.SetSuccessor(increasetime)

                '获取消费时间()
                Dim datas As New Entity.OnLineEntity
                datas.Offdate = txtOffdate.Text
                datas.Offtime = txtOfftime.Text
                 '获取上下机时间差
                Dim costTime As Single
                costTime = CInt(DateDiff("n", txtOntime.Text, txtOfftime.Text))

                '调用初始接收者
                costTime = preparetime.HandleTime(costTime)
    

      接下来就是根据职责链模式所获得的花费时间,在根据不同的用户(策略模式)进行计算花费。将在下篇博客中介绍。



             
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值