策略模式没有用在机房的下机结账,因为自己把临时用户给删了,为了使用策略模式就在充值功能上用到了。
UI层
''' <summary>
''' 确定充值
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
'判断控件是否为空
If txtCardNo.Text = "" And txtPrint.Text = "" And txtRecharge.Text = "" Then
Else
Dim cc As CashContext
cc = New CashContext(cmbCashContext.Text)
result = cc.GetResult(Double.Parse(txtRecharge.Text))
'卡号是否存在 是否退卡
Dim CardInfo As New Entity.RechargeEntity
CardInfo.CardNo = txtCardNo.Text.Trim
CardInfo.State = "使用"
Dim Facade As New Facade.SeekCardFacade
Dim table As DataTable
table = Facade.SeekCard(CardInfo)
If table.Rows.Count = 0 Then
MsgBox("没有此卡号或已经退卡!")
Else
'更改金额
Dim RechargeCardInfo As New Entity.RechargeEntity
RechargeCardInfo.CardNo = txtCardNo.Text.Trim
RechargeCardInfo.RechCash = table.Rows(0).Item(1) + result
RechargeCardInfo.State = "使用"
Dim RechCashFacade As New Facade.RechargeFacade
Dim strResult As Boolean
strResult = RechCashFacade.RechargeCard(RechargeCardInfo)
If strResult = False Then
MsgBox("没有此卡号!")
Else
'添加充值记录
Dim RechargeInfo As New Entity.RechargeEntity
RechargeInfo.CardNo = txtCardNo.Text.Trim
RechargeInfo.RechCash = result
RechargeInfo.RechTime = FrmLogin.DayTime
RechargeInfo.Head = FrmLogin.AdminID
RechargeInfo.Computer = FrmLogin.Comptername 'FrmLogin .Computer
Dim RechargeFacade As New Facade.RechargeFacade
Dim Rresult As Boolean
Rresult = RechargeFacade.Recharge(RechargeInfo)
If Rresult = False Then
MsgBox("没有此卡号!")
Else
txtPrint.Text = "充值卡号为:" & RechargeInfo.CardNo & vbCrLf & vbCrLf & "充值金额是:" & RechargeInfo.RechCash + vbCrLf & vbCrLf & "充值时间为:" & Now & vbCrLf & vbCrLf & "充值老师为:" & RechargeInfo.Head
MsgBox("充值成功!")
txtCardNo.Text = ""
txtRecharge.Text = ""
End If
End If
End If
End If
End Sub
Private Sub frmRecharge_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'comboBox控件中添加选择内容
Dim FieldRelation() As String '定义存储变量
FieldRelation = {"正常充值", "打折1.2", "冲200增50"} '存储变量中添加内容
cmbCashContext.Items.AddRange(FieldRelation) '添加内容
End Sub
在Facade层写的策略模式
Public Class CashContext
Public Cashsuper As CashSuper = Nothing
Public Sub New(ByVal cardtype As String)
Select Case cardtype
Case "正常充值"
Cashsuper = New CashNorMal()
Case "打折1.2"
Cashsuper = New CashRebate("1.2")
Case "冲200增50"
Cashsuper = New CashReturn("200", "50")
Case ""
Cashsuper = Nothing
End Select
End Sub
Public Function GetResult(ByVal money As Double) As Double
Return Cashsuper.acceptCash(money)
End Function
End Class
''' <summary>
''' 抽象策略模式
''' </summary>
Public MustInherit Class CashSuper
Public MustOverride Function acceptCash(ByVal money As Double) As Double
End Class
''' <summary>
''' 正常收费,继承CashSuper
''' </summary>
Public Class CashNorMal : Inherits CashSuper
Public Overrides Function acceptCash(ByVal money As Double) As Double
Return money
End Function
End Class
''' <summary>
''' 打折收费,继承CashSuper
''' </summary>
Public Class CashRebate : Inherits CashSuper
Public MoneyRebate As Double = 1D
'通过构造函数传递参数
Public Sub New(ByVal moneyRebate As String)
Me.MoneyRebate = Double.Parse(moneyRebate)
End Sub
Public Overrides Function acceptCash(money As Double) As Double
Return money * MoneyRebate
End Function
End Class
''' <summary>
''' 返利收费,继承CashSuper
''' </summary>
Public Class CashReturn : Inherits CashSuper
Private moneyCondition As Double = 0D
Private moneyReturn As Double = 0D
Public Sub New(ByVal moneyCondition As String, ByVal moneyReturn As String)
Me.moneyCondition = Double.Parse(moneyCondition)
Me.moneyReturn = Double.Parse(moneyReturn)
End Sub
Public Overrides Function acceptCash(money As Double) As Double
Dim result As Double = money
If money >= moneyCondition Then
result = money - Math.Floor((money / moneyCondition)) * moneyReturn
End If
Return result
End Function
End Class
这个策略模式就完成了,其他层的代码该咋写咋写,就是在充值金额上做了变动,其他的还是按照原来的7层写就好了。
在UI层中凡是涉及到逻辑判断的都应该放到BLL层判断的,但是当时自己知道的比较少,改和重写没有区别就不改了,这里也是可以用职责链的。
我们在机房重构中无意中用到了很多的设计模式,7层中的Facade层实际上就是外观模式的应用,但是我们没有意识到;还有Factory层,也是一个设计模式的应用,反射+配置文件实现数据的访问。(是不是抽象工厂就不了解了)。