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

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

相关文章

Deepseek使用指南与提问优化策略方式

《Deepseek使用指南与提问优化策略方式》本文介绍了DeepSeek语义搜索引擎的核心功能、集成方法及优化提问策略,通过自然语言处理和机器学习提供精准搜索结果,适用于智能客服、知识库检索等领域... 目录序言1. DeepSeek 概述2. DeepSeek 的集成与使用2.1 DeepSeek API

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

SpringBoot中的404错误:原因、影响及解决策略

《SpringBoot中的404错误:原因、影响及解决策略》本文详细介绍了SpringBoot中404错误的出现原因、影响以及处理策略,404错误常见于URL路径错误、控制器配置问题、静态资源配置错误... 目录Spring Boot中的404错误:原因、影响及处理策略404错误的出现原因1. URL路径错

Redis多种内存淘汰策略及配置技巧分享

《Redis多种内存淘汰策略及配置技巧分享》本文介绍了Redis内存满时的淘汰机制,包括内存淘汰机制的概念,Redis提供的8种淘汰策略(如noeviction、volatile-lru等)及其适用场... 目录前言一、什么是 Redis 的内存淘汰机制?二、Redis 内存淘汰策略1. pythonnoe

Python 中 requests 与 aiohttp 在实际项目中的选择策略详解

《Python中requests与aiohttp在实际项目中的选择策略详解》本文主要介绍了Python爬虫开发中常用的两个库requests和aiohttp的使用方法及其区别,通过实际项目案... 目录一、requests 库二、aiohttp 库三、requests 和 aiohttp 的比较四、requ

Redis过期键删除策略解读

《Redis过期键删除策略解读》Redis通过惰性删除策略和定期删除策略来管理过期键,惰性删除策略在键被访问时检查是否过期并删除,节省CPU开销但可能导致过期键滞留,定期删除策略定期扫描并删除过期键,... 目录1.Redis使用两种不同的策略来删除过期键,分别是惰性删除策略和定期删除策略1.1惰性删除策略

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序