个人重构——职责链模式
来源:互联网 发布:java怎么清理cookie 编辑:程序博客网 时间:2024/06/05 15:23
疑问1:在重构的过程中,我是无意中浏览到别人博客才知道职责链模式在重构中的应用,之前在敲的过程中怎么没有想起?而别人怎么就能想到此模式的应用?
引入正题:在机房下机的过程中,其中有一个计算消费时间的过程,按照之前的版本系统,是利用一系列的判断语句来计算,其实不然,加入职责链模式来解决此问题。
职责链模式(加薪非要老总批?):使多个对象都有机会处理请求,从而避免请求的发送者和接受者(处理者)之间的耦合关系,将这些对象连成一条链,而请求则沿着此链进行传递,直到有对象处理为止。
联系个人重构,在计算消费时间的过程中,要先后和准备时间,至少上机时间,递增时间比较,UI层获得的消费时间即“请求”,而准备时间,至少上机时间,递增时间则为一个个的接受对象,它们被封装成一个个的类,然后继承于抽象类,废话不多说,见类图。
对照设计模式那本书,除了代码语言由c#变成vb.net外,其它基本没有变化,所以代码还是很好写的,我在敲代码的过程中唯一与课本上不一样的是又引入了外观模式,减少U层的代码量,只需传递参数即可。
详见代码:
抽象类层OnlineTimeHandlerBLL层:
Imports FactoryImports EntityImports [Interface]''' <summary>''' 抽象类,利用职责链模式,计算消费时间。如若时间计算上变方式,也只需加具体类便好。''' </summary>''' <remarks></remarks>Public MustInherit Class OnlineTimeHandlerBLL '获得消费时间 'Protected onlineTime As Integer '设置继任者(上级) Property calculate As OnlineTimeHandlerBLL Public Sub SetNext(ByVal calculate As OnlineTimeHandlerBLL) Me.calculate = calculate End Sub '处理请求(消费时间) Public MustOverride Function Request(ByVal onlineTime As Integer) As IntegerEnd Class具体类层:PrepareTimeHandlerBLL层:
Public Class PrepareTimeHandlerBLL : Inherits OnlineTimeHandlerBLL Private prepareTime As Integer '构造函数,传入准备时间,具体实现使用传入实体 Public Sub New(ByVal enBasicData As List(Of BasicDataEntity)) Me.prepareTime = CInt(enBasicData(0).prepareTime) End Sub Public Overrides Function Request(onlineTime As Integer) As Integer If onlineTime < prepareTime Then Return 0 End If Return calculate.Request(onlineTime) '转移到最少上机时间 End FunctionEnd ClassLeastTimeHandlerBLL层:
Public Class LeastTimeHandlerBLL : Inherits OnlineTimeHandlerBLL Private leastTime As Double Public Sub New(ByVal enBasicData As List(Of BasicDataEntity)) '构造函数初始化 Me.leastTime = CInt(enBasicData(0).leastTime) End Sub Public Overrides Function Request(onlineTime As Integer) As Integer If onlineTime <= leastTime Then Return leastTime End If Return calculate.Request(onlineTime) End FunctionEnd ClassUnitTimeHandlerBLL层:
Public Class unitTimeHandlerBLL : Inherits OnlineTimeHandlerBLL Private unitTime As Double Public Sub New(ByVal enBasicData As List(Of BasicDataEntity)) Me.unitTime = CInt(enBasicData(0).unitTime) End Sub Public Overrides Function Request(onlineTime As Integer) As Integer Return onlineTime End FunctionEnd Class外观层FacOnlineTimeHandler:
Public Class FacOnlineTimeHandler Public Function onlineTime(ByVal consumeTime As Integer) As Integer Dim enBasicData As New BasicDataEntity Dim mylist As New List(Of BasicDataEntity) Dim limitMoney As New BLL.SetBasicDataBLL() mylist = limitMoney.DataRecord(enBasicData) '返回基本数据表信息 Dim prepareTime As New BLL.PrepareTimeHandlerBLL(mylist) '设定基本数据 Dim leastTime As New BLL.LeastTimeHandlerBLL(mylist) Dim unitTime As New BLL.unitTimeHandlerBLL(mylist) prepareTime.SetNext(leastTime) '设置上级 leastTime.SetNext(unitTime) Return prepareTime.Request(consumeTime) End FunctionUI层:
'计算消费时间 Dim calculate As New Facade.FacOnlineTimeHandler() Dim consumeTime As Double mylistData = limitMoney.DataRecord(enBasicData) '从数据信息获取记录 Dim consumeTime1 As Integer = DateDiff("n", CDate(txtOnDate.Text), CDate(txtOffDate.Text)) + DateDiff("n", CDate(txtOnTime.Text), CDate(txtOffTime.Text)) consumeTime = calculate.onlineTime(consumeTime1) '向外观传递参数"消费时间",接收返回值存在consumeTime consumeTime = FormatNumber(consumeTime / mylistData(0).unitTime, 1)
大致内容就是这些,我认为虽然加入设计模式貌似看起来反而更复杂,但如果加入处理消费时间的参数,则只需要加入具体处理类即可,其次,在学完设计模式后这也是一个很好的实践机会,反复的过程,强化的过程。
0 0
- 个人重构——职责链模式
- 个人重构——职责链模式PK状态模式
- 个人机房重构——下机与职责链的结合
- 机房重构——下机(职责链模式+策略模式)
- 【机房重构】下机——职责链模式+策略模式(理论篇)
- 【机房重构】下机——职责链模式+策略模式(实践篇)
- 机房重构--职责链模式应用
- 机房重构之职责链模式
- 机房重构职责链模式-上机
- 职责链模式应用——下机(机房重构知识点总结)
- 机房重构——职责链模式搞定计算下机时间问题
- 【机房重构】--职责链模式+策略模式 实现下机
- LN项目重构之职责链模式
- 【机房重构】--职责链模式实现上机
- 【机房重构】职责链模式之上机
- 《设计模式》——职责链模式
- 设计模式——职责链模式
- 设计模式——职责链模式
- 虚方法与抽象方法有什么区别
- 【php】使用重定向,伪装表单处理页面不存在
- WIN7刻录MOV文件到光盘分享
- numpy.distutils.system_info.NotFoundError: no lapack/blas resources found
- XenDesktop/XenApp 7.x 禁用/设置超时自动断开会话
- 个人重构——职责链模式
- mac 终端保存环境变量后 ls 等命令不能找到(command not fond)
- Direct-Load-apk启动插件的原理
- iOS之禁止横屏
- 从购买春节火车票,看互联网公司不应该做“恶”
- 广播机制(Broadcast Receiver)——第一行代码阅读笔记
- 第五周项目一体验常成员函数
- wireshark 可以过滤 https
- 毕晓英第二章14题