Tuesday, March 25, 2008

Inversion of Control vs Strategy Pattern

The Inversion of Control (IoC), also known as Dependent Injection (DI) , is orthogonal to the Strategy Pattern. Saying that they were the same pattern would be similar to saying that the Von Neumann Architecture and integrated circuit (IC) were the same thing.

The strategy pattern is one of many object oriented partitioning designs. It suggests how to divide business logics into separated components. Inversion of control (IoC), on the other hand, is one of many application wiring, configuring, and lifecycle controlling scenarios. It is about how to put separated business compnonents together into applications.

Application components partitioned in strategy pattern can be wired, configured, and controlled in various scenarios, not have to be the IoC. For instance, it is pretty common that applications use policy registries (therefore, a directory lookup scenario) to dynamically add, remove, resolve, and swap policy (strategy or algorithm) implementations.

Similarly, IoC has been widely used to assemble applications that are partitioned into components in various scnearios beyond the single strategy pattern. For instance, IoC are used to wire event suppliers/consumers, clients/services, implementations/adapters, etc..