002Visual Basic 策略模式

 


1.窗体

2.上下文

Public Class CashContext
    Private mCashsuper As CashSuper
    
    Public Sub New(ByVal type As String)
        Select Case type
            Case "正常收费"
                Dim cs0 As CashNormal = New CashNormal("正常收费")
                mCashsuper = cs0
            Case "满300返100"
                Dim cs1 As CashReturn = New CashReturn("300", "100")
                mCashsuper = cs1
            Case "打8折"
                Dim cs2 As CashRebate = New CashRebate("0.8")
                mCashsuper = cs2
        End Select
    End Sub
    Public Function GetResult(ByVal money As Double) As Double
        Return mCashsuper.acceptCash(money)
    End Function
End Class

3.抽象策略和具体策略 

'现金收费抽象类
Public Class CashSuper
    Public Overridable Function acceptCash(ByVal money As Double) As Double
        Return money
    End Function
End Class

'正常收费
Public Class CashNormal
    Inherits CashSuper
    Dim myMoneyRebate As Double
    Public Sub New(ByVal moneyRebate As String)
        myMoneyRebate = 1
    End Sub
    Public Overrides Function acceptCash(ByVal money As Double) As Double
        Return money * myMoneyRebate
    End Function
End Class

'打折收费
Public Class CashRebate
    Inherits CashSuper
    Dim myMoneyRebate As Double
    Public Sub New(ByVal moneyRebate As String)
        myMoneyRebate = Double.Parse(moneyRebate)
    End Sub

    Public Overrides Function acceptCash(ByVal money As Double) As Double
        Return money * myMoneyRebate
    End Function
End Class

Public Class CashReturn
    Inherits CashSuper
    Private myMoneyCondition As Double
    Private myMoneyReturn As Double
    Public Sub New(ByVal moneycondition As String, ByVal moneyReturn As String)
        myMoneyCondition = Double.Parse(moneycondition)
        myMoneyReturn = Double.Parse(moneyReturn)
    End Sub

    Public Overrides Function acceptCash(ByVal money As Double) As Double
        Dim result As Double
        result = money
        If money >= myMoneyCondition Then
            result = money - Math.Floor(money / myMoneyCondition) * myMoneyReturn
        End If
        Return result
    End Function
End Class

4.客户端代码

Public Class Form1
    Dim total As Double = 0

    Private Sub txtOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtOk.Click
        Dim myCashcontext As New CashContext(ComboBox1.SelectedItem.ToString)
        Dim totalPrice As Double
        Dim tmp As Double = Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtCount.Text)
        totalPrice = myCashcontext.getresult(tmp)

        ListBox1.Items.Add("单价:" + txtPrice.Text + "数量:" + txtCount.Text + "合计:" + totalPrice.ToString)

        total += totalPrice
        lblTotlePrice.Text = total.ToString
    End Sub

End Class

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ngbshzhn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值