Dependency inversion on Java and Android using the classic service locator pattern. For the development proccess of this project I have followed the Test-driven development (TDD) approach. I was very interested in creating a good set of tests to ensure that it works well in any case as it is a critical piece of the software architecture.

The library is free software (Apache-2.0) and you can see the code on GitHub.

Key Features

  • Easy to add and remove.
  • Lazy creation.
  • Thread safe.
  • Testing friendly.
  • Singleton.
  • Releasable.
  • Well tested.

Quickstart

  1. Implement Dependency or SingletonDependency interface.
  2. Implement DependencyProvider that know how to create previous dependency.
  3. Supply these providers to the DependencyLocator in some point before use the dependencies, for instance at the entry point of your application.
    1
    
    DependencyLocator.provide(MyDependency.class, new MyDependencyProvider());
    
  4. Locate this dependency where and when you want to use:
    1
    
    MyDependency dependency = (MyDependency)DependencyLocator.locate(MyDependency.class);
    
  5. Optional: Release this dependency when you do not need use it anymore:
    1
    
    DependencyLocator.release(dependency);
    

Sample Project

Check this sample project with three dependencies, one of them nested.

Download

Jitpack

If you use gradle, you just have to add the following in your build.gradle file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
allprojects {
  repositories {
  ...
      maven { url 'https://jitpack.io' }
   }
}
...
dependencies {
   compile 'com.github.nfdz:DependencyLocator:v1.0.0'
}

Jitpack works with several build systems, please checkout the documentation if you need help with yours.

Manual JAR

Download the JAR library from the GitHub Releases of the repository.