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
- Implement
Dependency
orSingletonDependency
interface. - Implement
DependencyProvider
that know how to create previous dependency. - 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());
- Locate this dependency where and when you want to use:
1
MyDependency dependency = (MyDependency)DependencyLocator.locate(MyDependency.class);
- 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:
|
|
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.