Search code examples
design-patternsdependency-injectionstrategy-pattern

who should instantiate and assign runtime strategy implementations?


When implementing the strategy pattern, how do you determine which class is responsible for:

  1. Selecting the specific concrete strategy implementation to pass to the Context class (assuming that the selection is based on some complex business logic and not a static flag)

  2. Instantiating the aforementioned concrete implementation and actually injecting it into the Context class

It feels like there ought to be some objective guidance out there that covers this. I've done some reading on various OOP patterns (i.e. GRASP and SOLID) but I still don't have a lot of clarity around this particular question.


Solution

  • This is very close to the issues I was having with a DI container. Have a look here, Mark Seemann has provided a great answer.