In this section we will discuss about how IOC and DI can help us build loosely coupled software architecture. I am not sure should we call this a design pattern or more of a approach. If you search around the web you will see lot of controversy on whether IOC is a design pattern or not. From my point of view it is a design pattern as it solves a problem context.
It would be great to see actual architectures implementing IOC using container oriented approaches. I am sure it will change the way we think about interaction between components.
So let us understand in detail about IOC and DI.
The Problem - Tight Coupling
Before even we get in to abbreviation of IOC and DIP, let's first understand the problem. Consider the below example we have a customer class which contains an address class object. The biggest issue with the code is tight coupling between classes. In other words the customer class depends on the address object. So for any reason address class changes it will lead to change and compiling of 'ClsCustomer' class also. So let's put down problems with this approach:
- The biggest problem is that customer class controls the creation of address object.
- Address class is directly referenced in the customer class which leads to tight coupling between address and customer objects.
- Customer class is aware of the address class type. So if we add new address types like home address, office address it will lead to changes in the customer class also as customer class is exposed to the actual address implementation.
Figure: - Problems of IOC
So if for any reason the address object is not able to create the whole customer class will fail in the constructor initialization itself.
Solution
Now that we know the issue, let's understand the solution. The solution definitely revolves around shifting the object creation control from the customer class to some one else. The main problem roots from the customer class creating the address object. If we are able to shift this task / control of object creation from the customer class to some other entity we have solved our problem. In other sentence if we are able to invert this control to a third party we have found our solution. So the solution name is IOC (Inversion of control).
Principles of IOC
The basic principle of IOC stands on the base of Hollywood principle (response given to amateurs auditioning in Hollywood):
Do not call us we will call you
Translating to bollywood (for struggling actors)
Aap Mauke ko mat bulao, mauka aap ke paas ayega - Hindi conversion ?
In other words it like address class saying to the customer class, do not create me I will create myself using some one else.
There are two principles of IOC:
- Main classes aggregating other classes should not depend on the direct implementation of the aggregated classes. Both the classes should depend on abstraction. So the customer class should not depend directly on the address class. Both address and customer class should depend on an abstraction either using interface or abstract class.
- Abstraction should not depend on details, details should depend on abstraction.
Figure: - IOC framework
Figure 'IOC framework' shows how we can achieve this decoupling. The simplest way would be to expose a method which allows us to set the object. Let the address object creation be delegated to the IOC framework. IOC framework can be a class, client or some kind of IOC container. So it will be two step procedure IOC framework creates the address object and passes this reference to the customer class.
Ways of implementing IOC
Ok, now we know the problem, let's try to understand the broader level solution. Let's look at how we implement the solution for IOC. IOC is implemented using DI (Dependency injection). We have discussed on a broader level about how to inject the dependency in the previous sections. In this section we will dive deeper in to other ways of implementing DI.

Figure: - IOC and DI
Figure 'IOC and DI' shows how IOC and DI are organized. So we can say IOC is a principle while DI is a way of implementing IOC. In DI we have four broader ways of implementing the same:
- Constructor way
- Exposing setter and getter
- Interface implementation
- Service locator
In the further sections we will walkthrough the same in more detail.
Constructor Methodology
In this methodology we pass the object reference in the constructor itself. So when the client creates the object he passes the object in the constructor while the object is created. This methodology is not suited for client who can only use default constructors.
Figure: - Constructor based DI
Setter and Getter
This is the most commonly used DI methodology. The dependent objects are exposed through set/get methods of classes. The bad point is because the objects are publicly exposed it breaks the encapsulation rule of object oriented programming.
Figure: - Getter and Setter













steve KrizanovicPosted May 25, 2011, 1:19 AM
Hi, Can you please explain the purpose or benefit of applying this pattern. I dont really understand why you would want to decouple naturally coupled component, or did you use the customer / address as an example only? I think by decoupling the code in this fasion and allowing users to customise the dependencies to be injected through the config, we are opening up a world of hurt. In the customer / address example, say a user does not realise there is a dependancy there and removed address. There is no recompiling, no complie time exceptions as everything is decoupled anyway, just an application which crashes at runtime. Surely this is not beneficial when developing an application.