[.Net Concept]適時採用事件動態繫結來替代用If判斷作功能的啟用
Private _enableLogData As Boolean
Public Property EnableLogData() As Boolean
Get
Return _enableLogData
End Get
Set(ByVal value As Boolean)
_enableLogData = value
End Set
End Property
Event Executing As EventHandler
Event Executed As EventHandler
Protected Sub OnExecuting(ByVal e As EventArgs)
RaiseEvent Executing(Me, e)
End Sub
Protected Sub OnExecuted(ByVal e As EventArgs)
RaiseEvent Executed(Me, e)
End Sub
Sub GoExecute()
For i As Integer = 1 To 100000000
OnExecuting(New EventArgs)
'運行動作,這邊為了看出差異,故不做動作
If EnableLogData Then
'紀錄動作,這邊為了看出差異,故不做動作
End If
OnExecuted(New EventArgs)
Next
End Sub
End Class
executor.EnableLogData = True
sw.Reset()
sw.Start()
executor.GoExecute()
Console.WriteLine(sw.ElapsedMilliseconds.ToString)
End Sub</pre></div>
Private _enableLogData As Boolean
Public Property EnableLogData() As Boolean
Get
Return _enableLogData
End Get
Set(ByVal value As Boolean)
If _enableLogData <> value Then
OnEnableLogDataChanging(New EventArgs)
_enableLogData = value
OnEnableLogDataChanged(New EventArgs)
End If
End Set
End Property
Event EnableLogDataChanging As EventHandler
Event EnableLogDataChanged As EventHandler
Event Executing As EventHandler
Event Executed As EventHandler
Protected Sub OnEnableLogDataChanging(ByVal e As EventArgs)
RaiseEvent EnableLogDataChanging(Me, e)
End Sub
Protected Sub OnEnableLogDataChanged(ByVal e As EventArgs)
RaiseEvent EnableLogDataChanged(Me, e)
End Sub
Protected Sub OnExecuting(ByVal e As EventArgs)
RaiseEvent Executing(Me, e)
End Sub
Protected Sub OnExecuted(ByVal e As EventArgs)
RaiseEvent Executed(Me, e)
End Sub
Sub GoExecute()
For i As Integer = 1 To 100000000
OnExecuting(New EventArgs)
'運行動作,這邊為了看出差異,故不做動作
OnExecuted(New EventArgs)
Next
End Sub
Private Sub LogData(ByVal sender As Object, ByVal e As System.EventArgs)
'紀錄動作,這邊為了看出差異,故不做動作
End Sub
Private Sub Executor_EnableLogDataChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.EnableLogDataChanged
RemoveHandler Executed, AddressOf LogData
If EnableLogData Then
AddHandler Executed, AddressOf LogData
End If
End Sub
End Class