Individual modules are interconnected using the Dependency Injection design pattern. The Google Guice library is used for this. The practical consequence is that it is easier to create separate tests of individual modules that are embedded in the test environment.
The Google Guice library has very good documentation, so we will only mention the necessary minimum here.

  • Instances of objects (only large objects that represent individual modules) are created by the Injector by calling .getInstance(Class<T>). The call parameter specifies the type of the new instance and should be an interface so that different implementations of this type can be cast in different circumstances
  • The Injector follows the configuration defined by the AbstractModule object. A specific implementation of a "module" with .bind(Class<T>).to(Class<? extends T>) calls defines specific classes that implement the required interfaces. For example:

.bind(Main.class).to(MainImpl.class)
If a specific procedure is to be used to create an instance, the module can implement that procedure with a method annotated with the @Provides annotation.

  • The implementing class must have a public standard constructor or a constructor marked with the @Inject annotation, whose parameters are all interfaces for which the module defines assignments. The Injector then supplies the individual instances as arguments.
  • The Injector can also resolve circular dependencies by creating a placeholder object.


Napíšte komentár