从事件处理程序中移出重复代码时的设计模式

时间:2012-12-21 08:39:49

标签: vb.net design-patterns

我有这段代码:

Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
    fTrafficSurveyA.incrementCount("Car")
    Call updateView()
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
    fTrafficSurveyA.incrementCount("Bicycle")
    Call updateView()
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
    fTrafficSurveyA.incrementCount("Lorry")
    Call updateView()
End Sub

我已将其更改为以下内容:

Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
    ButtonClickCode(CType(sender, Button))
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
    ButtonClickCode(CType(sender, Button))
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
    ButtonClickCode(CType(sender, Button))
End Sub

Private Sub ButtonClickCode(ByVal mySender As Button)

    Dim incrementVehicle As String
    Select Case mySender.Name
        Case "carButton"
            incrementVehicle = "Car"
        Case "bicycleButton"
            incrementVehicle = "Bicycle"
        Case "lorryButton"
            incrementVehicle = "Lorry"
        Case Else
            incrementVehicle = ""
    End Select

    fTrafficSurveyA.incrementCount(incrementVehicle)
    Call updateView()

End Sub

新代码包含更多代码 新版本是更好的练习,还是我应该遵循的第三种模式?


修改

使用Guffa的答案+其他评论我更改了基础TrafficSurveyA模型以包含枚举类型并更改了它的函数incrementCount,如此....这个枚举类型是否在正确的位置?< / p>

Enum vehicleType
    Car
    Lorry
    Bicycle
End Enum

Public Sub incrementCount(ByVal vehicle As vehicleType)
    ' Preconditions: none
    ' Postconditions: If vehicle is "Car", "Bicycle" or "Lorry" then 1 is added
    ' to the corresponding count. Otherwise nothing is done.

    Select Case vehicle
        Case vehicleType.Car : fCars = fCars + 1
        Case vehicleType.Bicycle : fBicycles = fBicycles + 1
        Case vehicleType.Lorry : fLorries = fLorries + 1
        Case Else 'do nothing
    End Select
End Sub

接口代码最终如下:

Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
    ButtonClickCode(TrafficSurveyA.vehicleType.Car)
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
    ButtonClickCode(TrafficSurveyA.vehicleType.Bicycle)
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
    ButtonClickCode(TrafficSurveyA.vehicleType.Lorry)
End Sub
Private Sub ButtonClickCode(ByVal incrementVehicle As TrafficSurveyA.vehicleType)

    fTrafficSurveyA.incrementCount(incrementVehicle)
    Call updateView()

End Sub

1 个答案:

答案 0 :(得分:2)

由于事件处理程序中的代码是identcal,因此您只能为所有事件设置一个事件处理程序:

Private Sub vehicleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click, bicycleButton.Click, lorryButton.Click
  Dim mySender As Button = CType(sender, Button)

  Dim incrementVehicle As String
  Select Case mySender.Name
    Case "carButton"
      incrementVehicle = "Car"
    Case "bicycleButton"
      incrementVehicle = "Bicycle"
    Case "lorryButton"
      incrementVehicle = "Lorry"
    Case Else
      incrementVehicle = ""
  End Select

  fTrafficSurveyA.incrementCount(incrementVehicle)
  Call updateView()

End Sub

另一种方法是保留单独的事件处理程序,并使用每个事件处理程序可以将正确的文本发送到公共方法的事实,因此您不需要检查控件名称来确定文本:

Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
  ButtonClickCode("Car")
End Sub

Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
  ButtonClickCode("Bicycle")
End Sub

Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
  ButtonClickCode("Lorry")
End Sub

Private Sub ButtonClickCode(ByVal incrementVehicle As String)
  fTrafficSurveyA.incrementCount(incrementVehicle)
  Call updateView()
End Sub