Suppose I have a class that represents a product to be priced using one of a number of different pricing strategies. This pricing occurs hundreds of times per second, so to eliminate repetitive if/else statements I am instead using a delegate to launch the appropriate strategy, like so:
Private Delegate Sub PricingModel(ByVal params As PricingParameters)
Private myPricingModel As PricingModel
Private myPricingParameters As PricingParameters
Public Sub RunPricingModel()
myPricingModel(myPricingParameters)
End Sub
My question is this: if I want to be able to change the strategy, what do I do with myPricingModel? Currently I am simply setting it to a new PricingModel:
Public Sub SwitchStrategy(ByVal strategy As PricingStrategy)
Select Case strategy
Case PricingStrategy.STRATEGY_X
myPricingModel = New PricingModel(AddressOf PricingModelStrategyX)
Case PricingStrategy.STRATEGY_Y
myPricingModel = New PricingModel(AddressOf PricingModelStrategyY)
Case Else
Exit Sub
End Select
End Sub
But this doesn't look right to me (though it seems to work). Is there an accepted/better way of doing this? Or is this the standard way? Or is this just simply a doomed approach from the start?