1.5. Inversion Of Control

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

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

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

This pattern has several names:

How?

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

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 cannot be stored in the container, then the service locator pattern is used:

public ServiceA(){

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

Side effects

  • Ease Unit test (use of Mock objects)

  • Ease Maintainability

  • Ease Refactoring

  • Component reuse ( POJOs != EJBs)

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus