How to understand the difference between "Dependency Injection" and "Inverse of Control"?
Problem lies in lack of examples to "Inverse of Control" different than "Dependency Injection". So, below You can find a simple example to see the difference.
On car traffic, each car is "controlled" by a driver. On front of stoplights, cars line up waiting for green light. Drivers accelerate at the sight of green lights, but they do it with delay. This leads to a situation as shown:
Applying the Inverse of Control principle, stoplights could take "control" of car's acceleration and execute a synchronized and fast start on green light. So inverse of control for car-semaphore scenario looks like this: How is Dependency Injection related to Inverse of Control?? Well, on Dependency Injection you take the "control" over the new statement from developers to architect. Control on developer looks like this:
Problem lies in lack of examples to "Inverse of Control" different than "Dependency Injection". So, below You can find a simple example to see the difference.
On car traffic, each car is "controlled" by a driver. On front of stoplights, cars line up waiting for green light. Drivers accelerate at the sight of green lights, but they do it with delay. This leads to a situation as shown:
Applying the Inverse of Control principle, stoplights could take "control" of car's acceleration and execute a synchronized and fast start on green light. So inverse of control for car-semaphore scenario looks like this: How is Dependency Injection related to Inverse of Control?? Well, on Dependency Injection you take the "control" over the new statement from developers to architect. Control on developer looks like this:
public class DirectControl { private SomeAppInterface app = new MyFavoriteImplementation(8080); }With Dependency Injection, Architect take the control and You get:
import javax.inject.Inject; public class InverseControl { @Inject private SomeAppInterface app; }
Another good example of IoC witout DI is @PostConstruct/@PreDestroy. You provide methods whit this annotations, but don't care about when to call it. EJB container call this methods - it takes controll.
ReplyDeleteIoC is a general principle that drives/influences application architecture design.
ReplyDeleteDI is just one technique of achieving IoC. Others: events, AOP, Monad (functional languages).
As You mentioned DI inverses place of making decision about concrete type. Events are stronger - they inverse decision not only concrete type but also decision about number of cooperating objects and time of execution (asynch. event handling).
AOP is the strongest inverses everything:)
With strong inversion we loose control, so as always: dedicated hammer to specific class of the problem.
I don't know what do You mean by "software math model" but I can guess that You like abstract and theoretic stuff. If so than You can consider following statement: IoC is an idea, DI is one of it's reifications and CDI for example is an implementation of the DI reification of the IoC general concept:)