【重构】策略和职责链下机

前言:

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

内容:

 

一、各个层调用关系:

二、UI层代码:

Private Sub btnOffLine_Click(sender As Object, e As EventArgs) Handles btnOffLine.Click

'1、卡号是否存在 2、卡号是否上机,上机记录表

'3、计算消费金额:1、小于( 最少上机时间和准备时间)2、大于( 最少上机时间和准备时间)小于递增时间 (临时和固定)

'4、更新上机记录表,卡表


If txtCardNo.Text = "" Then

MsgBox("请输入卡号")

Return

End If

Dim table1 As New DataTable

Dim Card As New Entity.EntityCard

Dim line As New Entity.LineLogEntity

Dim facade As New Facade.OffLineFacade

Dim table2 As New DataTable

Dim basic As New Entity.EntityBasicDataSet


Card.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.CardNo


Dim flag As Boolean

flag = facade.offLine(line, Card)

If flag = True Then

MsgBox("下机成功")

txtCardNo.Text = line.CardNo

txtBalance.Text = Card.Balance

txtType.Text = Card.type

txtStudentNo.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.OnDate

txtOffDate.Text = line.OffDate

txtmins.Text = line.mins

txtCash.Text = line.Cash


Else

MsgBox("下机失败")

End If


End Sub

三、外观层

 

1、职责连查看消费时间

Public Function ConsumeTimeF(ByVal basic As Entity.EntityBasicDataSet, ByVal card As Entity.EntityCard, ByVal line As Entity.LineLogEntity) As Integer

Dim preparetime As New BLL.PrepareTimeBLL(basic)

Dim leasttime As New BLL.LeastTimeBLL(basic)

Dim unittime As New BLL.UintTimeBLL


preparetime.setsuccessor(leasttime)

leasttime.setsuccessor(unittime)


Dim time As Integer

time = DateDiff("n", line.OnDate, line.OffDate)

Return preparetime.TimeRequest(time)


End Function

2、策略模式选择卡类型

Public Class CashContext

Dim cashsuper As CashSuper

'根据策略 不同,采用不同的计费方式

Public Function SelectType(ByVal basic As Entity.EntityBasicDataSet, ByVal card As Entity.EntityCard, ByVal line As Entity.LineLogEntity) As Single

Select Case card.type.Trim()

Case "固定用户"

cashsuper = New FixedUserBLL() '实例化固定用户策略


Case "临时用户"

cashsuper = New TmpUserBLL()

End Select

Return cashsuper.GetConsumMoney(basic, card, line)


End Function




四、BLL层

1、职责连查看消费时间

(1)时间基类




Public MustInherit Class TimeBLL

Property successor As TimeBLL

Public Sub setsuccessor(ByVal successor As TimeBLL) '设置继承类

Me.successor = successor

End Sub

'请求处理的抽象方法

Public MustOverride Function TimeRequest(ByVal time As Integer) As Integer




End Class

(2)LeastTimeBLL类

Public Class LeastTimeBLL : Inherits TimeBLL

Protected leastTime As Integer


Public Sub New(ByVal basic As Entity.EntityBasicDataSet)

Me.leastTime = CInt(basic.LeastTime) '将至少上机时间赋值为leastTime

End Sub


Public Overrides Function TimeRequest(time As Integer) As Integer

If time <= leastTime Then '如果上机时间小于至少上机时间,返回至少上机时间

Return leastTime

Else

Return successor.TimeRequest(time)

End If

End Function

End Class

(3)PreparetimeBLL类 

Public Class PrepareTimeBLL : Inherits TimeBLL

Dim 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 Sub



Public Overrides Function TimeRequest(time As Integer) As Integer

If time <= preparetime Then '如果上机时间小于准备时间,返回0

Return 0

Else

Return successor.TimeRequest(time)

End If


End Function

End Class

(4)Unittime类​​​​​​​

Public Class UintTimeBLL : Inherits TimeBLL


'正常消费

Public Overrides Function TimeRequest(time As Integer) As Integer

Return time

End Function

End Class

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

 

(1)固定用户

Public Class FixedUserBLL : Inherits CashSuper

'固定用户

Dim fixedRate As Single

Public Overrides Function GetConsumMoney(ByVal basic As Entity.EntityBasicDataSet, ByVal card As Entity.EntityCard, ByVal line As Entity.LineLogEntity) As Single

fixedRate = Trim(Int(basic.Rate))

Dim consumMoney As Single

consumMoney = Trim(CSng(fixedRate) * CSng(line.mins * 1.0 / 60.0))

If consumMoney < Trim(Int(basic.Rate)) Then

consumMoney = Int(basic.Rate)

End If

Return consumMoney


End Function

End Class

(2)临时用户



Public Class TmpUserBLL : Inherits CashSuper '临时用户


Dim TmpRate As Single

Public Overrides Function GetConsumMoney(basic As Entity.EntityBasicDataSet, card As Entity.EntityCard, line As Entity.LineLogEntity) As Single

TmpRate = basic.tmpRate

Dim consumMoney As Single

consumMoney = Trim(CSng(TmpRate) * CSng(line.mins * 1.0 / 60.0))

If consumMoney < Int(basic.tmpRate) Then

consumMoney = Int(basic.tmpRate)

End If

Return consumMoney

End Function

End Class


五、DAL层

Public Class AdcountDAL : Implements IDAL.IAdcount




Public Function update(adcount As EntityAdCount) As Integer Implements IAdcount.update

Dim sql As String

Dim sqlhelper As New SQLHelper.sqlhelper

Dim flag As Integer


Dim 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 flag


End Function

End Class

 

  1. 存储过程:

    ALTER PROCEDURE [dbo].[pro_Offline]
    
    @CardNO varchar(20),
    
    @OffDate datetime,
    
    @mins int,
    
    @Cash numeric(10, 2),
    
    @state varchar(20),
    
    @Balance numeric(10, 2)
    
    AS
    
    BEGIN
    
    
    update 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
    
    

     

 
  1. 总结:

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值