机房重构——职责链模式搞定计算下机时间问题

来源:互联网 发布:阿尔法淘宝宝贝激活码 编辑:程序博客网 时间:2024/06/09 16:41

       经过放假6天的调整,过年后,大家又可以整装待发,一起奋斗,一起迎接新一年的挑战了,祝大家日子过得猴开心!!程序设计的猴赛雷!!~~

       今天总结一下职责链模式,以及职责链模式在机房下机中的应用。

定义

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


自己的理解:这个模式为什么称之为职责链模式,在公司中,每个人的职责权限都是不同的,我们不能越权去处理一些事情,每个权限解决事物的条件不同,就拿请假来说吧,我请2小时假,纪委有权利批给我,但是如果我要请两天的假,纪委就没有权利批给我了,我就要去找米老师,这样这个请求就会一层一层的被传递下去,知道遇到有权限处理我这个问题的人。再说说其中的解耦作用,比如在公司中,有上千个人,我需要办一件事情,向离我最近的一个部门提出一个请求,之后,这个请求就会被传递下去知道解决为止,而这个”我“,是不知道最终问题是被哪一个部门所解决的,这就达到了解耦的目的。


UML图


优点

  • 简化对象的相互连接,降低耦合度:当客户提交请求,请求是沿链传递,直到有一个对象处理它,接受者和发送者都没有对方的明确消息,对象自己也不知道链的结构
  • 指派责任灵活修改:随时随地增加或修改处理一个请求的结构


机房中的应用

这个模式可以帮助我们解决下机时间的计算问题,上机过程中所消费的时间有很多临界条件判断,分为三种情况:1.小于准备时间,消费时间为0  2.大于准备时间,小于至少上机时间,花费时间为至少上机时间  3.大于至少上机时间:就按照单位递增时间来算出来一个值
         首先这三种情况是层层递进的关系,我们需要三个类来处理三种不同的情况,实际时间传入第一个类,满足条件,直接处理,不满足条件,则传递给其他继承类,知道处理完成。那么我们来看看机房中的类图怎么画:



        首先要定义一个抽象类(TimeHandlerBLL),这个类是个处理时间的接口,下边三个子类分别继承这个抽象类,还定义了一个外观类,来分担U层代码负担,同时使得U层和B层解耦,起到实例化对象和传参的作用。

代码

UI:

 Dim cardlineinfo As New Entity.LineRecordEntity            cardlineinfo.Logouttime = TimeOfDay.ToShortTimeString  '给上机记录实体附上下机时间            cardlineinfo.Logintime = txtOnlinetime.Text.ToString            Dim logouttime As String = cardlineinfo.Logouttime '定义变量存放下机时间            Dim offFacade As New Facade.OnlinetimeFacade  '调用外观层,获取消费时间            Dim basicdata As List(Of Entity.BasicData)            Dim userbll As New BLL.UserBLL            basicdata = userbll.getBasicList '得到基础数据            Dim consumetime As Integer = offFacade.costTime(basicdata, cardlineinfo) '调用外观,并入参数

Facade:
Public Function costTime(ByVal basicdata As List(Of Entity.BasicData), lineinfo As Entity.LineRecordEntity) As Integer        '实例化,通过构造函数,传递参数        Dim bPrepareTime As New PrepareTimeHandlerBLL(basicdata)        Dim bLeastTime As New BLeastTimeHandlerBLL(basicdata)        Dim bAddTime As New UnitTimeHandlerBLL(basicdata)        bPrepareTime.setsuccessor(bLeastTime) '设置下一个继承者        bLeastTime.setsuccessor(bAddTime)        'Dim line As New Entity.LineRecordEntity        Dim time As Integer '计算实际在线时间        time = DateDiff("n", lineinfo.Logintime, lineinfo.Logouttime)        Return bPrepareTime.handleTime(time)    End Function

B-TimeHandlerBLL:抽象类
Public MustInherit Class TimeHandlerBLL '抽象基类    Protected successor As TimeHandlerBLL    Public Sub setsuccessor(ByVal successor As TimeHandlerBLL)  '设置继承类        Me.successor = successor    End Sub    Public MustOverride Function handleTime(ByVal time As Integer) As Integer '处理请求的抽象方法End Class

B-PrepareTimeHandlerBLL:准备时间

Public Class PrepareTimeHandlerBLL : Inherits TimeHandlerBLL    Dim preparetime As Integer    Public Sub New(ByVal EnBasicData As List(Of Entity.BasicData)) '构造函数,传入准备时间值        Me.preparetime = CInt(EnBasicData(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 FunctionEnd Class


B-LeastTimeHandlerBLL至少上机时间:

Public Class BLeastTimeHandlerBLL : Inherits TimeHandlerBLL  '至少上机时间处理    Private leasttime As Integer    Public Sub New(ByVal basicdata As List(Of Entity.BasicData))        Me.leasttime = CInt(basicdata(0).Limitedtime) '将基础数据赋给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 FunctionEnd Class


UnitTimeHandlerBLL

Public Class UnitTimeHandlerBLL : Inherits TimeHandlerBLL  '单位时间计算    Private unittime As Integer    Public Sub New(ByVal basicdata As List(Of Entity.BasicData))        Me.unittime = CInt(basicdata(0).Addtime)    End Sub    Public Overrides Function handleTime(time As Integer) As Integer        Return Math.Abs(Int(-time / unittime)) * unittime    End FunctionEnd Class


总结

     完成这一功能,更多的是借鉴大神们的博客,因为在敲上机的时候,我是自己敲得,深知不用设计模式的痛苦,而下机需要调用的数据又非常的多,逻辑判断算的上整个机房系统中最复杂的一部分,所以就看了很多很多师哥师姐们的博客,虽然少了一些自己对这个模式如何应用到机房中,和应该应用到机房中的哪个部分少了些思考,但是在借鉴的过程中,对这一个设计模式由最开始的模模糊糊,到现在的实际应用,也学到了不少的东西。








2 0
原创粉丝点击