博客
关于我
VB.NET & 职责链模式 (下机时间操作)
阅读量:108 次
发布时间:2019-02-26

本文共 3890 字,大约阅读时间需要 12 分钟。

     前几天一直在讲设计模式,在和师哥师姐的讨论过程中,发现能在机房收费个人重构版中用到好几个设计模式,首先来讨论讨论职责链模式:

     首先当看见这个模式的第一眼就觉得这是一个很简单的模式,可是当使用起来真的得考虑许多,首先要明白什么是链?链是一系列节点的集合,可以灵活拆分再重组。这也是与 链表不同的地方,用户可以去访问节点中的任何一点作为开始节点。

定义:

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

举例:

     现在正在敲个人重构版机房收费系统,在下机的过程中,我们需要和基本数据的设定联系起来,即上机时间是分为三段来考虑的,准备时间,至少上机时间,单位递增时间,在判断处理的时候他则是一次来进行的,这不正与职责链模式不谋而合了吗?参照职责链中涨工资的小故事,我们可以把分成三个类,准备时间,至少上机时间和单位时间

看类图:

看相关代码:

首先得需要一个抽象类,处理请求的一个接口:

'BL_TimeHandler,抽象类,定义一个处理请求的接口Public MustInherit Class BL_TimeHandler    Protected calaulate As BL_TimeHandler    Public Sub Setcalaulate(ByVal calaulate As BL_TimeHandler)  '设置继承者        Me.calaulate = calaulate    End Sub    '处理请求的抽象方法    Public MustOverride Function HandleTime(ByVal time As Integer) As IntegerEnd Class
然后就是三个子类对于父类的继承:

'BL_PrepareTimeHandler,准备时间处理类,继承BL_TimeHandlerPublic Class BL_PrepareTimeHandler : Inherits BL_TimeHandler    Dim preparetime As Integer    '构造函数,利用泛型集合传入准备时间的值    Public Sub New(ByVal EnBasicdata As IList(Of Entity.BasicDataEntity))        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 calaulate.HandleTime(time)    '转到下一位继承者        End If    End FunctionEnd Class
'BL_unitTimeHandler,单位时间处理类Public Class BL_unitTimeHandler : Inherits BL_TimeHandler    Private unittime As Integer    '构造函数,传入递增时间    Public Sub New(ByVal enBasicdata As IList(Of Entity.BasicDataEntity))        Me.unittime = CInt(enBasicdata(0).unitTime)    End Sub    '大于至少时间,返回实际消费时间    Public Overrides Function HandleTime(time As Integer) As Integer        Return Math.Abs(Int(-time / unittime)) * unittime    End Function
'BL_LeastTimeHandler,   至少上机时间类Public Class BL_LeastTimeHandler : Inherits BL_TimeHandler    Private LeastTime As Integer    '构造函数,传入最少上机时间的值    Public Sub New(ByVal enBasicdata As IList(Of Entity.BasicDataEntity))        Me.LeastTime = CInt(enBasicdata(0).leastTime)    End Sub    Public Overrides Function HandleTime(time As Integer) As Integer        If time <= LeastTime Then            Return LeastTime        Else            Return calaulate.HandleTime(time)        End If    End FunctionEnd Class
至此职责链模式就到此结束了,为了便于调用,可以增加一个接口,方便调用,这样就只需要传入两个参数就可了。
Public Class BL_OnlineTimeCount    Public Function CostTime(ByVal enbasicdata As IList(Of Entity.BasicDataEntity), enLineInfo As Entity.LineEntity) As Integer        '实例化类,通过构造函数,传递函数        Dim bPrepareTime As New BL_PrepareTimeHandler(enbasicdata)        Dim bLeastTime As New BL_LeastTimeHandler(enbasicdata)        Dim bStepTime As New BL_unitTimeHandler(enbasicdata)        bPrepareTime.Setcalaulate(bLeastTime)  '设置职责链继承者        bLeastTime.Setcalaulate(bStepTime)        Dim time As Integer   '计算上下机时间差        time = DateDiff("n", enLineInfo.Ontime, enLineInfo.Offtime) + DateDiff("n", enLineInfo.Ondate, enLineInfo.Offdate)        Return bPrepareTime.HandleTime(time)   '职责链处理,返回上机时间    End FunctionEnd Class

在U层中的调用代码:

Dim enline As New Entity.LineEntity        With enline            .Ondate = lineRe(0).Ondate            .Ontime = lineRe(0).Ontime            .Offdate = CStr(Format(Now(), "yyyy-MM-dd"))            .Offtime = CStr(Format(Now(), "HH:mm:ss"))        End With        Dim basicdataList As IList(Of Entity.BasicDataEntity)        Dim BLLBasicdata As BasicDataBLL = New BasicDataBLL()        Dim enbasicdata As New Entity.BasicDataEntity  '调用返回基本数据函数,返回实体集合        basicdataList = BLLBasicdata.ReadBasic(enbasicdata)        '调用职责链模式,计算上机时间        enline.consumeTime = onTimeCount.CostTime(basicdataList, enline)

分类:

     这样在自己的个人机房重构中就完整的对职责链模式熟悉了一遍,对于职责链的学习,也只是处于一个初级阶段,这也纯属属于“纯的职责链模式(要么承担责任,要么将责任全部推给下一家”,而且要求这个请求在过程结束之前必须能够被一个处理者对象所接收;除此之外还有对应的“不纯的职责链模式(允许某一个请求允许某个请求被一个具体处理者部分处理后再向下传递,或者一个具体处理者处理完某请求后其后继处理者可以继续处理该请求,而且一个请求可以最终不被任何处理者对象所接收)”未完

 

你可能感兴趣的文章
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>