Replace conditional (VB.NET: Select-Case) with Strategy Pattern... HOW?!
Alright folks,... let's get to the point: Buzz words sometimes help to understand that there is something that needs to be improved. What might be even more helpful is a hint on HOW to actually improve something.
Like this one.
Anyone knows that a good developer follows the open-closed principle. Open to extension, closed to change. Select-Case in VB.NET for instance discounts this.
If you have something like:
Select Case customer.Type
You will end up in having to change your existing implementation each time a new type of customer arises. The strategy pattern can help in implementing something to avoid this potential risk.
I took the example on this website: http://blogs.microsoft.co.il/blogs/gilf/archive/2009/11/22/applying-strategy-pattern-instead-of-using-switch-statements.aspx
It works pretty well. However,... now I need to change the implementation of the Context class to add new types to an internal Dictionary. Also it doesn't handle the Case Else.
Can anyone please introduce an example of how to apply the Strategy Pattern in a way that allows me to replace Conditionals with a construct that renders changes in existing code completely unnecessary and even handles Case Else?
That'd be awesome!
Christian Jacob, Aug 12, 2010
Could it be an appropriate way to use reflection to find all classes that inherit from my strategyclass, instantiate and add them dynamically to my dictionary at runtime? And if so,... how does this work?
Aug 13, 2010
It's been some time, but in the meantime, I took this sample and wrote a little virtual lab that tries to explain how this stuff works. I also included Roberts' suggestion for a reflective way of introducing changeless context classes. Great solution, Robert!
If you like the documentation, comment on it. If anyone has suggestions or any criticism: Go for it. I love to improve my own work. :)
Christian Jacob, Jul 05, 2013
This post is a continuation from the previous one, which shows how to get all classes that implements the IPassengerTitleStrategy and create and object of them and put them in a list which could be used to databind to a drop down list.
The following method will populate an IList whith implementations of type T.
This is how you can retrieve a list of objects from the classes that implements the interface IPassengerTitleStrategy:
Robert Blixt, Aug 15, 2010
This is interesting... I have taken a somewhat different approach to the
strategy pattern than what is presented on that page. First I have removed
the need for enums, as I try to avoid them if possible for maintainability concerns.
The context is now a full blown object using no statics.
The default behavior is initialized in the constructor, this should be equivalent
with the 'case else'.
To change the behavior of the context you provide a new IPassengerTitleStrategy
for the constructor. In this implementation it isn't possible to change behavior
after the object was created but that is easily fixed by adding a property to
Also, if you need new behavior just add another class that inherits from the
IPassengerTitleStrategy, there is no need to modify any enums or the Context
class at all.
Let me now if I completely missed the intent of you question :o)
Robert Blixt, Aug 13, 2010