Inversion of Control

Inversion Of Control

1 Overview

The services are not responsible for the instantiation of the components they depend on.

This architecture provides a loosely coupled design where the implementation of dependant services can be transparently exchanged.

This pattern has several names :

  • Hollywood principle : "don't call me, I will call you"
  • Inversion of Control
  • Dependency injection

2 How

Not to let the object create itself the instances of the object it references. This job is delegated to the container (assembler in the picture).

ioc.gif

3 Injection

There are two ways to inject a dependency :

using a constructor:

public ServiceA(ServiceB serviceB)

using setter methods:

public void setServiceB(ServiceB serviceB)

When a client service can not be stored in the container then the service locator pattern is used:

public ServiceA(){
          this.serviceB =Container.getSInstance().getService(ServiceB.class);
        }

4 Side effects

  • Ease Unit test (use of Mock objects)
  • Ease Maintainability
  • Ease Refactoring
  • Component reuse ( POJOs != EJBs)
Tags:
Created by Benjamin Mestrallet on 05/22/2007
Last modified by Hoat Le on 04/02/2009

Products

generated on Thu Sep 02 15:49:34 UTC 2010

eXo Optional Modules

eXo Core Foundations


Copyright (c) 2000-2010. All Rights Reserved - eXo platform SAS
2.4.30451