机房重构---下机(策略模式和职责连模式)

2024-03-27 01:38

本文主要是介绍机房重构---下机(策略模式和职责连模式),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:

下机需要查看消费时间以及选择用户类型,因此这里用到了职责连模式以及策略模式两种模式。消费时间用的是职责连模式,选择用户类型(固定用户,临时用户)。涉及多个表的查询,用视图是个不错的选择。

内容:


一、各个层调用关系:


二、UI层代码:

 Private Sub btnOffLine_Click(sender As Object, e As EventArgs) Handles btnOffLine.Click'1、卡号是否存在 2、卡号是否上机,上机记录表 '3、计算消费金额:1、小于( 最少上机时间和准备时间)2、大于( 最少上机时间和准备时间)小于递增时间 (临时和固定)'4、更新上机记录表,卡表If txtCardNo.Text = "" ThenMsgBox("请输入卡号")ReturnEnd IfDim table1 As New DataTableDim Card As New Entity.EntityCardDim line As New Entity.LineLogEntityDim facade As New Facade.OffLineFacadeDim table2 As New DataTableDim basic As New Entity.EntityBasicDataSetCard.CardNo = txtCardNo.Text()'查询卡表获取卡类型等table1 = facade.selectOffF(Card)'查询基础数据表table2 = facade.selectBasicF(basic)Card.type = table1.Rows(0).Item(3)Card.Balance = table1.Rows(0).Item(2)line.OnDate = table1.Rows(0).Item(1)line.OffDate = Format(Now, "yyyy-MM-dd HH:mm:ss")basic.LeastTime = table2.Rows(0).Item(5)basic.UnitTime = table2.Rows(0).Item(4)basic.ReadyTime = table2.Rows(0).Item(6)basic.tmpRate = table2.Rows(0).Item(3)basic.Rate = table2.Rows(0).Item(2)Dim facade2 As New Facade.CashContext'计算消费时间line.mins = facade.ConsumeTimeF(basic, Card, line)'选择卡类型line.Cash = facade2.SelectType(basic, Card, line)Card.Balance = CDec(Card.Balance) - CDec(line.Cash)line.CardNo = Card.CardNoDim flag As Booleanflag = facade.offLine(line, Card)If flag = True ThenMsgBox("下机成功")txtCardNo.Text = line.CardNotxtBalance.Text = Card.BalancetxtType.Text = Card.typetxtStudentNo.Text = table1.Rows(0).Item(5)txtStudentName.Text = table1.Rows(0).Item(6)txtDepartment.Text = table1.Rows(0).Item(8)txtSex.Text = table1.Rows(0).Item(7)txtOnDate.Text = line.OnDatetxtOffDate.Text = line.OffDatetxtmins.Text = line.minstxtCash.Text = line.CashElseMsgBox("下机失败")End IfEnd Sub

三、外观层

1、职责连查看消费时间

    Public Function ConsumeTimeF(ByVal basic As Entity.EntityBasicDataSet, ByVal card As Entity.EntityCard, ByVal line As Entity.LineLogEntity) As IntegerDim preparetime As New BLL.PrepareTimeBLL(basic)Dim leasttime As New BLL.LeastTimeBLL(basic)Dim unittime As New BLL.UintTimeBLLpreparetime.setsuccessor(leasttime)leasttime.setsuccessor(unittime)Dim time As Integertime = DateDiff("n", line.OnDate, line.OffDate)Return preparetime.TimeRequest(time)End Function

2、策略模式选择卡类型

Public Class CashContextDim cashsuper As CashSuper'根据策略 不同,采用不同的计费方式Public Function SelectType(ByVal basic As Entity.EntityBasicDataSet, ByVal card As Entity.EntityCard, ByVal line As Entity.LineLogEntity) As SingleSelect Case card.type.Trim()Case "固定用户"cashsuper = New FixedUserBLL() '实例化固定用户策略Case "临时用户"cashsuper = New TmpUserBLL()End SelectReturn cashsuper.GetConsumMoney(basic, card, line)End Function


四、BLL层

1、职责连查看消费时间

(1)时间基类

Public MustInherit Class TimeBLLProperty successor As TimeBLLPublic Sub setsuccessor(ByVal successor As TimeBLL) '设置继承类Me.successor = successorEnd Sub'请求处理的抽象方法Public MustOverride Function TimeRequest(ByVal time As Integer) As IntegerEnd Class
(2)LeastTimeBLL类

Public Class LeastTimeBLL : Inherits TimeBLLProtected leastTime As IntegerPublic Sub New(ByVal basic As Entity.EntityBasicDataSet)Me.leastTime = CInt(basic.LeastTime) '将至少上机时间赋值为leastTimeEnd SubPublic Overrides Function TimeRequest(time As Integer) As IntegerIf time <= leastTime Then '如果上机时间小于至少上机时间,返回至少上机时间  Return leastTimeElseReturn successor.TimeRequest(time)End IfEnd Function
End Class
(3)PreparetimeBLL类 
Public Class PrepareTimeBLL : Inherits TimeBLLDim preparetime As Integer' Public Sub New是VB.net默认的构造函数  form_load是Form类在调用New构造函数后加载窗体绘图后才调用的方法 Public Sub New(ByVal basic As Entity.EntityBasicDataSet)Me.preparetime = CInt(basic.ReadyTime)  '传入准备时间End SubPublic Overrides Function TimeRequest(time As Integer) As IntegerIf time <= preparetime Then '如果上机时间小于准备时间,返回0Return 0ElseReturn successor.TimeRequest(time)End IfEnd Function
End Class

(4)Unittime类

Public Class UintTimeBLL : Inherits TimeBLL'正常消费Public Overrides Function TimeRequest(time As Integer) As IntegerReturn timeEnd Function
End Class

2、策略模式选择卡类型:

(1)固定用户

Public Class FixedUserBLL : Inherits CashSuper'固定用户Dim fixedRate As SinglePublic Overrides Function GetConsumMoney(ByVal basic As Entity.EntityBasicDataSet, ByVal card As Entity.EntityCard, ByVal line As Entity.LineLogEntity) As SinglefixedRate = Trim(Int(basic.Rate))Dim consumMoney As SingleconsumMoney = Trim(CSng(fixedRate) * CSng(line.mins * 1.0 / 60.0))If consumMoney < Trim(Int(basic.Rate)) ThenconsumMoney = Int(basic.Rate)End IfReturn consumMoneyEnd Function
End Class
(2)临时用户

Public Class TmpUserBLL : Inherits CashSuper  '临时用户Dim TmpRate As SinglePublic Overrides Function GetConsumMoney(basic As Entity.EntityBasicDataSet, card As Entity.EntityCard, line As Entity.LineLogEntity) As SingleTmpRate = basic.tmpRateDim consumMoney As SingleconsumMoney = Trim(CSng(TmpRate) * CSng(line.mins * 1.0 / 60.0))If consumMoney < Int(basic.tmpRate) ThenconsumMoney = Int(basic.tmpRate)End IfReturn consumMoneyEnd Function
End Class

五、DAL层

Public Class AdcountDAL : Implements IDAL.IAdcountPublic Function update(adcount As EntityAdCount) As Integer Implements IAdcount.updateDim sql As StringDim sqlhelper As New SQLHelper.sqlhelperDim flag As IntegerDim paras As SqlParameter() = {New SqlParameter("@UserID", adcount.UserID),New SqlParameter("@CancelCash", adcount.CancelCash),New SqlParameter("@CheckCash", adcount.CheckCash),New SqlParameter("@RechCash", adcount.RechCash),New SqlParameter("@Checkdate", adcount.Checkdate)}sql = "proc_Account"flag = sqlhelper.ExecAddDelUpdate(sql, CommandType.StoredProcedure, paras)Return flagEnd Function
End Class
存储过程:

ALTER PROCEDURE [dbo].[pro_Offline]@CardNO varchar(20),@OffDate datetime,@mins    int,@Cash    numeric(10, 2),@state  varchar(20),@Balance numeric(10, 2)
AS
BEGINupdate Y_LineLog_Info set OffDate=@OffDate  ,mins =@mins  ,Cash=@Cash ,state=@state where CardNo =@CardNO  update Y_Card_Info set Balance=Balance  where CardNo =@CardNO  END

总结:

下机用到了两个设计模式,其实设计到哪部分特别复杂就该考虑用设计模式去解耦合,设计模式还需要我们继续去研究。

这篇关于机房重构---下机(策略模式和职责连模式)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/850536

相关文章

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

RabbitMQ工作模式中的RPC通信模式详解

《RabbitMQ工作模式中的RPC通信模式详解》在RabbitMQ中,RPC模式通过消息队列实现远程调用功能,这篇文章给大家介绍RabbitMQ工作模式之RPC通信模式,感兴趣的朋友一起看看吧... 目录RPC通信模式概述工作流程代码案例引入依赖常量类编写客户端代码编写服务端代码RPC通信模式概述在R

利用Python实现时间序列动量策略

《利用Python实现时间序列动量策略》时间序列动量策略作为量化交易领域中最为持久且被深入研究的策略类型之一,其核心理念相对简明:对于显示上升趋势的资产建立多头头寸,对于呈现下降趋势的资产建立空头头寸... 目录引言传统策略面临的风险管理挑战波动率调整机制:实现风险标准化策略实施的技术细节波动率调整的战略价

SQL Server身份验证模式步骤和示例代码

《SQLServer身份验证模式步骤和示例代码》SQLServer是一个广泛使用的关系数据库管理系统,通常使用两种身份验证模式:Windows身份验证和SQLServer身份验证,本文将详细介绍身份... 目录身份验证方式的概念更改身份验证方式的步骤方法一:使用SQL Server Management S

Redis高可用-主从复制、哨兵模式与集群模式详解

《Redis高可用-主从复制、哨兵模式与集群模式详解》:本文主要介绍Redis高可用-主从复制、哨兵模式与集群模式的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录Redis高可用-主从复制、哨兵模式与集群模式概要一、主从复制(Master-Slave Repli

一文带你搞懂Redis Stream的6种消息处理模式

《一文带你搞懂RedisStream的6种消息处理模式》Redis5.0版本引入的Stream数据类型,为Redis生态带来了强大而灵活的消息队列功能,本文将为大家详细介绍RedisStream的6... 目录1. 简单消费模式(Simple Consumption)基本概念核心命令实现示例使用场景优缺点2

Redis中6种缓存更新策略详解

《Redis中6种缓存更新策略详解》Redis作为一款高性能的内存数据库,已经成为缓存层的首选解决方案,然而,使用缓存时最大的挑战在于保证缓存数据与底层数据源的一致性,本文将介绍Redis中6种缓存更... 目录引言策略一:Cache-Aside(旁路缓存)策略工作原理代码示例优缺点分析适用场景策略二:Re

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大