码迷,mamicode.com
首页 > 其他好文 > 详细

“模”法无边—策略模式+简单工厂实现下机收费

时间:2014-08-16 21:09:51      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   使用   io   strong   ar   cti   

  下机收费是一种算法,临时用户和学生下机下机就是两种算法,让我想到了使用策略模式,根据是否为临时用户和学生,选择具体的算法,这属于简单工厂,好的,简单工厂+策略模式实现下机收费

策略类-封装具体的策略

 

‘封装具体的算法,需要引用一个算法

Public Class consumeContext

    Dim strategyconsume As Consume

 

    Sub New(ByRef strategy As Consume)

        Me.strategyconsume = strategy

    End Sub

 

    Public Function ContextInterface() As Double

 

        Return strategyconsume.getConsume()

    End Function

 

End Class

算法父类-提供可重写的方法和公共成员

 

Public Class Consume

 

    Protected line As LineInfo

    Protected consumemoney As Double

    Protected chargestandard As ChargeStandardInfo

    Protected count As Int16

 

    Public Overridable Function getConsume() As Double

 

    End Function

End Class

 

算法A

Public Class tempConsume

 

    Inherits Consume

 

    Sub New(ByRef line As LineInfo, ByRef chargestandard As ChargeStandardInfo)

        Me.line = line

 

        Me.chargestandard = chargestandard

    End Sub

 

    Public Overrides Function getConsume() As Double

 

        If line.ConsumeTime Mod (Convert.ToInt16(chargestandard.UnitTime)) <> 0 Then

            consumemoney = (line.ConsumeTime \ chargestandard.UnitTime + 1) * chargestandard.TempCharge

        Else

            consumemoney = (line.ConsumeTime \ chargestandard.UnitTime) * chargestandard.TempCharge

        End If

 

        Return consumemoney

 

    End Function

 

End Class

 

算法B

Public Class fixComsume

 

    Inherits Consume

 

    Sub New(ByRef line As LineInfo, ByRef chargestandard As ChargeStandardInfo)

        Me.line = line

 

        Me.chargestandard = chargestandard

    End Sub

 

    Public Overrides Function getConsume() As Double

 

        If line.ConsumeTime Mod (Convert.ToInt16(chargestandard.UnitTime)) <> 0 Then

            consumemoney = (line.ConsumeTime \ chargestandard.UnitTime + 1) * chargestandard.FixCharge

        Else

            consumemoney = (line.ConsumeTime \ chargestandard.UnitTime) * chargestandard.FixCharge

        End If

 

        Return consumemoney

    End Function

 

End Class

 

客户端代码

    ‘ 得到收费金额,虽然放在B层,但相当于客户端代码

    Public Function getConsume(ByRef member As StudentInfo, ByRef line As LineInfo, ByRef chargestandard As ChargeStandardInfo) As Double

 

        Select Case member.IsOnLine

 

            Case "True"

 

                Dim stragey As New fixComsume(line, chargestandard)

 

                Dim cashcontext As New consumeContext(stragey)

 

                Return cashcontext.ContextInterface()

 

            Case "False"

 

                Dim stragey As New tempConsume(line, chargestandard)

 

                Dim cashcontext As New consumeContext(stragey)

 

                Return cashcontext.ContextInterface()

 

        End Select

 

    End Function

 

End Class

 

设计模式在我看来是很高大上的东西,“只可远观不可亵玩”,这次在别的同学都用的情况下,自己也尝试使用了一下设计模式(虽然用的是最简单的),觉得设计模式也可以是很接地气的东西,大家,大胆的用啊!

“模”法无边—策略模式+简单工厂实现下机收费,布布扣,bubuko.com

“模”法无边—策略模式+简单工厂实现下机收费

标签:des   style   color   使用   io   strong   ar   cti   

原文地址:http://blog.csdn.net/tgbsqliuying/article/details/38616217

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!