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