The Dependency Injection pattern (see the article Fowler, 2004) allows you to inject implementation objects into a class hierarchy in a consistent way. This is useful when classes delegate specific tasks to objects referenced in fields. These fields have abstract types (that is, interfaces or abstract classes) so that the dependency on actual implementation classes is removed.
In this first section, we will briefly show some Java examples that use Google Guice. Of course, all the injection principles naturally apply to Xtend as well.
Note
If you want to try the following examples yourself, you need to create a new Plug-in Project, for example, org.example.guice
and add com.google.inject
and javax.inject
as dependencies in the MANIFEST.MF
.
Let's consider a possible scenario: a Service
class that abstracts from the actual implementation of a Processor
class and a Logger
class. The following is a possible implementation:
public class Service { private Logger logger; private...