onCreate()
small number of dependencies through the entry point ( main()
or onCreate()
method) into the program is very reasonable. However, in many projects there are many classes, each of which has different dependencies that must be satisfied. Creating an instance and connecting everything together takes a large amount of code. Even worse, this code will constantly change every time new classes are added to the application and existing classes change to introduce new dependencies.BattleOfBastards
) will probably need the help of the Allies ( Allies
). Also, the Iron Bank ( IronBank
) will finance the house. The modified main method will be as follows: public class BattleOfBastards { public static void main(String[] args){ IronBank bank = new IronBank(); Allies allies = new Allies(bank); Starks starks = new Starks(allies, bank); Boltons boltons = new Boltons(allies, bank); War war = new War(starks, boltons); war.prepare(); war.report(); } }
@Override
annotation in classes. If you used Butterknife , @BindView
is also an annotation that hides behind itself some metadata that helps generate code.@Inject
focus on the two - @Inject
and @Component
.@Inject
public class Starks{ /** * * Inject Dagger **/ //Feild injection @Inject Allies allies; //Constructor injection @Inject public Starks(){ // - } //Method injection @Inject private void prepareForWar(){ // - } }
@Inject
annotation will tell Dagger which dependencies should be provided to the dependent object. It's like the agents of the iron bank, who negotiate with the houses and determine the amount of credit that they can provide the house.@Component
@Component
, in general, is something like a bridge between @Module
(consider this annotation later) and @Inject
.War
class - Starks
and Boltons
.build.gradle
file from the branch of my project. Also make sure that the annotation processing is enabled (File -> Settings -> Build, execution and deployment -> Compiler -> Annotation Processing -> Enable annotation processing (the flag should be checked)). Do not forget to note this: File -> Settings -> Build, execution and deployment -> Gradle -> Runner -> Delegate IDE build / run to cradle.@Inject
annotationStarks
and Boltons
into the War
class with the help of Dagger 2. What we should explicitly tell him. Below is an example of how to do this using an implementation using a constructor. public class Boltons implements House { @Inject //Dagger 2 public Boltons(){ } @Override public void prepareForWar() { // - System.out.println(this.getClass().getSimpleName()+" prepared for war"); } @Override public void reportForWar() { // - System.out.println(this.getClass().getSimpleName()+" reporting.."); } }
public class Starks implements House { @Inject //Dagger 2 public Starks(){ } @Override public void prepareForWar() { // - System.out.println(this.getClass().getSimpleName()+" prepared for war"); } @Override public void reportForWar() { // - System.out.println(this.getClass().getSimpleName()+" reporting.."); } }
War
class, where we must note this.War
War
needs to provide him with two classes on which he depends - Starks
and Boltons
. public class War { private Starks private Boltons boltons; @Inject public War(Starks starks, Boltons bolton){ this.starks = starks; this.boltons = bolton; } public void prepare(){ starks.prepareForWar(); boltons.prepareForWar(); } public void report(){ starks.reportForWar(); boltons.reportForWar(); } }
@Component
annotation@Component
is a bridge between generated code and dependencies. Also @Component
tells Dagger 2 how to @Component
dependency. Let's make the BattleComponent
interface inside the BattleOfBastards
class (can be done separately). @Component interface BattleComponent { War getWar(); }
getWar()
function will return an instance of War
that we can use in the right place.DaggerBattleComponent
- it will help us implement the War
class. Use this class to get a copy of War
. @Component interface BattleComponent { War getWar(); } public class BattleOfBastards { public static void main(String[] args){ // // Starks starks = new Starks(); // Boltons boltons = new Boltons(); // War war = new War(starks,boltons); // war.prepare(); // war.report(); // Dagger 2 BattleComponent component = DaggerBattleComponent.create(); War war = component.getWar(); war.prepare(); war.report(); } }
DaggerBattleComponent
class we can use the getWar()
method, which returns an instance of War
that embeds the Starks
and Boltons
.@Inject
and @Component
). Then we applied annotations in our example and implemented dependencies using Dagger 2.Source: https://habr.com/ru/post/344314/
All Articles